Skip to content

Instantly share code, notes, and snippets.

@bowbowbow
Last active January 1, 2016 04:40
Show Gist options
  • Save bowbowbow/0d4a900083601d765408 to your computer and use it in GitHub Desktop.
Save bowbowbow/0d4a900083601d765408 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <math.h>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
Mat sourceImg;
Mat resultImg;
unsigned char average_filter(int y, int x, int w){
int sum = 0;
for(int ny = -w; ny <= w; ny++){
for(int nx = -w ; nx <= w; nx++){
int ty = y+ny;
int tx = x+nx;
//ty와 tx가 범위 밖을 나갔을 때 테두리 선에서 선대칭 되는 위치에 있는 픽셀값을 이용했습니다.
if(ty<0)
ty *= -1;
if(ty>=sourceImg.rows)
ty = 2*sourceImg.rows-ty;
if(tx<0)
tx *= -1;
if(tx>=sourceImg.cols)
tx = 2*sourceImg.cols-tx;
sum += (int)sourceImg.at<unsigned char>(ty, tx);
}
}
return sum/((2*w+1)*(2*w+1));
}
int main(){
sourceImg =imread("/Users/clsrn1581/Desktop/noise.png", CV_LOAD_IMAGE_GRAYSCALE);
resultImg = Mat(sourceImg.rows, sourceImg.cols, CV_8UC1, 1);
for(int y = 0 ;y < sourceImg.rows; y++){
for(int x = 0; x < sourceImg.cols; x++){
resultImg.at<unsigned char>(y,x) = average_filter(y,x, 5);
}
}
namedWindow("Source Image");
namedWindow("Result Image");
imshow("Source Image", sourceImg);
imshow("Result Image", resultImg);
int key = waitKey();
destroyAllWindows();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment