octave-bug-tracker
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Octave-bug-tracker] [bug #52933] [octave-forge] (image) regionprops Per


From: Avinoam Kalma
Subject: [Octave-bug-tracker] [bug #52933] [octave-forge] (image) regionprops Perimeter returns Matlab incompatible results
Date: Tue, 23 Jan 2018 00:31:13 -0500 (EST)
User-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36

Follow-up Comment #6, bug #52933 (project octave):

voilà:


function perim = rp_perimeter (cc)
  ## FIXME: this should be vectorized.  We were previously using
  ##        bwboundaries (incorrectly, see bug #52926) but
  ##        bwboundaries is doing a similar loop internally.
  CHAIN_CODE = [3, 2, 1, 4, -1, 0, 5, 6, 7];

  no = cc.NumObjects;
  boundaries = cell (no, 1);
  bw = false (cc.ImageSize);
  perim = zeros(no, 1);
  no
  for k = 1:no
    idx = cc.PixelIdxList{k};
    bw(idx) = true;
    boundaries{k} = __boundary__ (bw, 8);
    if (k != no)
      bw(idx) = false;
    endif
    np = size (boundaries{k}, 1);
    if (np == 2)
## single pixel - perimeter is 0      
      perim(k) = 0;
    else

## calculating perimeter according to Vossepoel and Smeulders,
## Computer Graphics and Image Processing 20(4):347-364, 1982.
## see: Cris Luengo, "Measuring boundary length" 
## http://www.crisluengo.net/index.php/archives/310

      # boundary of component k
      boundary = boundaries {k};
      # distance between consequtive pixels in the boundary
      dists = boundary (2:end,:) - boundary (1:end-1,:) + 2;
      # converting x_y distances to vector
      dists_vec = dists(:,2) + (dists(:,1)-1)*3;
      # converting distances to chain code
      chain_code = CHAIN_CODE (dists_vec);
      # odd numbers in the chain code - digonal movement
      odd =  sum(mod (chain_code, 2));
      # even entries in the chain code - vetical of digonal movement
      even = np - 1 - odd;
      # corners are places where the chain code changes value
      chain_code_change = chain_code - [chain_code(end),
chain_code(1:end-1)];
      corners = numel(find (chain_code_change != 0));
      # using Vossepoel and Smeulders formula
      perim(k) = even*0.980 + odd*1.406 - corners*0.091;
    endif
  endfor

#old code  
#  npx = cellfun ("size", boundaries, 1);
#  dists = diff (cell2mat (boundaries));
#  dists(cumsum (npx)(1:end-1),:) = [];
#  dists = sqrt (sumsq (dists, 2));
#  subs = repelems (1:no, [1:no; (npx-1)(:)']);
#  perim = accumarray (subs(:), dists(:), [no 1]);

endfunction




    _______________________________________________________

Reply to this item at:

  <http://savannah.gnu.org/bugs/?52933>

_______________________________________________
  Message sent via/by Savannah
  http://savannah.gnu.org/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]