Thursday, March 26, 2015

Digital Image processing with c++ ( Chapter 5 ) - Image Smoothing (Mean filter)

Today i'm going to explain how to add mean filter to noisy image


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

using namespace cv;
using namespace std;


int main()
{
 Mat RGBImage;

 RGBImage =imread("c:/Pictures/8_Color-Noise.png");

 Mat grayScaleImage(RGBImage.size(),CV_8UC1);
 grayScaleImage=Scalar(0);
 Mat FinalImage(RGBImage.size(),CV_8UC1);
 cvtColor(RGBImage,grayScaleImage,CV_RGB2GRAY);
 int kernalWidth=7;
 int kernalHeight=7;
 int kernalSize=kernalHeight*kernalWidth;
 int rows=grayScaleImage.rows;
 int cols=grayScaleImage.cols;

 int verticleImageBound=(kernalHeight-1)/2;
 int horizontalImageBound=(kernalWidth-1)/2;

 for(int row=0+verticleImageBound;row<rows-verticleImageBound;row++){

  for(int col=0+horizontalImageBound;col<cols-horizontalImageBound;col++){

   int ave=0;
   for(int kRow=0;kRow<kernalHeight;kRow++){
    for(int kCol=0;kCol<kernalWidth;kCol++){
     ave+=grayScaleImage.at<uchar>(kRow+row-verticleImageBound,kCol+col-horizontalImageBound);

    }
   }
   FinalImage.at<uchar>(row, col)=ave/kernalSize;

  }

 }


 namedWindow("Gray",0);
 resizeWindow("Gray",512,512);
 imshow("Gray",grayScaleImage);
 namedWindow("smooth",0);
 resizeWindow("smooth",512,512);
 imshow("smooth",FinalImage);

 waitKey();
 return 0;
}


Sample image can download from here


When you run this code you see results like this


2 comments:

Unknown said...

Great job machan

xaxonbabayan said...

Borgata Hotel Casino & Spa Announces Return of Casino
Borgata Hotel Casino 춘천 출장샵 & Spa Announces 광주광역 출장샵 Return of Casino Casino 속초 출장마사지 & Spa Announces Return of Casino 군포 출장마사지 Entertainment; Borgata 안성 출장샵 Hotel Casino & Spa Announces