Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use erode and dilate function in opencv?

Tags:

c++

image

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;
}  
like image 969
U23r Avatar asked Jun 26 '13 20:06

U23r


2 Answers

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.

like image 154
Esenti Avatar answered Oct 25 '22 13:10

Esenti


Kernel is basically a matrix. This is multiplied or overlapped on the input matrix(image) to produce the desired output modified(in this case dilated) matrix(image).

Try changing the parameters of Mat() in dilate(img_bw, img_final, Mat(), Point(-1, -1), 2, 1, 1); you're basically changing the number of pixels (height and width) of the kernel, which will change the dilation effect on the original pic.

So in the parameters of dilate you use Mat() instead of a number as already stated by esenti.

like image 44
Tarun Avatar answered Oct 25 '22 14:10

Tarun