필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
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));

답변 (1개)

Pratham Shah
Pratham Shah 2023년 6월 2일
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
Surabhi A S
Surabhi A S 2023년 6월 5일
편집: Surabhi A S 2023년 6월 5일
Yes, it worked out now. But Iwant to calculate edge length of incomplete triangle edges by completing them.

댓글을 달려면 로그인하십시오.

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by