19,472
社区成员
发帖
与我相关
我的任务
分享
void fillHole(const Mat srcBw, Mat &dstBw)
{
Size m_Size = srcBw.size();
Mat Temp=Mat::zeros(m_Size.height+2,m_Size.width+2,srcBw.type());//延展图像
srcBw.copyTo(Temp(Range(1, m_Size.height + 1), Range(1, m_Size.width + 1)));
cv::floodFill(Temp, Point(0, 0), Scalar(255));
Mat cutImg;//裁剪延展的图像
Temp(Range(1, m_Size.height + 1), Range(1, m_Size.width + 1)).copyTo(cutImg);
dstBw = srcBw | (~cutImg);
}
int main()
{
Mat img=cv::imread("23.jpg");
Mat gray;
cv::cvtColor(img, gray, CV_RGB2GRAY);
Mat bw;
cv::threshold(gray, bw, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
Mat bwFill;
fillHole(bw, bwFill);
imshow("填充前", gray);
imshow("填充后", bwFill);
waitKey();
return 0;
}
先把图像二值化了