Ask Your Question
0

Dilate and Erode

asked Nov 4 '17

procton gravatar image

updated Nov 9 '17

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

Preview: (hide)

1 answer

Sort by » oldest newest most voted
0

answered Nov 5 '17

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.

Preview: (hide)

Comments

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 (Nov 5 '17)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 (Nov 5 '17)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 (Nov 5 '17)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 (Nov 5 '17)edit

Question Tools

1 follower

Stats

Asked: Nov 4 '17

Seen: 1,652 times

Last updated: Nov 09 '17