Applying Gaussian blur/filter to an image in opencv c++

Santhosh picture Santhosh · Feb 24, 2014 · Viewed 26.3k times · Source

I am using the Gaussian function from openCV to make my image blur/filter and to retrieve the values of the pixels of an image, before and after the blur is applied on it.

problem is there is no difference in pixel values between image without blur and with blur, but image display after the Gaussian blur is actually blurred and kernel matrix is of size 3*3. it would be surely appreciated, if anyone can solve this problem,thanks in advance, here is my code.

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

using namespace cv;
using namespace std;


int main()
{
  Mat image = imread("C:/santhu/bitmap.bmp");
  int rows=image.rows;
  int cols=image.cols;

  if (image.empty()) 
    {
      cout << "Cannot load image!" << endl;
      return -1;
    }

  cout<<"the output for matrix of pixels";

  //code to access pixel values of an image
  cout<<"\nbefore blur";
  for (int i = 0; i <rows; i++)
    {
      Vec3b *ptr = image.ptr<Vec3b>(i);

      for (int j = 0; j < cols; j++)
        {   
      Vec3b pixel = ptr[j];
      //cout<<pixel<<"\t";
        }
      cout<<"\n";
    }

  imshow("Image", image);//displaying image
  waitKey(0);

  Mat image1=image.clone();//cloning image

  GaussianBlur( image, image1, Size( 7, 7), 0, 0 );//applying Gaussian filter 


  cout<<"\nafter blur";

  //code to access pixel values of an image
  for (int i = 0; i < rows; i++)
    {
      Vec3b *ptr = image1.ptr<Vec3b>(i);

      for (int j = 0; j < cols; j++)
        {   
      Vec3b pixel = ptr[j];
      //cout<<pixel<<"\t";
        }
      cout<<"\n";
    }


  imshow("image1:",image1);//displaying image1
  waitKey(0);
}

Answer