C++ 如何在opencv中使用erode和dilate函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17329932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:05:51  来源:igfitidea点击:

How to use erode and dilate function in opencv?

c++imageopencv

提问by U23r

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

图片: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;
}  

回答by Esenti

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

根据官方文档,第三个参数应该是内核(或结构元素)。您当前正在传递 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.

在这种情况下,将使用默认的 3x3 内核。

回答by Tarun

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.

尝试更改Mat()in的参数dilate(img_bw, img_final, Mat(), Point(-1, -1), 2, 1, 1);基本上是在 更改内核的像素数(高度和宽度),这将改变原始图片的膨胀效果。

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

因此,在dilate您使用的参数中,Mat()而不是 esenti 已经声明的数字。