Create the generated curve differently (function "bwboundaries")

1 view (last 30 days)
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

Accepted Answer

Image Analyst
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
Alberto Acri
Alberto Acri on 17 Dec 2022
Edited: Alberto Acri on 17 Dec 2022
I tried extracting the coordinates (boundary) but I don't know why the figure comes out 90° counterclockwise oriented.
first = B{1,1};
second = B{2,1};
matrix = [first; second];
figure
plot(matrix(:,1), matrix(:,2), 'g.');
axis equal
xlim([0 512]);
ylim([0 512]);
I tried rotating the coordinates with this code but the two curves result slightly shifted to the right from the original image (boundary.png). Could you tell me why that is? I should have a correct overlap between the coordinates and the .png (as in the code you showed me in the previous post).
theta = 90*3;
R = [cosd(theta) -sind(theta); sind(theta) cosd(theta)];
c=mean(matrix,1);
Rmatrix = (matrix-c)*R'+c;
figure
plot(Rmatrix(:,1), Rmatrix(:,2), 'r.');
axis equal
xlim([0 512]);
ylim([0 512]);
Image Analyst
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing and Computer Vision in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!