原文链接:http://blog.csdn.net/matrix_space/article/details/40263725
function [ output_args ] = Untitled( )
img = imread('D:/图片/5.jpg');
img = double(img);
size_info = size(img);
height = size_info(1);
width = size_info(2);
Filter_size = 7;
sigma = Filter_size/6;
F_gaussian = fspecial('gaussian',Filter_size,sigma);
N = 3;
img = imfilter(img,F_gaussian,'conv');
img_out = img;
for i = 1+N:height-N
for j = 1+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
img_out(i,j,:) = img(h,w,:);
end
end
imshow(img_out/255);
end
效果:
opencv :
void MaoBoLi(Mat & img){
Mat temp;
img.convertTo( temp, CV_32F);
Mat result_tem = temp.clone();
GaussianBlur(temp, result_tem, Size (5, 5), 0);
Mat result = temp.clone();
int size=3;
float k1, k2;
float m, n;
int h, w;
srand( (unsigned)time(NULL) );
for (int i = size; i < temp.rows - size; i++) {
float *re = result.ptr<float>(i);
float *re_t = result_tem.ptr<float>(i);
for (int j = size; j < temp.cols - size; j++) {
k1 = (float)((rand() % 100)) / 100.0 - 0.5;
k2 = (float)((rand() % 100)) / 100.0 - 0.5;
m = (k1*(size * 2 - 1));
n = (k2*(size * 2 - 1));
h = (int)(i + m) % temp.rows;
w = (int)(j + n) % temp.cols;
result.at<Vec3f>(i, j) = result_tem.at<Vec3f>(h, w);
}
}
result.convertTo(result, CV_8U);
imshow("maoboli", result);
}