Ask Your Question

Dilate and Erode

asked 2017-11-04 07:38:37 -0500

procton gravatar image

updated 2017-11-09 12:40:37 -0500

Just a general question regarding contour detection. I have found several examples where dilation and erosion is made sometimes before canny or sobel and sometime after. Which is the correct approach ? In my scenario I have 2048x2048 images with very little objet to detect, very close each other.

My goal is to get bulding contours. This is the input image. image description

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-11-05 06:10:56 -0500

matman gravatar image

Dilate and erode are maximum-/minimum filters. So there is no correct or wrong approach. It simply depends on what you try to achieve.

Please post some example images and your code, so people can take a look at your specific problem.

edit flag offensive delete link more


My goal is to get bulding contours. This is the input image. I am using a single HSV channel My code (most important pieces for char limit on comment).

Imgproc.medianBlur(hsv, median, 1);
Imgproc.threshold(median, bin, 30, 255, Imgproc.THRESH_BINARY);
Imgproc.Sobel(bin, grad_x, CvType.CV_16S, 1, 0, 3, 1, 0, Core.BORDER_DEFAULT);
Core.convertScaleAbs(grad_x, abs_grad_x);
Imgproc.Sobel(bin, grad_y, CvType.CV_16S, 0, 1, 3, 1, 0, Core.BORDER_DEFAULT);
Core.convertScaleAbs(grad_y, abs_grad_y);

Imgproc.findContours(grad, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_NONE);
procton gravatar imageprocton ( 2017-11-05 08:16:17 -0500 )edit

Processing a Sobel filter on a binarized image makes little sense.

You can try to smooth the input image a little bit using cv::GaussianBlur and than find the edges using cv::Canny on the colour image. After that you can use cv::findContours.

Otherwise you can play around with cv::adaptiveThreshold on the grayscale image and find contours right after that. If you have merged or broken contours you can try to use cv::erode or cv::dilate before finding contours.

matman gravatar imagematman ( 2017-11-05 09:38:34 -0500 )edit

if I well understand you are refeferring to the original image ? Do you discourage the use of HSV channel, as I am trying ? What do you think if I try to detect buildings using inRange ? (I see that on Hsv channel buildings are close to black color

procton gravatar imageprocton ( 2017-11-05 10:19:22 -0500 )edit

For Canny it is the best using the colour image because the algorithm uses the strongest gradient for each pixel out of the 3 channels.

Using adaptiveThreshold makes the binarization more robust against inhomogeneous illumination compared to a simple threshold. For this you need a grayscale/single-channel image which is the V-channel in HSV image for example, but you can play around with the other channels, too. You can use what ever fits best, these are just some ideas you could try.

matman gravatar imagematman ( 2017-11-05 11:44:39 -0500 )edit

Question Tools

1 follower


Asked: 2017-11-04 07:38:37 -0500

Seen: 1,006 times

Last updated: Nov 09 '17