如何在不关闭循环中的孔的情况下对左边的二值图像进行扩展?我也对高效地做这件事感兴趣。

Context:我需要训练一个阅读手写体数字的人。信不信由你,左边的图像应该是9。因为数据集有很多这样写的9s,所以我可能有机会训练一个模型来识别它。我确实需要应用一些扩展,以使数字厚度与输入到预先训练的模型中的数字相似。我想如果我失去了循环中的洞,我就没有机会了。
发布于 2020-01-17 18:49:43
你只需要填充轮廓内的洞,反求它,然后把它相乘成膨胀的图像:

以下是opencv代码(c++):
Mat img__ = imread("E:/1.jpg", 0);
Mat img1;
threshold(img__, img1, 0, 255, THRESH_OTSU); # you don't need this line, I used it because I read the image from my hard disk. You can comment this line
vector<vector<Point>> contours;
vector< Vec4i > hierarchy;
findContours(img1, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_NONE);
Mat tmp = Mat::zeros(img1.size(), CV_8U);
for (size_t i = 0; i < contours.size(); i++)
{
if (hierarchy[i][2] < 0) # this stands for the inner contours which surrounds the hole
drawContours(tmp, contours, i, Scalar(255, 255, 255), -1);
}
Mat img2;
dilate(img1, img2, Mat::ones(9, 9, CV_8U));
imshow("original", img1);
imshow("1", tmp);
imshow("3", img2);
tmp = 255 - tmp;
imshow("2", tmp);
tmp = tmp / 255;
multiply(tmp, img2, img2);
imshow("4", img2);
waitKey(0);https://stackoverflow.com/questions/59792501
复制相似问题