How to get the coordinate of the boundaries of the entities in the attached image. I have written the code using bwboundaries but the output plot of boundaries is weird ?
2 views (last 30 days)
Show older comments
Sumit Chaudhary
on 7 Apr 2018
Commented: Image Analyst
on 8 Apr 2018
clc ;
clear;
close all;
cutoff = 32;
orginal_image = imread('boundaries.jpg');
gray_image = rgb2gray(orginal_image);
[x y] = size(gray_image );
dummy_image = zeros(x,y);
for p=1:x
for q = 1:y
if (gray_image(p,q) <= cutoff)
dummy_image(p, q) = cutoff;
end
end
end
dummy_image=imcomplement(dummy_image);
figure
set(gcf, 'Position', get(0, 'ScreenSize'));
subplot(1,2,1),
imshow(orginal_image)
subplot(1,2,2),
imshow(dummy_image)
boundaries = bwboundaries(dummy_image);
numberOfBoundaries = size(boundaries, 1);
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k};
figure
plot(thisBoundary(:,2), thisBoundary(:,1), 'g', 'LineWidth', 2);
end
hold off;
0 Comments
Accepted Answer
Walter Roberson
on 8 Apr 2018
orginal_image = imread('boundaries.jpg');
that gets you original_image of class uint8, range 0 to 255.
gray_image = rgb2gray(orginal_image);
gray_image is now uint8, range 11 to 162
dummy_image = zeros(x,y);
dummy_image is double (not uint8)
for p=1:x
for q = 1:y
if (gray_image(p,q) <= cutoff)
dummy_image(p, q) = cutoff;
end
end
end
The parts of gray_image that are less than the cutoff are set to 32, and everything above the cutoff is left as 0. dummy_image is now double with values either 0 (for the brighter places) or 32 (for the darker places)
dummy_image=imcomplement(dummy_image);
double images are expected to be in the range 0 to 1. imcomplement takes 1-value . So afterwards, dummy_image consists of doubles that are either +1 (for the brighter places) or -31 (for the darker places).
boundaries = bwboundaries(dummy_image);
bwboundaries looks for zeros. There are no zeros, only +1 and -31 . Everything is considered to be within the boundary.
Now, if you had instead used
dummy_image = zeros(x, y, 'uint8')
then dummy_image after the cutoff loop would still be 0 (for the brighter places) and 32 (for the darker places), but that would be uint8. imcomplement applied to uint8 is 255 - value, so after the imcomplement the values would be 255 (for the brighter places) and 223 (for the darker places), data type uint8. bwboundaries looks for zeros; there are no zeros, only 255 and 223. Everything would still be considered to be within the boundary.
If you had simply used
dummy_image = gray_image >= cutoff;
and no imcomplement, then
boundaries = bwboundaries(dummy_image);
would detect about 166 areas. Your could would then proceed to open 166 figures to plot them (since you have that figure inside the plotting loop.)
Some of the regions are pretty small. You may wish to consider using bwareafilt()
More Answers (1)
Image Analyst
on 8 Apr 2018
Like I said in your duplicate post, extract the green channel, threshold, then call bwboundaries() to get a list of (x,y) coordinates, or call bwperim() to get an image of the perimeters.
But your code works, even though the thresholding part could be done more directly like this:
dummy_image - gray_image <= cutoff;
So what is "weird" about the list of boundary coordinates you get from bwboundaries()?
2 Comments
Image Analyst
on 8 Apr 2018
Well you obviously have an object that is the entire image. So you chose the wrong threshold.
It looks like you have a working answer for Walter, so good luck to you. Write back if you need additional help.
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!