Reputation: 3267
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.
Upvotes: 2
Views: 1144
Reputation: 1865
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);
Upvotes: 5