#include<opencv2/core/core.hpp>
#include"MyBlur.hpp"
using namespace cv;
Mat MyMeanFilter(Mat& inputMat,int oddNum){
Mat dstImg(inputMat.rows,inputMat.cols,inputMat.type());
for(int i=0;i<inputMat.rows;i++){
for(int j=0;j<inputMat.cols;j++){
int mid = oddNum/2;//size 5x5 7x7 9x9 ... odd x odd
int r = i-mid;
int c = j-mid;
int sumB = 0;
int sumG = 0;
int sumR = 0;
for(int x=0;x<oddNum;x++){
for(int y=0;y<oddNum;y++){
//dealing of overflow of boundary
//eq: overflow region of elements is 0(zero)!(I think)
if(r + x < 0 || c + y < 0 ||r+x > inputMat.rows-1 || c+y > inputMat.cols-1){
continue;
}
sumB += inputMat.at<Vec3b>(r+x,c+y)[0];
sumG += inputMat.at<Vec3b>(r+x,c+y)[1];
sumR += inputMat.at<Vec3b>(r+x,c+y)[2];
}
}
sumB -= inputMat.at<Vec3b>(i,j)[0];
sumG -= inputMat.at<Vec3b>(i,j)[1];
sumR -= inputMat.at<Vec3b>(i,j)[2];
int dividend = oddNum * oddNum - 1;
dstImg.at<Vec3b>(i,j)[0] = saturate_cast<uchar>(sumB/dividend);
dstImg.at<Vec3b>(i,j)[1] = saturate_cast<uchar>(sumG/dividend);
dstImg.at<Vec3b>(i,j)[2] = saturate_cast<uchar>(sumR/dividend);
}
}
return dstImg;
}
#include"OpenCVLib2.hpp"
using namespace cv;
int main(){
Mat img = imread("p1.jpg");
imshow("Origin",img);
Mat&& dstImg = MyMeanFilter(img,7);
imshow("Effect1",dstImg);
dstImg = MyMeanFilter(img,17);
imshow("Effect2",dstImg);
waitKey(0);
return 0;
}