How can I detect a triangle in my image?

48 visualizaciones (últimos 30 días)
Damien Lahire Gomez-Martinho
Damien Lahire Gomez-Martinho el 14 de Dic. de 2021
Comentada: Image Analyst el 16 de Dic. de 2021
Please I need help for detect triangles in the image I attached. I don't know how to do it.

Respuestas (3)

Abolfazl Chaman Motlagh
Abolfazl Chaman Motlagh el 14 de Dic. de 2021
Editada: Abolfazl Chaman Motlagh el 15 de Dic. de 2021
Hi. in case your problem is exactly what you attached. here is an easy solution i can provide:
I = imread('image.jpeg'); %read image
I = rgb2gray(I); % transform image to grayscale
C = corner(I); % find corners in image
G = imgradient(I); % create gradient of image
BW = G>max(G(:))*0.1; % make a binary image by thresholding gradient image
CC = bwconncomp(BW); % extract connected components in binary image
ind = sub2ind(size(I),C(:,2),C(:,1)); % transform coordinate of corner points to linear index
for component=1:CC.NumObjects
% determine which corners belong to which components
Index_in_componet{component} = ismember(ind,CC.PixelIdxList{component});
% check if number of corner belong to a components is equal 3
Is_triangle(component) = sum(Index_in_componet{component}) == 3;
end
triangle = find(Is_triangle); % find the component with 3 corner
% find XY cordinate of Corners in Triangle
[Triangle_points_Y,Triangle_points_X] = ind2sub(size(I),ind(Index_in_componet{triangle}));
% find XY cordinate of All points in selected component
[Row,Col] = ind2sub(size(I),CC.PixelIdxList{triangle});
% Visualization
J = zeros(size(I));
for i=1:numel(Row)
J(Row(i),Col(i))=1;
end
figure;
subplot(1,2,1);
imshow(I)
hold;
scatter(Triangle_points_X,Triangle_points_Y,50,'b','filled')
subplot(1,2,2);
imshow(J);
The Output :
  3 comentarios
Abolfazl Chaman Motlagh
Abolfazl Chaman Motlagh el 15 de Dic. de 2021
yes of course because the codes after triangle,it is asumed that there is 1 triangle in image.
so this will do it :
I = imread('image2.jpeg'); %read image
I = rgb2gray(I); % transform image to grayscale
C = corner(I); % find corners in image
G = imgradient(I); % create gradient of image
BW = G>max(G(:))*0.1; % make a binary image by thresholding gradient image
CC = bwconncomp(BW); % extract connected components in binary image
ind = sub2ind(size(I),C(:,2),C(:,1)); % transform coordinate of corner points to linear index
for component=1:CC.NumObjects
% determine which corners belong to which components
Index_in_componet{component} = ismember(ind,CC.PixelIdxList{component});
% check if number of corner belong to a components is equal 3
Is_triangle(component) = sum(Index_in_componet{component}) == 3;
end
triangle = find(Is_triangle); % find the component with 3 corner
J = zeros(size(I));
figure;
subplot(1,2,1);
imshow(I)
hold;
for T = triangle
% find XY cordinate of Corners in Triangle
[Triangle_points_Y,Triangle_points_X] = ind2sub(size(I),ind(Index_in_componet{T}));
% find XY cordinate of All points in selected component
[Row,Col] = ind2sub(size(I),CC.PixelIdxList{T} );
% Visualization
for i=1:numel(Row)
J(Row(i),Col(i))=1;
end
scatter(Triangle_points_X,Triangle_points_Y,50,'b','filled')
end
subplot(1,2,2);
imshow(J);
Damien Lahire Gomez-Martinho
Damien Lahire Gomez-Martinho el 15 de Dic. de 2021
Thank you Abolfazl, so helpful info!

Iniciar sesión para comentar.


yanqi liu
yanqi liu el 15 de Dic. de 2021
yes,sir,may be use the Circularity,such as
clc; clear all; close all;
img = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/834120/image.jpeg');
bw = ~im2bw(img);
stats = regionprops(bw, 'BoundingBox', 'Circularity');
cis = cat(1, stats.Circularity);
id = cis<1 & cis>0.5;
figure; imshow(img, []);
hold on; rectangle('position', stats(id).BoundingBox, 'EdgeColor', 'r', 'LineWidth', 2)
  5 comentarios
Image Analyst
Image Analyst el 15 de Dic. de 2021
@yanqi liu, a fun trick that you might want to know about to convert a scalar field of a structure into a vector is to enclose it in brackets:
cis = [stats.Circularity];
I do it this way all the time instead of using cat(1,)
yanqi liu
yanqi liu el 16 de Dic. de 2021
@Image Analyst yes,it is great,thanks

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 15 de Dic. de 2021
Editada: Image Analyst el 15 de Dic. de 2021
Lots of ways to do it. Here is one:
rgbImage = imread('image.jpeg');
[rows, columns, numberOfColorChannels] = size(rgbImage)
subplot(2, 2, 1);
imshow(rgbImage);
impixelinfo
mask = rgb2gray(rgbImage) < 128;
subplot(2, 2, 2);
imshow(mask);
impixelinfo
[labeledImage, numRegions] = bwlabel(mask);
props = regionprops(mask, 'Area')
allAreas = [props.Area]
index = find(allAreas == 2635)
triangle = ismember(labeledImage, index);
subplot(2, 2, 3);
imshow(triangle);
  2 comentarios
Damien Lahire Gomez-Martinho
Damien Lahire Gomez-Martinho el 15 de Dic. de 2021
Thanks you Sir!
Image Analyst
Image Analyst el 16 de Dic. de 2021
Damien, since there are several ways that you can pick out triangles from the other objects, you need to specify what about the triangle blobs makes them unique. I mean, we could pick out triangles based on intensity, location, shape, size, etc. So you need to specify the range of all those things so that the right features are measured. You could check on all of those features, but you may not have to if you know certain things. You might need to specify only one, like I did when I specified the area. But I just as well could have done it by saying that the top-most blob in the image is the triangle if we know for a fact that the top blob will be a triangle. Or if the triangle is a unique intensity, like a gray level of 193 while all the other blobs have a different gray level, but could otherwise be located randomly, then we could just pick out blobs with a gray level of 193. If there could be any number of triangles, and they could be located anywhere, and they could be any brightness and size then you'd need a more sophisticated algorithm than one that simply looked at one particular feature.

Iniciar sesión para comentar.

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by