%%%% Tile
%%%%% 实现拼贴效果
%%%%% 将原图像进行分块,然后让图像块在
%%%%% 新图像范围内进行随机移动,确定移动后的边界
%%%%% 将移动后的图像块填入新图像内
clc;
clear all;
close all;
Image=imread(‘4.jpg‘);
Image1=double(Image);
Image2=Image1;
Image2(:,:,1)=0;
Image2(:,:,2)=0;
Image2(:,:,3)=0;
%%%% 块数 (1-99)
N=8;
%%%% 错开距离的最大百分比(0.01-0.99)
Percent=0.25;
Size_Info=size(Image);
%%% 确定图像的长和宽
Height=Size_Info(1);
Width=Size_Info(2);
%%% 确定图像的分块大小,及错开的最大距离
Min_value=min(Width,Height);
Block_size=floor(Min_value/N);
Rand_Dist=max(Block_size*Percent,2);
%%% 确定图像在长和宽方向的块数
N_row=floor(Height/Block_size);
N_col=floor(Width/Block_size);
Block_height=0;
Block_width=0;
%%% 图像如果在长或宽方向有多余的,统计在内
if(N_row*Block_size<Height)
Block_height=(Height-Block_size*N_row)/2;
N_row=N_row+1;
end
if(N_col*Block_size<Width)
Block_width=(Width-Block_size*N_col)/2;
N_col=N_col+1;
end
First_value=(Block_size+1)/2;
%%% 图像块原始坐标
Row_coordinate_origin(1:N_row)=0;
Col_coordinate_origin(1:N_col)=0;
for i=1:N_row-1;
Row_coordinate_origin(i)=round(First_value+(i-1)*Block_size);
end
Row_coordinate_origin(N_row)=round((Height+(N_row-1)*Block_size)/2);
for i=1:N_col-1
Col_coordinate_origin(i)=round(First_value+(i-1)*Block_size);
end
Col_coordinate_origin(N_col)=round((Width+(N_col-1)*Block_size)/2);
%%% 图像块的错开后的坐标
Row_coordinate(1:N_row,1:N_col)=0;
Col_coordinate(1:N_row,1:N_col)=0;
for i=1:N_row;
for j=1:N_col
Row_coordinate(i,j)=Row_coordinate_origin(i)+floor(Rand_Dist*2*(rand()-0.5));
end
end
for i=1:N_row
for j=1:N_col
Col_coordinate(i,j)=Col_coordinate_origin(j)+floor(Rand_Dist*2*(rand()-0.5));
end
end
%%% 对图像进行拼贴处理
Block_half=(Block_size-1)/2;
for i=1:N_row
for j=1:N_col
%%% 首先确定错开后的坐标没有越界
if(Row_coordinate(i,j)>Height || Col_coordinate(i,j)>Width)
continue;
end
%%% 将图像块分成四部分,从图像的中心向四条边进行搜索
%%% 计算图像块四个方向的边界
%%% 计算图像块上边界
if(Row_coordinate(i,j)-Block_half<1)
Block_h_down=Row_coordinate(i,j)-1;
row_down=1;
row_down_origin=Row_coordinate_origin(i)-Block_h_down;
else
row_down=round(Row_coordinate(i,j)-Block_half);
row_down_origin=round(Row_coordinate_origin(i)-Block_half);
end
%%% 计算图像块的下边界
if(Row_coordinate(i,j)+Block_half>Height)
Block_h_up=Height-Row_coordinate(i,j);
row_up=Height;
row_up_origin=Row_coordinate_origin(i)+Block_h_up;
else
row_up=round(Row_coordinate(i,j)+Block_half);
row_up_origin=round(Row_coordinate_origin(i)+Block_half);
end
%%% 如果最下面的图像块比标准的要小,单独计算
%%% 多出的一部分的下边界
if(i==N_row && Block_height~=0)
if(Row_coordinate(i,j)+Block_height>Height)
Block_h_up=Height-Row_coordinate(i,j);
row_up=Height;
row_up_origin=Row_coordinate_origin(i)+Block_h_up;
else
row_up=floor(Row_coordinate(i,j)+Block_height);
row_up_origin=floor(Row_coordinate_origin(i)+Block_height);
end
end
%%% 计算图像的左边界
if(Col_coordinate(i,j)-Block_half<1)
Block_w_down=Col_coordinate(i,j)-1;
col_down=1;
col_down_origin=Col_coordinate_origin(j)-Block_w_down;
else
col_down=round(Col_coordinate(i,j)-Block_half);
col_down_origin=round(Col_coordinate_origin(j)-Block_half);
end
%%计算图像的右边界
if(Col_coordinate(i,j)+Block_half>Width)
Block_w_up=Width-Col_coordinate(i,j);
col_up=Width;
col_up_origin=Col_coordinate_origin(j)+Block_w_up;
else
col_up=round(Col_coordinate(i,j)+Block_half);
col_up_origin=round(Col_coordinate_origin(j)+Block_half);
end
%%% 如果最右边的图像块比标准的要小,单独计算
%%% 多出的一部分的右边界
if(j==N_col && Block_width~=0)
if(Col_coordinate(i,j)+Block_width>Width)
Block_w_up=Width-Col_coordinate(i,j);
col_up=Width;
col_up_origin=Col_coordinate_origin(j)+Block_w_up;
else
col_up=floor(Col_coordinate(i,j)+Block_width);
col_up_origin=floor(Col_coordinate_origin(j)+Block_width);
end
end
Image2(row_down:row_up,col_down:col_up,:) = ...
Image(row_down_origin:row_up_origin,col_down_origin:col_up_origin,:);
end
end
imshow(Image2/255);
原图:
效果图: