How can I dilate a binary image without closing holes with OpenCV

南笙酒味 提交于 2021-02-11 06:01:38

问题


How can I apply dilation to the binary image on the left without closing the hole in the loop? I'm also interested in doing it efficiently.

Context: I need to train a CNN to read handwritten digits. Believe it or not, the image on the left is supposed to be a 9. Since the dataset has a lot of 9s written that way I may have a chance at training a model to recognise it. I do need to apply some dilation though, in order to get the digit thickness to be similar to that of the digits fed into the pre-trained model. I think if I lose the hole in the loop, I'll have no chance.


回答1:


You just need to fill the hole inside the contour, inverse it, then multiply it to the dilated image:

Here is the opencv code (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/how-can-i-dilate-a-binary-image-without-closing-holes-with-opencv

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!