【问题标题】:Extract Freeman chain code of an object - Matlab提取对象的弗里曼链码 - Matlab
【发布时间】:2021-04-21 10:17:36
【问题描述】:

我正在尝试使用此代码根据https://www.crisluengo.net/archives/324 中的代码生成弗里曼链码,但它使用的是 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,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,-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

【问题讨论】:

  • DIPimage 是免费的。你可以使用它。
  • 是的。我在安装中找不到该函数,并且由于“要将 dip_image 对象转换回 MATLAB 数组,请使用函数 dip_array”,dip_array 有什么作用?你能把它添加到上面的脚本中吗?
  • dip_array 函数是 dip_image 类 (source) 的方法。我在下面的答案中发布了一些提示,希望能帮助您翻译代码。

标签: matlab image-processing contour chaincode


【解决方案1】:

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

  • 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&gt;=0) &amp;&amp; all(newcoord&lt;sz) 更改为all(newcoord&gt;0) &amp;&amp; all(newcoord&lt;=sz)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-19
    • 2014-02-22
    相关资源
    最近更新 更多