Link to home
Start Free TrialLog in
Avatar of unknown_
unknown_

asked on

bwtraceboundary

Hello,

Can someone explain me what the following code does ?

Thanks in advance !
BW = imread('blobs.png');
      imshow(BW,[]);
      s=size(BW);
      for row = 2:55:s(1)
        for col=1:s(2)
          if BW(row,col), 
            break;
          end
        end
 
     contour = bwtraceboundary(BW, [row, col], 'W');

Open in new window

Avatar of yuk99
yuk99
Flag of United States of America image

This code is extract from Matlab's help of bwtraceboundary function.
http://www.mathworks.com/access/helpdesk/help/toolbox/images/bwtraceboundary.html
You show here only part of the code. The full code is below. What the code does is there as well:

Read in and display a binary image. Starting from the top left, project a beam across the image searching for the first nonzero pixel. Use the location of that pixel as the starting point for the boundary tracing. Including the starting point, extract 50 pixels of the boundary and overlay them on the image. Mark the starting points with a green x. Mark beams that missed their targets with a red x.

Do you need line-by-line description? Which lines you don't understand exactly?
BW = imread('blobs.png');
imshow(BW,[]);
s=size(BW);
for row = 2:55:s(1)
   for col=1:s(2)
      if BW(row,col),
         break;
      end
   end
 
   contour = bwtraceboundary(BW, [row, col], 'W', 8, 50,...
                                   'counterclockwise');
   if(~isempty(contour))
      hold on;
      plot(contour(:,2),contour(:,1),'g','LineWidth',2);
      hold on;
      plot(col, row,'gx','LineWidth',2);
   else
      hold on; plot(col, row,'rx','LineWidth',2);
   end
end

Open in new window

Avatar of masheik
Avatar of unknown_
unknown_

ASKER

can someone explain what's the purpose of that for loop in the code ?

for row = 2:55:s(1)
   for col=1:s(2)
      if BW(row,col),
         break;
      end
   end
ASKER CERTIFIED SOLUTION
Avatar of yuk99
yuk99
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial