#include <QCoreApplication>
#include <iostream>
#include <fstream>
#include <opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <math.h>
int main(int argc, char *argv[])
{
cv::Mat srcImage = cv::imread("./imori.jpg",cv::IMREAD_COLOR);
int width = srcImage.rows;
int height = srcImage.cols;
cv::Mat dstImage = cv::Mat::zeros(height,width,CV_8UC3);
int k_size = 3;
int p = floor(k_size / 2);
//filtering
double v = 0;
int *vs = new int[k_size * k_size]; //int vs[9];
int count = 0;
for (int j = 0; j < height;j++)
{
for (int i = 0 ; i < width ; i++)
{
for (int c = 0; c<3; c++)
{
v = 0;
count = 0;
//初始化一个3*3的kernel
for(int i = 0;i < k_size*k_size;i++)
{
vs[i] = 999;
}
//将每一个像素anchor点,取其3*3的像素值,储存在vs[3*3]中
for(int _j = -p;_j < p+1;_j++)
{
for (int _i = -p; _i < p+1;_i++)
{
//当此像素anchor不为图像的第一个像素点时
if(((j + _j) >= 0)&&((i + _i) >=0))
{
vs[count++] = (int)srcImage.at<cv::Vec3b>(j + _j,i + _i)[c];
}
}
}
//将3*3的数组,按升序进行排序sort(first,last)
std::sort(vs,vs + (k_size * k_size));
//取其中值,定义为该像素的灰度值
dstImage.at<cv::Vec3b>(j,i)[c] = (uchar)vs[int(floor(count/2)) + 1];
}
}
}
cv::imshow("SrcImage",srcImage);
cv::imshow("DstImage",dstImage);
std::cout << "END!" << std::endl;
cv::waitKey(0);
return 0;
}