Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
腐蚀不能正常工作 我尝试在C++中实现腐蚀(形态学运算),用OpenCV读取并显示腐蚀后的第一幅图像和图像。p>_C++_Opencv_Image Processing - Fatal编程技术网

腐蚀不能正常工作 我尝试在C++中实现腐蚀(形态学运算),用OpenCV读取并显示腐蚀后的第一幅图像和图像。p>

腐蚀不能正常工作 我尝试在C++中实现腐蚀(形态学运算),用OpenCV读取并显示腐蚀后的第一幅图像和图像。p>,c++,opencv,image-processing,C++,Opencv,Image Processing,我想一步一步地解释我是如何做到的: 我使用一个随机值为255或0的矩阵创建了我的“二进制图像”(我只使用了黑白) 我将初始图像复制到最终图像中 然后我用一个3x3的遮罩检查了矩阵(最终图像),然后我计算了9的值是否为255 我的面具像: [255 255 255 255 255 255 255 255 255] 如果我的计数器=9,我会将与遮罩中心不同的像素涂成黑色 这是我的代码: #include <iostream> #include "opencv2/imgproc/imgp

我想一步一步地解释我是如何做到的:

  • 我使用一个随机值为255或0的矩阵创建了我的“二进制图像”(我只使用了黑白)
  • 我将初始图像复制到最终图像中
  • 然后我用一个3x3的遮罩检查了矩阵(最终图像),然后我计算了9的值是否为255
  • 我的面具像:

    [255 255 255
    255 255 255
    255 255 255]
    
  • 如果我的计数器=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;
    }
    
    #包括
    #包括“opencv2/imgproc/imgproc.hpp”
    #包括“opencv2/highgui/highgui.hpp”
    使用名称空间std;
    使用名称空间cv;
    #定义宽度16
    #定义高度16
    int main(int argc,字符**argv)
    {
    垫图像(高度、宽度、CV_8UC1);
    Mat imageFinal(高度、宽度、CV_8UC1);
    int值[宽度][高度]={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, 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};
    对于(int row=0;row对于(int a=-1;a我不是100%确定,但在阅读了之后,我认为正确的实现如下所示:

    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 
    
    应用过滤器时,您应该只更改正在查看的像素的值,但更改所有相邻像素,而不是像素本身

    我觉得维基文章中的公式不太有启发性,也许这有助于理解:

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

    为什么我有-1?这不是个好问题?或者“但结果不正确”…什么是“不正确”?图像太小,看不出任何差异。但侵蚀的正确公式或过滤器是什么?我写了一个答案,不保证正确,但您可以尝试一下。您已经链接到同一图像两次。非常感谢您的解释:D@Laurentiu不客气。尽管你的问题已经解决,你接受了答案,请考虑改进这个问题。在目前的状态中,还不清楚产生的图像有什么问题。