I have a code to length of a shape but I am not getting the correct output accordingly.

1 visualización (últimos 30 días)
I have a code to find the edge length of triangle. But I am not getting the exact output and I wish to know if the length is in cm/m/mm.
I have attached the image and would like to find the edge length of triangle which the user selects. At the end the number of triangles in the image should be displayed. This is the code:
% Load the image containing multiple triangles
image = imread('triangles_image.png');
% Display the image
imshow(image);
title('Select a triangle by clicking on it');
% Prompt the user to select triangles and assign numbers
numTriangles = input('Enter the number of triangles in the image: ');
triangleVertices = cell(numTriangles, 1);
triangleLabels = cell(numTriangles, 1);
for i = 1:numTriangles
% Prompt the user to select the vertices of each triangle
fprintf('Triangle %d:\n', i);
[x, y] = ginput(3);
triangleVertices{i} = [x, y];
triangleLabels{i} = num2str(i);
% Display the selected vertices
hold on;
plot(x, y, 'ro', 'MarkerSize', 10);
text(x, y, triangleLabels{i});
end
% Prompt the user to select a triangle by its number
selectedTriangleNum = input('Enter the number of the triangle to calculate side lengths: ');
% Get the vertices of the selected triangle
selectedTriangleVertices = triangleVertices{selectedTriangleNum};
% Calculate the lengths of the sides
sideLengths = zeros(1, 3);
sideLengths(1) = sqrt((selectedTriangleVertices(2,1) - selectedTriangleVertices(1,1))^2 + ...
(selectedTriangleVertices(2,2) - selectedTriangleVertices(1,2))^2);
sideLengths(2) = sqrt((selectedTriangleVertices(3,1) - selectedTriangleVertices(2,1))^2 + ...
(selectedTriangleVertices(3,2) - selectedTriangleVertices(2,2))^2);
sideLengths(3) = sqrt((selectedTriangleVertices(1,1) - selectedTriangleVertices(3,1))^2 + ...
(selectedTriangleVertices(1,2) - selectedTriangleVertices(3,2))^2);
% Display the lengths of the sides
fprintf('Side lengths of Triangle %d:\n', selectedTriangleNum);
fprintf('Side 1: %.2f\n', sideLengths(1));
fprintf('Side 2: %.2f\n', sideLengths(2));
fprintf('Side 3: %.2f\n', sideLengths(3));

Respuestas (1)

Pratham Shah
Pratham Shah el 2 de Jun. de 2023
Hello Surabhi!
Your code is working properly and giving correct output for attached image. As far as unit is concerned, that depends on the resolution of the camera. i.e. How much distance one pixel covers. Becuase here you are calculating length based on the pixel co-ordinates of vertices.
  1 comentario
Surabhi A S
Surabhi A S el 5 de Jun. de 2023
Editada: Surabhi A S el 5 de Jun. de 2023
Yes, it worked out now. But Iwant to calculate edge length of incomplete triangle edges by completing them.

Iniciar sesión para comentar.

Categorías

Más información sobre Image Segmentation and Analysis en Help Center y File Exchange.

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by