Ask Your Question
0

Blurring issues (only 1/3 of the image is blurred)

asked 2014-03-26 23:31:30 -0600

zuba gravatar image

updated 2014-03-27 10:58:23 -0600

Haris gravatar image

Hello, I am trying for days to figure out what the prolem is in my loop or blurring function. I get only the right 1/3 of the image blurred. Please help!

void Blurring (const Mat& input, Mat& output)
{

      float sum;
      float neighborhood[3][3] = {
                            {1/9.0, 1/9.0, 1/9.0},
                            {1/9.0, 1/9.0, 1/9.0},
                            {1/9.0, 1/9.0, 1/9.0}
                           }; 

       for(int y = 0; y < input.rows; y++)
            for(int x = 0; x < input.cols; x++)
                output.at<unsigned char>(y,x) = 0.0;
                output = input.clone();

        //bluring operation
       for(int y = 1; y < input.rows - 1; y++){
            for(int x = 1; x < input.cols - 1; x++){
                sum = 0.0;
                for(int k = -1; k <= 1;k++){
                    for(int j = -1; j <=1; j++){
                        sum = sum + neighborhood[j+1][k+1]*input.at<unsigned char>(y - j, x - k);
                    }
                }
                output.at<unsigned char>(y,x) = sum;
            }
        }
}
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
4

answered 2014-03-27 01:00:21 -0600

Haris gravatar image

updated 2014-03-27 02:12:00 -0600

As Martin Peris said in your code probably you are passing 3 channel image and accessing pixel value using 'uchar', so either pass gray image or use vec3b to access pixel value.

You can use OpenCV inbuilt filter2D instead your own.

  Mat src=imread("img.jpg");
  Mat dst; 
  Mat kernel=(Mat_<float>(3,3)<<  1/9.0, 1/9.0, 1/9.0,\
                                  1/9.0, 1/9.0, 1/9.0,\
                                  1/9.0, 1/9.0, 1/9.0);

  filter2D(src, dst,-1, kernel,Point(-1,-1),0,BORDER_DEFAULT );
edit flag offensive delete link more
1

answered 2014-03-27 01:21:22 -0600

I agree with Haris Moonamkunnu, you can use OpenCV built-in functionality to do what you want.

Besides, you are getting only 1/3 of the image blurred with your function because you are probably providing it a color image (3 channels - RGB) as input and you are looping over it as a grayscale image (1 channel - GRAY).

edit flag offensive delete link more

Comments

Thank you guys for taking the time to help. Actually I don't know if it would be considered cheating to use a built in function instead of doing our own loop. The image I am imreading is a gray image by the way. The assignment is about averaging the image , each pixel with its 8 neighbors....(3x3)

In other words what exactly do I have to do to fix the issue in my coding? I am too new to programming! :(

zuba gravatar imagezuba ( 2014-03-27 09:45:00 -0600 )edit

@zuba Your code works fine here with gray scale image, one more time try with Mat src=imread("dest.jpg",0); other wise show your full code.

Haris gravatar imageHaris ( 2014-03-27 10:55:22 -0600 )edit
1

Thank you. All I had to do was to add the CV_LOAD_IMAGE_GRAYSCALE in the imread statement and now works great! Or 0 as you said correctly!

zuba gravatar imagezuba ( 2014-03-27 11:40:52 -0600 )edit

Question Tools

Stats

Asked: 2014-03-26 23:31:30 -0600

Seen: 407 times

Last updated: Mar 27 '14