% method : 利用邻域的任意一点代替当前邻域所有像素点
%%%% mosaic
clc; clear all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); Image=imread('4.jpg'); Image=double(Image); size_info=size(Image); height=size_info(1); width=size_info(2); N=11; % 控制邻域大小 Image_out=Image; for i=1+N:N:height-N for j=1+N:N:width-N k1=rand()-0.5; k2=rand()-0.5; m=(k1*(N*2-1)); n=(k2*(N*2-1)); h=floor(mod(i+m,height)); w=floor(mod(j+n,width)); if w==0; w=width; end if h==0 h=height; end Image_out(i-N:i+N,j-N:j+N,1)=Image(h,w,1); Image_out(i-N:i+N,j-N:j+N,2)=Image(h,w,2); Image_out(i-N:i+N,j-N:j+N,3)=Image(h,w,3); end end
imshow(Image_out/255);
原图
效果图
具体的算法 可以参考:
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 "Mosaic" 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); int P_size = 9 ; float k1, k2; int n_row; int n_col; float m, n; int h,w; n_row = Image_in .rows; n_col = Image_in .cols; Mat sub_mat; srand( (unsigned)time(NULL) ); for (int i = P_size ; i < Image_in.rows-P_size-1 ; i = i +P_size) { for (int j = P_size ; j < Image_in.cols-1-P_size ; j = j +P_size) { k1 =(double)((rand() % 100))/100.0-0.5; k2 =(double)((rand() % 100))/100.0-0.5; m =(k1*(P_size*2-1)); n =(k2*(P_size*2-1)); h =(int)(i+m)% n_row; w =(int)(j+n)% n_col; sub_mat = Image_out .operator()(Range(i-P_size,i+P_size), Range(j-P_size,j+P_size)); sub_mat.setTo(Scalar(Image_in.at< Vec3b > (h,w))); } } 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); }
原图
效果图