MATLAB 已知连通域边界坐标,输出二值图像

代码如下:

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

结果如下:


详细可前往这里

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值