How can I segment and label edges in a binary matrix?

조회 수: 5 (최근 30일)
Neuroesc
Neuroesc 2024년 9월 20일
답변: Aastha 2024년 9월 23일
I have a binary matrix that contains a polygon with different edges and vertices. Edges are false, non-edges are true. I would like to detect the edges of the polygon (i.e. lines with different orientations) like this:
The aim would be to get an output Matrix, the same size as my input one, where the edge values are labelled according to which edge they correspond to.
I have looked through Matlab's segmentation and edge detection functions but can't seem to find anything that does this.
Thanks for any help.

채택된 답변

Aastha
Aastha 2024년 9월 23일
I see that you want to mark and label all the edges in a polygon with different orientations and lengths.
You may refer to the steps mentioned below to do so:
1. Convert the input image to grayscale using the rgb2gray function. Next, use the grayscale image as input for an edge detector to prepare for the line detection process. This can be achieved using the edge function with the "canny" method, as illustrated in the code snippet below:
% Load and convert image to grayscale
I = imread('image.png');
I = rgb2gray(I);
imshow(I);
% Edge detection using Canny method
BW = edge(I, 'canny');
For more information on “rgb2gray” and “edge” function, you may refer to links of MathWorks documentation mentioned below:
2. Next, you can apply the Hough transform to detect edges of various orientations in the image using the “houghpeaks” function to identify possible lines. The output contains the orientations and locations of the detected lines. The code snippets illustrates this:
% Apply Hough Transform
[H, T, R] = hough(BW);
figure
imshow(H, [], 'XData', T, 'YData', R, 'InitialMagnification', 'fit');
xlabel('\theta'), ylabel('\rho');
axis on, axis normal, hold on;
% Detect Hough peaks (lines)
P = houghpeaks(H, 8, 'threshold', ceil(0.5 * max(H(:))));
x = T(P(:,2)); y = R(P(:,1));
plot(x, y, 's', 'color', 'white');
For any further information on “hough” and “houghpeaks” functions, refer to the related documentation links mentioned below:
3. Finally, you can use the “houghlines” function to extract lines from the detected peaks. Overlay the detected lines on the original image and visualize them. For each detected line, mark the start and end points with yellow and red crosses, respectively:
% Extract and plot the lines
lines = houghlines(BW, T, R, P, 'FillGap', 5, 'MinLength', 1);
figure, imshow(I), hold on
max_len = 0;
% Iterate through each detected line
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1), xy(:,2), 'LineWidth', 3);
% Plot the endpoints of the lines
plot(xy(1,1), xy(1,2), 'x', 'LineWidth', 2, 'Color', 'yellow');
plot(xy(2,1), xy(2,2), 'x', 'LineWidth', 2, 'Color', 'red');
% Track the longest line
len = norm(lines(k).point1 - lines(k).point2);
if len > max_len
max_len = len;
xy_long = xy;
end
end
On following the above-mentioned steps, I got the output image as attached below:
I hope this helps!

추가 답변 (0개)

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by