DGMJ DGMJ - 2 months ago 5
C++ Question

How to get clean edge without broken lines?

I'm working on a binary image to get its edge. I used cannyedge function from opencv but the result is less desirable.

Click for the Images

int edgeThresh = 1;
int lowThreshold = 100;
int const max_lowThreshold = 100;
int ratio = 3;
int kernel_size = 3;

blur(binaryImage, detected_edges, Size(3, 3));
Canny(binaryImage, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size);
dst = Scalar::all(0);
src.copyTo(dst, detected_edges);
imwrite(defaultPath + "edge_" + filename, dst);

I did a dirty workaround which works but again added to processing time:

Canny(detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size);
blur(detected_edges, detected_edges, Size(3, 3));
Canny(detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size);

I am new to opencv and image processing so most likely I am missing something.

Please enlighten me. Thanks!


FIRSTLY: for the image like this, where it just white and black pixels, use findContours function, it would be faster and more precise. If you need to draw contours you just found (draw your result) use drawContours function (draw on new Mat, the same size of the one you found the contours on).

Documentation about this HERE.

SECONDLY: it may be a problem that your values of kernel size or thresholds are incorrect. The second threshold is reccomended to be 3 times bigger than the first one. I think that the problem may be with your kernel size (last argument in Canny function). Try not using this function overload at all, use the one without this argument or lower your kernel size.