Skip to content

Instantly share code, notes, and snippets.

@bowbowbow
Created January 1, 2016 04:40
Show Gist options
  • Save bowbowbow/5e17263523227e0129b2 to your computer and use it in GitHub Desktop.
Save bowbowbow/5e17263523227e0129b2 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <math.h>
#include <opencv2/opencv.hpp>
using namespace cv;
Mat sourceImg;
Mat resultImg;
//가우시안 함수
double gaussian_func(int y, int x, double sd){
double sd2 = pow(sd,2);
return exp(-(x*x+y*y)/(2*sd2))/(asin(1)*2*2*sd2);
}
unsigned char gaussian_filter(int y, int x, double sd){
int w = ceil(sd*6);
if(w %2 == 0)
w++;
int sum = 0;
for(int ny = -w/2; ny <= w/2; ny++){
for(int nx = -w/2 ; nx <= w/2; 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)*gaussian_func(ny, nx, sd);
}
}
return sum;
}
int main(){
double sd;
scanf("%lf", &sd);
sourceImg = imread("/Users/clsrn1581/Desktop/noise.png", 0);
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) = gaussian_filter(y,x, sd);
}
}
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