U23r U23r - 1 month ago 12
C++ Question

How to use erode and dilate function in opencv?

I'm trying to eliminate the thing around the number with erode and dilate process. I tryed but nothing happened. I changed the values just for see if would change something, but again, nothing has changed. The image continues like in the link above. What about this parameters... I read the documentation but don't quite understand (as you can see, I was guessing in the function). What am I doing wrong?

the image: https://docs.google.com/file/d/0BzUNc6BOkYrNeVhYUk1oQjFSQTQ/edit?usp=sharing

the code:

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;

int main ( int argc, char **argv )
{
Mat im_gray;
Mat img_bw;
Mat img_final;

Mat im_rgb = imread("cam.jpg");
cvtColor(im_rgb,im_gray,CV_RGB2GRAY);


adaptiveThreshold(im_gray, img_bw, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY_INV, 105, 1);


dilate(img_bw, img_final, 0, Point(-1, -1), 2, 1, 1);


imwrite("cam_final.jpg", img_final);

return 0;
}

Answer

According to official docs, the third argument should be the kernel (or structuring element). You are currently passing 0:

dilate(img_bw, img_final, 0, Point(-1, -1), 2, 1, 1);

Try rewriting it this way:

dilate(img_bw, img_final, Mat(), Point(-1, -1), 2, 1, 1);

In this case, a default 3x3 kernel will be used.