简体   繁体   English

openCV c++ 中的手动腐蚀图像问题(二进制图像)

[英]Problem with manual erosion image in openCV c++ ( Binary image)

I'm writing manual code for erosion or dilatation.我正在编写用于腐蚀或膨胀的手动代码。 And I don't know why the following code does not work.而且我不知道为什么下面的代码不起作用。

As you can see, when I'm in the point of (i,j) I go around this point and look for any white pixel.正如你所看到的,当我在 (i,j) 点时,我 go 在这个点附近寻找任何白色像素。 If there is anywehere (in the Little 3x3 matrix (ii,jj) ) white pixel I change my (i, j) point to white.如果有任何地方(在 Little 3x3 矩阵 (ii,jj) 中)白色像素,我将 (i, j) 点更改为白色。

Does anybody know why it's not working?有谁知道为什么它不起作用?

I made myself sure that I load the binary image.我确保自己加载了二进制图像。 If I count white and black Pixels summary the gile me the right number of etery Pixels.如果我计算白色和黑色像素,那么我就会知道正确的像素数。

 for (int i=1; i<gray.cols-1; i++)
    {
        for(int j=1; j<gray.rows-1; j++)
            {
                ii=i;
                jj=j;

                for (ii-1; ii<i+2; ii++ )
                {
                    for (jj-1; jj<j+2; jj++ )
                    {

                        if (gray.at<uchar>(ii,jj) ==255)
                            gray.at<uchar>(i,j) = 255;
                    };
                };
};

If anybody has an idea how to do it differently?如果有人知道如何以不同的方式做到这一点?

You should create temporary image to write results of erosion/dilation to.您应该创建临时图像来写入腐蚀/膨胀的结果。 Your problem is that you are writing to original image.您的问题是您正在写入原始图像。 Assume that only the first pixel is white.假设只有第一个像素是白色的。 At first iteration you will color second pixel to white.在第一次迭代中,您会将第二个像素着色为白色。 And then in second iteration you will look at newly colored second pixel and color third pixel in white.然后在第二次迭代中,您将看到新着色的第二个像素和白色的第三个像素。 And so on for all pixels in line.对于所有的像素,依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM