0

I am trying this code to generate a freeman chain code based on the code in https://www.crisluengo.net/archives/324 but it uses the DIPimage. Therefore, does someone has an idea how to by pass the dip_array function?

Code:

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
Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
alirazi
  • 159
  • 8
  • DIPimage is freely available. You could use it. – Cris Luengo Jan 16 '21 at 19:09
  • True. I can not find the function in the installation and as "To convert a dip_image object back to a MATLAB array use the function dip_array" what does the the dip_array does? can you please add it to the script above? – alirazi Jan 16 '21 at 21:06
  • The `dip_array` function is a method of the `dip_image` class ([source](https://github.com/DIPlib/diplib/blob/c4c7482fab3d52ed3e85bf31d976635160fe320c/dipimage/%40dip_image/dip_image.m#L900)). I posted some hints in the answer below that will hopefully help you translate the code. – Cris Luengo Jan 16 '21 at 22:17

1 Answers1

1

I don't want to translate the whole code, I don't have time right now, but I can give a few pointers:

  • dip_array(img) extracts the MATLAB array with the pixel values that is inside the dip_image object img. If you use BW as input image here, you can simply remove the call to dip_array: indx = find(BW,1)-1.

  • imsize(img) returns the sizes of the image img. The MATLAB function size is equivalent (in this particular case).

  • Dimensions for the dip_image object are different from those for MATLAB arrays: they are indexed as img(x,y), whereas MATLAB arrays are indexed as BW(y,x).

  • Indices for dip_image objects start at 0, not at 1 as MATLAB arrays do.

These last two points change how you'd compute start. I think it'd be something like this:

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

But it's easier to use ind2sub (not sure why I did the explicit calculation in the blog post):

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

You also probably want to swap the two columns of directions for the same reason, and change all(newcoord>=0) && all(newcoord<sz) into all(newcoord>0) && all(newcoord<=sz).

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120