参考:

PS 图像特效,非线性滤波器

// define head function
#ifndef PS_ALGORITHM_H_INCLUDED
#define PS_ALGORITHM_H_INCLUDED

#include <iostream>
#include <string>
#include "cv.h"
#include "highgui.h"
#include "cxmat.hpp"
#include "cxcore.hpp"

using namespace std;
using namespace cv;


void Show_Image(Mat&, const string &);


#endif // PS_ALGORITHM_H_INCLUDED

/*
This program will generate
"Paint" effect.

*/

#include "PS_Algorithm.h"
#include <time.h>

using namespace std;
using namespace cv;

int main(void)
{
string Img_name("4.jpg");
Mat Image_in;
Image_in=imread(Img_name);
// Show_Image(Image_in, Img_name);

Mat Image_out(Image_in.size(), CV_32FC3);
Image_in.convertTo(Image_out, CV_32FC3);


Mat r(Image_in.size(), CV_32FC1);
Mat g(Image_in.size(), CV_32FC1);
Mat b(Image_in.size(), CV_32FC1);

Mat rgb[]={b,g,r};

split(Image_out, rgb);

int P_size=3;
Mat sub_mat;

double min_val;

for (int i=P_size; i<Image_in.rows-P_size-1; i++)
{
for (int j=P_size; j<Image_in.cols-1-P_size; j++)
{
for(int k=0; k<3; k++)
{
sub_mat=rgb[k].operator()(Range(i-P_size,i+P_size), Range(j-P_size,j+P_size));
cv::minMaxLoc(sub_mat, &min_val);
Image_out.at<Vec3f>(i,j)[k]=min_val;
}
/*
sub_mat=b.operator()(Range(i-P_size,i+P_size), Range(j-P_size,j+P_size));
cv::minMaxLoc(sub_mat, min_val);
Image_out.at<Vec3f>(i,j)[0]=*min_val;

sub_mat=g.operator()(Range(i-P_size,i+P_size), Range(j-P_size,j+P_size));
cv::minMaxLoc(sub_mat, min_val);
Image_out.at<Vec3f>(i,j)[1]=*min_val;

sub_mat=r.operator()(Range(i-P_size,i+P_size), Range(j-P_size,j+P_size));
cv::minMaxLoc(sub_mat, min_val);
Image_out.at<Vec3f>(i,j)[2]=*min_val;
*/

}
}

Image_out=Image_out/255.0;

Show_Image(Image_out, "out");

imwrite("out.jpg", Image_out*255);

waitKey();
cout<<"All is well."<<endl;

}


// define the show image
#include "PS_Algorithm.h"
#include <iostream>
#include <string>

using namespace std;
using namespace cv;

void Show_Image(Mat& Image, const string& str)
{
namedWindow(str.c_str(),CV_WINDOW_AUTOSIZE);
imshow(str.c_str(), Image);

}



原图 

OpenCV——非线性滤波器_#include

效果图

OpenCV——非线性滤波器_图像特效_02