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 次查看(过去 30 天)
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;

采纳的回答

Walter Roberson
Walter Roberson 2018-4-8
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()

更多回答(1 个)

Image Analyst
Image Analyst 2018-4-8
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 个评论
Sumit Chaudhary
Sumit Chaudhary 2018-4-8
This the output when we use : plot(thisBoundary(:,2), thisBoundary(:,1), 'g', 'LineWidth', 2); My output should contains co-ordinates of all the boundaries points of the entities present and same should be visually obtained by plotting . Please elucidate @Image Analyst, Why this is happening ? This thing was weird to me.
Image Analyst
Image Analyst 2018-4-8
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.

请先登录,再进行评论。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by