微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

提取对象的弗里曼链码 - Matlab

如何解决提取对象的弗里曼链码 - Matlab

我正在尝试使用此代码根据 https://www.crisluengo.net/archives/324 中的代码生成一个 freeman 链码,但它使用 DIPimage。因此,有人知道如何绕过 dip_array 函数吗?

代码

clc;
clear all;
 
Image = rgb2gray(imread('https://upload-icon.s3.us-east-2.amazonaws.com/uploads/icons/png/1606078271536061993-512.png'));
 
BW = imbinarize(Image);
BW = imfill(BW,'holes');
BW = bwareaopen(BW,100);
BW = padarray(BW,60,'both')
BW = imcomplement(BW);

imshow(BW)

[B,L] = bwboundaries(BW,'noholes');
         
%%%%%%%https://www.crisluengo.net/archives/324%%%%%
directions = [ 1,0
               1,-1
               0,-1
              -1,0
              -1,1
               0,1
               1,1];
           
indx = find(dip_array(img),1)-1;
           
sz = imsize(img);
start = [floor(indx/sz(2)),0];
start(2) = indx-(start(1)*sz(2));

cc = [];       % The chain code
coord = start; % Coordinates of the current pixel
dir = 1;       % The starting direction
while 1
   newcoord = coord + directions(dir+1,:);
   if all(newcoord>=0) && all(newcoord<sz) ...
         && img(newcoord(1),newcoord(2))
      cc = [cc,dir];
      coord = newcoord;
      dir = mod(dir+2,8);
   else
      dir = mod(dir-1,8);
   end
   if all(coord==start) && dir==1 % back to starting situation
      break;
   end
end

解决方法

我不想翻译整个代码,我现在没有时间,但我可以指点一下:

  • dip_array(img) 使用 dip_image 对象 img 内的像素值提取 MATLAB 数组。如果您在此处使用 BW 作为输入图像,您只需删除对 dip_array 的调用:indx = find(BW,1)-1

  • imsize(img) 返回图像 img 的大小。 MATLAB 函数 size 是等效的(在这种特殊情况下)。

  • dip_image 对象的维度与 MATLAB 数组的维度不同:它们的索引为 img(x,y),而 MATLAB 数组的索引为 BW(y,x)

  • dip_image 对象的索引从 0 开始,而不是像 MATLAB 数组那样从 1 开始。

最后两点改变了您计算 start 的方式。我想应该是这样的:

indx = find(BW,1);
sz = size(BW);
start = [1,floor((indx-1)/sz(2))+1];
start(1) = indx-((start(2)-1)*sz(1));

但使用 ind2sub 更容易(不知道为什么我在博客文章中做了显式计算):

indx = find(BW,1);
sz = size(BW);
start = ind2sub(sz,indx);

出于同样的原因,您可能还想交换 directions 的两列,并将 all(newcoord>=0) && all(newcoord<sz) 更改为 all(newcoord>0) && all(newcoord<=sz)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。