Solved

# bwtraceboundary

Posted on 2009-04-19
1,741 Views
Hello,

Can someone explain me what the following code does ?

``````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');
``````
0
Question by:unknown_

LVL 15

Expert Comment

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
``````
0

LVL 9

Expert Comment

0

Author Comment

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
0

LVL 15

Accepted Solution

The first FOR loop goes through rows starting at row 2, then 2+55=57 row, 112, etc.
The second FOR loop scans all pixels in the selected row and stops at the first one, which is not 0 (not black).
Then it doing bwtraceboundary starting at selecting point (with coordinates row and col), initial direction West, 8-connected neighborhood connectivity, up to 50 boundary pixels, search in counterclockwise direction.
If it finds something the script then plots initial point and the contour in green, if not - the initial point in red.
That's all.
0

## Featured Post

### Suggested Solutions

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.