Effacer les filtres
Effacer les filtres

I want to find the edge lengths of the shape in a given image.

7 vues (au cours des 30 derniers jours)
Surabhi A S
Surabhi A S le 11 Juin 2023
Commenté : Surabhi A S le 12 Juin 2023
I have an edged image and would like to highlight the edges of triangles and also find the length of each side of each triangle. Display the highlighted triangles and the length of each side of triangle individually.
The edges of the triangles are not straight but have few uneven (curvy) lines as well. I want to highlight all of them making the edges into a straight line. Also as there are different shapes other than triangles, I have to first detect it as a triangle (maybe wrt to angle between two lines) and then find the length of each side of triangle.

Réponses (1)

Image Analyst
Image Analyst le 11 Juin 2023
help houghlines
HOUGHLINES Extract line segments based on Hough transform. LINES = HOUGHLINES(BW, THETA, RHO, PEAKS) extracts line segments in the image BW associated with particular bins in a Hough transform. THETA and RHO are vectors returned by function HOUGH. Matrix PEAKS, which is returned by function HOUGHPEAKS, contains the row and column coordinates of the Hough transform bins to use in searching for line segments. HOUGHLINES returns LINES structure array whose length equals the number of merged line segments found. Each element of the structure array has these fields: point1 End-point of the line segment; two-element vector point2 End-point of the line segment; two-element vector theta Angle (in degrees) of the Hough transform bin rho Rho-axis position of the Hough transform bin The end-point vectors contain [X, Y] coordinates. LINES = HOUGHLINES(...,PARAM1,VAL1,PARAM2,VAL2) sets various parameters. Parameter names can be abbreviated, and case does not matter. Each string parameter is followed by a value as indicated below: 'FillGap' Positive real scalar. When HOUGHLINES finds two line segments associated with the same Hough transform bin that are separated by less than 'FillGap' distance, HOUGHLINES merges them into a single line segment. Default: 20 'MinLength' Positive real scalar. Merged line segments shorter than 'MinLength' are discarded. Default: 40 Class Support ------------- BW can be logical or numeric and it must be real, 2-D, and nonsparse. References ---------- Rafael C. Gonzalez, Richard E. Woods, Steven L. Eddins, "Digital Image Processing Using MATLAB", Prentice Hall, 2003 Example ------- % Search for line segments corresponding to five peaks in the Hough % transform of the rotated circuit.tif image. Additionally, highlight % the longest segment. I = imread('circuit.tif'); rotI = imrotate(I,33,'crop'); BW = edge(rotI,'canny'); [H,T,R] = hough(BW); imshow(H,[],'XData',T,'YData',R,'InitialMagnification','fit'); xlabel('\theta'), ylabel('\rho'); axis on, axis normal, hold on; P = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:)))); x = T(P(:,2)); y = R(P(:,1)); plot(x,y,'s','color','white'); % Find lines and plot them lines = houghlines(BW,T,R,P,'FillGap',5,'MinLength',7); figure, imshow(rotI), hold on max_len = 0; for k = 1:length(lines) xy = [lines(k).point1; lines(k).point2]; plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green'); % plot beginnings and ends of 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'); % determine the endpoints of the longest line segment len = norm(lines(k).point1 - lines(k).point2); if ( len > max_len) max_len = len; xy_long = xy; end end % highlight the longest line segment plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','cyan'); See also HOUGH, HOUGHPEAKS. Documentation for houghlines doc houghlines
  11 commentaires
Image Analyst
Image Analyst le 12 Juin 2023
% Demo to show how drawline can be used to draw lines and measure distances on the image.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
%===============================================================================
% Display demo image in a maximized window.
rgbImage = imread('peppers.png');
hFig1 = figure;
imshow(rgbImage);
hFig1.WindowState = 'maximized';
hFig1.NumberTitle = "off";
hFig1.Name = 'Demo by Image Analyst';
%===============================================================================
% Ask user to draw line over the image.
uiwait(helpdlg('Draw a line. Click mouse button down, drag, then release mouse button.'));
button = 'Yes';
lineCount = 0;
xy = zeros(1, 4);
while contains(button, 'Yes', 'IgnoreCase',true)
% User draws a line. It exits as soon as they lift the mouse.
hLine = drawline();
% Get the coordinates in the form [xLeft, yTop, width, height].
roiPosition = hLine.Position; % Column 1 is the x values, column 2 is y.
lineCount = lineCount + 1;
% Save this coordinate. Format is [x1, x2, y1, y2]
xy(lineCount, :) = roiPosition(:)';
% Delete the ROI object.
delete(hLine);
% and replace it with a colored line in the graphical overlay.
hold on;
plot(xy(lineCount, 1:2), xy(lineCount, 3:4), '.-', 'LineWidth', 2, 'MarkerSize', 30);
caption = sprintf('You have drawn %d lines so far.', lineCount);
title(caption, 'FontSize', fontSize);
% Ask user if they want to click another.
message = sprintf('%d lines drawn so far.\nDraw another line?', lineCount);
button = questdlg(message, message, 'Yes', 'No -- Quit', 'Yes');
if contains(button, 'Quit','IgnoreCase',true)
break;
end
end
hold off;
% Get the lengths of the lines.
deltaX = xy(:, 2) - xy(:, 1);
deltaY = xy(:, 4) - xy(:, 3);
lineLengths = sqrt(deltaX .^ 2 + deltaY .^ 2);
meanLength = mean(lineLengths); % Get mean length of all the lines.
caption = sprintf('You drew %d lines.\nMean Line Length = %.1f pixels.', lineCount, meanLength);
fprintf('%s\n', caption)
title(caption, 'FontSize', fontSize);
% Show coordinates in the command window.
xy
%===============================================================================
% Show line lengths as a bar chart.
hFig2 = figure;
hFig2.NumberTitle = "off";
hFig2.Name = 'Demo by Image Analyst';
subplot(1, 2, 1);
bar(lineLengths);
yline(meanLength, 'Color', 'r', 'LineWidth', 2);
grid on;
xlabel('Line Number', 'FontSize', fontSize);
ylabel('Line Length in Pixels', 'FontSize', fontSize);
caption = sprintf('Lengths of the %d Lines You Drew', lineCount);
title(caption, 'FontSize', fontSize);
% Show histogram of the line lengths.
subplot(1, 2, 2);
histogram(lineLengths); % Take histogram and automatically plot bar chart of line length distribution.
xline(meanLength, 'Color', 'r', 'LineWidth', 2);
grid on;
xlabel('Line Length in Pixels', 'FontSize', fontSize);
ylabel('Count', 'FontSize', fontSize);
caption = sprintf('Mean Line Length = %.1f pixels.', meanLength);
title(caption, 'FontSize', fontSize);
hFig2.WindowState = 'maximized';
Surabhi A S
Surabhi A S le 12 Juin 2023
Thanks for the demo. Is there any way in which the length would be automatically displayed, without the user drawing the line. As i ahev amny samples I can't keep on drawing the line to find the length. So I would like the code itself detect the length automatically, I think I have got half of the code done which I have mentioned in earlier comment.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Computer Vision with Simulink dans Help Center et File Exchange

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by