简体   繁体   English

我的均值滤波器是否正确? 我需要更改使其工作吗?

[英]Am I doing the mean filter correct? What do i need to change to make it work?

The problem is i dont know how to get the math done properly when using a mean filter. 问题是我不知道在使用均值滤波器时如何正确地完成数学运算。 3x3 kernel with a weight value of 1 in all 9 kernels. 在所有9个内核中权重值为1的3x3内核。 I got some help to use the sum part, but i do not know if it works correctly, i certainly cant build. 我得到了一些使用总和部分的帮助,但是我不知道它是否正确运行,我当然无法构建。

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()

{
    Mat gray_image, convolued_image;

    gray_image = imread( "C:/1.jpg", CV_LOAD_IMAGE_GRAYSCALE);   // Read the file
    convolued_image = gray_image;


    if(!gray_image.data )                                     // Check for invalid input

    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

    namedWindow( "RGB Input", CV_WINDOW_AUTOSIZE );    
    imshow( "RGB Input", gray_image );                 



     Mat meanImg;
       gray_image.copyTo(meanImg);

      namedWindow( "meanImg", CV_WINDOW_AUTOSIZE );    
    imshow( "meanImg", meanImg );  


        waitKey(0);    

        for (int y = 0; y < gray_image.rows; y++)
       {
               for (int x = 0; x < gray_image.cols; y++)
              {
                      int intesity = gray_image.at<uchar>(y,x);       
                      int sum = gray_image.at<uchar>(y+1,x+1);
                            sum = gray_image.at<uchar>(y+1,x);
                            sum = gray_image.at<uchar>(y+1,x-1);
                            sum = gray_image.at<uchar>(y,x-1);
                            sum = gray_image.at<uchar>(y,x+1);
                            sum = gray_image.at<uchar>(y-1,x-1);
                            sum = gray_image.at<uchar>(y+1,x);
                            sum = gray_image.at<uchar>(y+1,x+1);

                    int mean = sum/9;

                meanImg.at<uchar>(y,x) = mean;


              }
       }


    return 0;
}
sum = gray_image.at<uchar>(y+1,x);
sum = gray_image.at<uchar>(y+1,x-1);
sum = gray_image.at<uchar>(y,x-1);

Your variable is named sum , but you aren't actually adding anything here. 您的变量名为sum ,但是您实际上并未在此处添加任何内容。

You also start off your loop by reading outside the bounds of the image. 您还可以通过读取图像边界之外的内容来开始循环。

Once you get it to build, you'll have these bugs to attend to: 构建好之后,您将遇到以下错误:

You left out a character in 您在其中遗漏了一个角色

sum = gray_image.at<uchar>(y+1,x);

These lines replace the value of sum with a new value. 这些行用新值替换sum的值。

To actually add them together, do 要将它们实际添加在一起,请执行

sum += gray_image.at<uchar>(y+1,x);

You're also indexing outside the image bounds - you need to handle the cases where x or y are 0 or their respective maximum properly. 您还要在图像边界之外建立索引-您需要正确处理xy为0或它们各自的最大值的情况。

And in the inner loop, you say y++ where you should say x++ . 在内部循环中,您说y++ ,而应该说x++

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

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