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 日間)
古いコメントを表示
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/171944/image.jpeg)
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 件のコメント
採用された回答
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
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 件のコメント
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!