Create the generated curve differently (function "bwboundaries")
1 view (last 30 days)
Show older comments
Alberto Acri
on 14 Dec 2022
Commented: Image Analyst
on 17 Dec 2022
Hi. I would like to be able to improve the following code so that:
1) the background always remains white and the curves always black (such as the .png figure);
2) the red curve that creates the code I would like it to become red but inside the black curve (see image obtained from the code)
3) I want to save all the coordinates ("boundary") that the code creates.
Below is the code:
baseFileName = 'image.png';
fullFileName = fullfile(pwd, baseFileName);
imag = imread(fullFileName);
figure();
imshow(imag);
%
imag_int8 = int8(imag);
BW = imbinarize(imag_int8);
%
[B,L] = bwboundaries(BW,'noholes');
figure
imshow(label2rgb(L, @jet, [0 0 0]))
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'r', 'LineWidth', 5)
end
0 Comments
Accepted Answer
Image Analyst
on 16 Dec 2022
This works fine:
baseFileName = 'boundary.png';
fullFileName = fullfile(pwd, baseFileName);
binaryImage = imread(fullFileName); % It's already a logical binary image.
% Invert it so the black background is now the foreground.
binaryImage = ~binaryImage;
imshow(binaryImage); % Display it.
% Find boundaries.
[B,L] = bwboundaries(binaryImage,'noholes');
% Plot boundaries over image.
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'r.-', 'LineWidth', 1, 'MarkerSize', 20)
end
Note, zoomed in, the pixels are like little squares and the coordinate is at the center of the pixel (center of the square block).
2 Comments
Image Analyst
on 17 Dec 2022
Not sure why you're ignoring my solution. Did you think it was no good so you decided to do it your own (wrong) way?
You're making the common beginner mistake of mixing up (x,y) with (row, column) (in addition to the mistake of ignoring my advice). bwboundaries returns (row, column) -- in other words (y, x) -- while plot needs (x, y).
Also, your "B" needs only 1 index, not 2, since there is just one cell for each boundary. There is no need for a second dimension even though what's inside the cell is a 2-D array.
Go back to the way I taught you and you'll be fine.
More Answers (0)
See Also
Categories
Find more on Image Processing and Computer Vision in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!