0

I am trying to find and extract the boundary of a binary image in matlab, without using something like bwboundaries.

Is there a more manual way to do this, using loops maybe and changing the colour of the boundary pixels.

Any help would be appreciated.

user3461851
  • 45
  • 12

2 Answers2

0

Since it is binary so you can use two loop pairs.

-first ensure that image is binary.(just in case it is greyscale threshold it)

-use the first loop pair for height and one for width and trace any change i.e if a pixel is black and the next pixel is white draw that point onto a new mat.(i.e mark that point as 255 or of any colour you desire)

-Do the same for width and height and store it into another mat.

-Then add both the mats and average out the result.

This is the manual way and it may not be efficient.But it helps you modify the process to get you the exact edges.

(Source:I had used this technique for detecting perspective transformed rectangles containing bar code in java since canny edge used to give out too many edges due to bar code lines)

sonic_4vi
  • 31
  • 1
  • 5
  • I understand what you mean by using a loop pair to cycle through each pixel using width and height, but what is the second loop pair for. What exactly does adding the mats and averaging them do. – user3461851 Apr 30 '14 at 18:22
  • the first loop will detect only vertical edge pixels if u take a rectangle for example the horizontal pixels will not be detected. But if you switch width and height in the next pair u will be able to detect only the horizontal edge pixels. so finding them both and adding them will get u the exact rectangle.it will only thicken the edges of a rhombus figure so it works fine – sonic_4vi May 01 '14 at 04:53
  • Stack overflow does not allow me to post images or links due to low rep.otherwise would have put up the results. – sonic_4vi May 01 '14 at 05:08
-1

I don't understand what you mean by manual way. Is it mean pixel by pixel or anything else? Anyway try this example and fully explain your question that what you really want.

d1 = double(imread('cameraman.TIF'))./255;  %# Load the image, scale from 0 to 1
subplot(2,2,1); imshow(d1); title('d1');    %# Plot the original image
d = edge(d1,'canny',.6);                    %# Perform Canny edge detection
subplot(2,2,2); imshow(d); title('d');      %# Plot the edges
ds = bwareaopen(d,40);                      %# Remove small edge objects
subplot(2,2,3); imshow(ds); title('ds');    %# Plot the remaining edges
iout = d1;
BW = ds;
iout(:,:,1) = iout;                           %# Initialize red color plane
iout(:,:,2) = iout(:,:,1);                    %# Initialize green color plane
iout(:,:,3) = iout(:,:,1);                    %# Initialize blue color plane
iout(:,:,2) = min(iout(:,:,2) + BW, 1.0);     %# Add edges to green color plane
iout(:,:,3) = min(iout(:,:,3) + BW, 1.0);     %# Add edges to blue color plane
subplot(2,2,4); imshow(iout); title('iout');  %# Plot the resulting image

you can also track boundary by using Blob method but it depend on your requirements.

Gujjar
  • 367
  • 1
  • 5
  • 24