边界提取小程序-MATLAB

本文介绍了一种使用MATLAB进行边界检测的方法。通过构建稀疏矩阵并应用特定算法来定位非零元素的边界,这些边界周围必须包含零元素。文中详细展示了MATLAB代码,并通过实例演示了如何从稀疏矩阵中找出边界元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 原理
    实现原理很简单,我给了一个系数矩阵,在系数矩阵中找到非零元素的内外边界。
    满足本身值非零,边界必须有零才能作为边界。
    用MATLAB模拟了一下效果。

  • 代码

clc
clear

% %区域填充颜色
% x=[2 1 2 2 0 2 2];
% x2=[2 4 2 2 3 2 2];
% y=[3 2 1 4 2 0 3];
% y2=[4 2 0 1 2 3 4];
% figure
% fill(x,y,'r',x2,y2,'r')
% 
% % pt_num=200;
% % x=unidrnd(10,1,pt_num);
% % y=unidrnd(10,1,pt_num);

matA=sparse(100,100);
matA(13,64:65)=1;
matA(14,63:66)=1;
matA(15,61:68)=1;
matA(16,59:69)=1;
matA(17,58:71)=1;
matA(18,56:72)=1;
matA(19,53:73)=1;
matA(20,51:76)=1;
matA(21,48:79)=1;
matA(22,47:80)=1;
matA(23,45:82)=1;
matA(24,42:83)=1;
matA(25,40:85)=1;
matA(26,40:85)=1;
matA(27,39:85)=1;

matA(28,39:45)=1;
matA(28,73:86)=1;

matA(29,40:46)=1;
matA(29,71:87)=1;

matA(30,41:47)=1;
matA(30,69:86)=1;

matA(31,40:47)=1;
matA(31,68:86)=1;

matA(32,41:49)=1;
matA(32,65:84)=1;

matA(33,43:50)=1;
matA(33,64:85)=1;

matA(34,42:52)=1;
matA(34,63:83)=1;

matA(35,43:53)=1;
matA(35,61:83)=1;

matA(36,43:54)=1;
matA(36,60:82)=1;

matA(37,45:57)=1;
matA(37,59:79)=1;

matA(38,46:79)=1;
matA(39,45:78)=1;
matA(40,44:77)=1;
matA(41,43:76)=1;
matA(42,45:73)=1;
matA(43,43:74)=1;
matA(44,43:72)=1;
matA(45,42:72)=1;
matA(46,40:71)=1;
matA(47,38:68)=1;
matA(48,37:65)=1;
matA(49,42:63)=1;
matA(50,44:61)=1;
matA(51,45:60)=1;
matA(52,47:58)=1;
matA(53,47:58)=1;
matA(54,49:57)=1;
matA(55,52:57)=1;
matA(56,53:56)=1;
matA(57,55:56)=1;

matA(73,18:56)=1;
matA(74,19:53)=1;
matA(75,20:53)=1;
matA(76,22:51)=1;
matA(77,23:50)=1;
matA(78,25:48)=1;
matA(79,26:46)=1;
matA(80,27:47)=1;
matA(81,27:45)=1;
matA(82,29:44)=1;
matA(83,30:43)=1;
matA(84,32:42)=1;
matA(85,33:41)=1;
matA(86,35:41)=1;
matA(87,36:39)=1;
matA(88,36:38)=1;
matA(89,38)=1;
figure(1)
spy(matA)

[m,n]=size(matA);
matB=sparse(100,100);
% %次要选择
% for ia=2:m-1
%     for ib=2:n-1
%         if(matA(ia,ib)~=0)
%             if(sum(matA(ia-1:ia,ib-1))==0)
%                 matB(ia,ib)=1;
%             elseif(sum(matA(ia-1,ib-1:ib))==0)
%                 matB(ia,ib)=1;
%             elseif((sum(matA(ia:ia+1,ib-1))==0))
%                 matB(ia,ib)=1;
%             elseif(sum(matA(ia+1,ib-1:ib))==0)
%                 matB(ia,ib)=1;
%             elseif(sum(matA(ia-1,ib:ib+1))==0)
%                 matB(ia,ib)=1;
%             elseif(sum(matA(ia-1:ia,ib+1))==0)
%                 matB(ia,ib)=1;
%             elseif(sum(matA(ia+1,ib:ib+1))==0)
%                 matB(ia,ib)=1;
%             elseif(sum(matA(ia:ia+1,ib+1))==0)
%                 matB(ia,ib)=1;
%             end
%         end
%     end
% end

%主要选择
cellInd=[];
for ia=2:m-1
    for ib=2:n-1     
        if(matA(ia,ib)~=0)
            if(matA(ia+1,ib)==0)
                matB(ia,ib)=1;
                cellInd=[cellInd ; ia ib];
            elseif(matA(ia-1,ib)==0)
                matB(ia,ib)=1;
                cellInd=[cellInd ; ia ib];
            elseif(matA(ia,ib+1)==0)
                matB(ia,ib)=1;
                cellInd=[cellInd ; ia ib];
            elseif(matA(ia,ib-1)==0)
                matB(ia,ib)=1;
                cellInd=[cellInd ; ia ib];
            end
        end
    end
end

%第一行和最后一行
for ic=1:n
    if(matA(1,ic)==1)
        matB(1,ic)=1;
        cellInd=[cellInd ;1 ic];
    elseif(matA(m,ic)==1)
        matB(m,ic)=1;
        cellInd=[cellInd ;m ic];
    end
end
%第一列和最后一列
for id=1:m
    if(matA(id,1)==1)
        matB(id,1)=1;
        cellInd=[cellInd ;id 1];
    elseif(matA(id,n)==1)
        matB(id,n)=1;
        cellInd=[cellInd ;id n];
    end
end
figure(2)
spy(matB)

[newPoints,newIndex]=sortrows(cellInd,1);
minRow=min(newPoints(:,1));
maxRow=max(newPoints(:,1));

contourOne=[];
% for ie=minRow:maxRow
%     line=sortrows(newPoints(newPoints(:,1)==ie,:),2);
%     deffeV=line(2:end,:)-line(1:end-1,:);
%     gap=find(deffeV(:,2)~=1);
%     if fix(length(gap))+1==1
%         
%     end
% end
  • 结果图
    findcontour
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sophia_xw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值