clc;
A=imread('baimao.jpg');
figure(1);
subplot(1,2,1);
imshow(A);title('原图');
[rows , cols , colors] = size(A);%得到原来图像的矩阵的参数
n=6;
nrows = floor(rows/n)*n;%将不一定是n的整数倍的图像大小化为整数倍
ncols = floor(cols/n)*n;
for color = 1:3
for j = 1:n:nrows
for i = 1:n:ncols
Res(j:j+n-1, i:i+n-1, color) =mean(mean(A(j:j+n-1, i:i+n-1, color)));%对列进行取均值处理
end
Res(j:j+n-1,ncols:cols ,color) = mean(mean(A(j:j+n-1,ncols:cols,color)));%处理最后的列
end
for i = 1:n:ncols
Res(nrows:rows, i:i+n-1, color) = mean(mean(A(nrows:rows, i:i+n-1, color)));%处理最后的行
end
Res(nrows:rows, ncols:cols, color) = mean(mean(A(nrows:rows, ncols:cols , color)));%处理最后的角
end
subplot(1,2,2);
imshow(Res);title('马赛克图');