使用opencv实现matlab中的imfill填充孔洞功能,整体思路如下:

1.      首先给原始图像四周加一圈全0,并保存为另一幅图像

2.      使用floodFill函数给新图像进行填充,种子点设置为Point(0, 0),填充颜色为全白。因为原始图像四周加了一圈0,因此使用floodFill填充之后,整个图像除了原始图像中内部的点是黑色之外其他地方全是白色。

3.      将填充之后的图像颜色反转,再剪裁成原始图像大小。此时这张图像除了内部需要填充的地方是白色之外其他地方都是黑色。

4.      最后将新图像和原始图像取个并集,完成。

代码如下:

/**
\brief  填充二值图像孔洞
\param  srcimage    [in]    输入具有孔洞的二值图像
\param  dstimage    [out]   输出填充孔洞的二值图像

\return void
*/
void imfill(Mat srcimage, Mat &dstimage)
{
    Size m_Size = srcimage.size();  
    Mat temimage = Mat::zeros(m_Size.height + 2, m_Size.width + 2, srcimage.type());//延展图像    
    //imshow("temimage", temimage);
    srcimage.copyTo(temimage(Range(1, m_Size.height + 1), Range(1, m_Size.width + 1)));  
    floodFill(temimage, Point(0,0), Scalar(255)); 
    //imshow("temimage", temimage);
    //waitKey(0);
    Mat cutImg;//裁剪延展的图像 
    temimage(Range(1, m_Size.height + 1), Range(1, m_Size.width + 1)).copyTo(cutImg);  
    dstimage = srcimage | (~cutImg);  
}

GitHub 加速计划 / opencv31 / opencv
77.36 K
55.71 K
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:1 个月前 )
3919f33e G-API: Introduce level optimization flag for ONNXRT backend #26293 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake 18 小时前
489df18a Use border value in ipp version of warp affine #26313 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake 20 小时前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐