侵蚀无法正常工作

Erosion not working correct

本文关键字:工作 常工作      更新时间:2023-10-16

我试图在c 中实现侵蚀(形态操作(,我使用openCV来读取和显示侵蚀后的第一个图像和图像。

我想逐步解释我的表现:

  1. 我使用一个随机值为255或0的矩阵创建了我的"二进制图像"(我只使用了黑白(。
  2. 我将初始图像复制到最终图像
  3. 然后,我用3x3掩码浏览了矩阵(最终图像(,如果我的9个值为255。

我的面具喜欢:

[255 255 255
255 255 255
255 255 255]
  1. 如果我的计数器是= 9,我为黑色面具中心不同的像素涂上了颜色。

这是我的代码:

#include <iostream>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace std;
using namespace cv;
#define WIDTH  16
#define HEIGHT 16
int main( int argc, char** argv )
{
    Mat image(HEIGHT, WIDTH, CV_8UC1);
    Mat imageFinal(HEIGHT, WIDTH, CV_8UC1);
    int values[WIDTH][HEIGHT] = {0, 0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 0, 0,
                                 0, 0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 0, 0,
                                 0, 0,   0, 255, 255, 255,   0,   0,   0,   0,   0,   0,   0,   0, 0, 0,
                                 0, 0, 255, 255, 255, 255, 255,   0,   0,   0,   0,   0,   0,   0, 0, 0,
                                 0, 0, 255, 255, 255, 255, 255,   0,   0,   0,   0, 255, 255, 255, 0, 0,
                                 0, 0, 255, 255, 255, 255,   0,   0,   0,   0, 255, 255, 255, 255, 0, 0,
                                 0, 0,   0, 255, 255,   0,   0,   0,   0, 255, 255, 255, 255, 255, 0, 0,
                                 0, 0,   0,   0,   0,   0,   0,   0, 255, 255, 255, 255, 255,   0, 0, 0,
                                 0, 0,   0,   0,   0,   0,   0, 255, 255, 255, 255, 255,   0,   0, 0, 0,
                                 0, 0,   0,   0,   0,   0, 255, 255, 255, 255, 255,   0,   0,   0, 0, 0,
                                 0, 0,   0,   0,   0, 255, 255, 255, 255, 255,   0,   0,   0,   0, 0, 0,
                                 0, 0,   0,   0, 255, 255, 255, 255, 255,   0,   0,   0,   0,   0, 0, 0,
                                 0, 0,   0,   0, 255, 255, 255, 255, 255, 255, 255, 255,   0,   0, 0, 0,
                                 0, 0,   0,   0, 255, 255, 255, 255, 255, 255, 255, 255,   0,   0, 0, 0,
                                 0, 0,   0,   0,   0, 255, 255, 255, 255, 255, 255,   0,   0,   0, 0, 0,
                                 0, 0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 0, 0};
    for(int row = 0; row < WIDTH; row++){
        for(int col = 0; col < HEIGHT; col++){
            image.data[col + row * image.cols] = values[row][col];
        }
    }
    image.copyTo(imageFinal);
    int count = 0;
    for(int row = 1; row < image.rows-1; row++){
        for(int col = 1; col < image.cols-1; col++){
            count = 0;
            for(int a = -1; a <= 1; a++){
                for(int b = -1; b <= 1; b++){
                    if(image.at<uchar>(row + a, col + b) == 255){
                        count++;
                    }
                }
            }cout << count << endl;
            if(count == 9){
                for(int a = -1; a <= 1; a++){
                    for(int b = -1; b <= 1; b++){
                        if(a != 0 && b != 0){
                            imageFinal.at<uchar>(row + a, col + b) = 0;
                        }
                    }
                }
            }
        }
    }
    imshow("Image", image);
    imshow("final", imageFinal);
    waitKey(0);  
    return 0;
}

但结果不正确。

初始图像和正确的结果:[错误的结果] [1]

我不是100%确定的,但是在阅读了侵蚀的Wiki条目后,我认为正确实现将是这样的:

for each pixel(x,y) in original_image
    count neigbours where original_image == 255
    if count == 9
        new_image(x,y) = 255
    else 
        new_image(x,y) = 0
    end
end

当你有

for each pixel(x,y) in original_image
    count neigbours where original_image == 255
    if count == 9
        set all neighbours (but not the pixel itself) in new_image to 0
end

基本上侵蚀应对图像的3x3部分进行...

1 1 1         ? ? ?            0 1 1      ? ? ? 
1 1 1    ->   ? 1 ?      and   1 1 1  ->  ? 0 ? 
1 1 1         ? ? ?            1 1 1      ? ? ? 

但是你做

1 1 1         0 0 0 
1 1 1    ->   0 1 0
1 1 1         0 0 0 

应用过滤器时,您应该仅更改您要查看的像素的值,但是您可以更改所有邻居,而不是像素本身。

我在Wiki文章中发现公式不太启发,也许这有助于理解:

new_image(x,y) = (3x3Sub(old_image,x,y) == mask) * 255