代码如下:
function Img=drawObject2(base_points,texturesize)
% Draw the contour as one closed line white line in an image, and make
% the object (hand) white using imfill
%{
input:
base_points : 表示轮廓点坐标
texturesize : 指的是待处理图像的宽高
output:
I_texture : 表示只有目标区域为白色,其余为黑色的二值图
Img : 表示以目标区域的中心为中心,抠出含有
目标区域的40*40的正方形
Center_x :表示抠出图像的中心的横坐标
Center_y :表示抠出图像的中心的纵坐标
%}
Length=length(base_points);
I_texture=false(texturesize+2);
if(nargin<3)
for k=1:Length
x{1,k}=round([base_points(k).y(:);base_points(k).y(1,1)]); x{1,k}=min(max(x{1,k},1),texturesize(1));
y{1,k}=round([base_points(k).x(:);base_points(k).x(1,1)]); y{1,k}=min(max(y{1,k},1),texturesize(2));
Lines{1,k}=[(1:length(x{1,k})-1)' (2:length(x{1,k}))'];
end
else
for k=1:Length
x{1,k}=round(base_points(k).y(:)); x{1,k}=min(max(x{1,k},1),texturesize(1));
y{1,k}=round(base_points(k).x(:)); y{1,k}=min(max(y{1,k},1),texturesize(2));
end
end
for i=1:Length
for j=1:size(Lines{1,i},1)
xp=[x{1,i}(Lines{1,i}(j,1)) x{1,i}(Lines{1,i}(j,2))];
yp=[y{1,i}(Lines{1,i}(j,1)) y{1,i}(Lines{1,i}(j,2))];
dx=abs(xp(2)-xp(1)); dy=abs(yp(2)-yp(1));
if(dx==dy)
if(xp(2)>xp(1)), xline=xp(1):xp(2); else xline=xp(1):-1:xp(2); end
if(yp(2)>yp(1)), yline=yp(1):yp(2); else yline=yp(1):-1:yp(2); end
elseif(dx>dy)
if(xp(2)>xp(1)), xline=xp(1):xp(2); else xline=xp(1):-1:xp(2); end
yline=linspace(yp(1),yp(2),length(xline));
else
if(yp(2)>yp(1)), yline=yp(1):yp(2); else yline=yp(1):-1:yp(2); end
xline=linspace(xp(1),xp(2),length(yline));
end
I_texture(round(xline+1)+(round(yline+1)-1)*size(I_texture,1))=1;
end
I_texture1=bwfill(I_texture,1,1);
I_texture2=~I_texture1(2:end-1,2:end-1);
I{1,i}=I_texture2;
end
Img=imadd_img2(I,texturesize);
function img_add=imadd_img2(I,texturesize)
% I是一个保存了所有待叠加图片的元胞数组
img_num=length(I);
img_add=zeros((texturesize));
for v = 1:img_num %逐一读取图像
Y_k = I{1,v};
q2=size(Y_k,3);
if q2>1
Y_k=rgb2gray(Y_k);
end
Y_k=double(Y_k);
img_add = img_add|Y_k ;
end
end
end
结果如下:
详细可前往这里。