How can I detect a triangle in my image?

67 views (last 30 days)
Please I need help for detect triangles in the image I attached. I don't know how to do it.

Answers (3)

Abolfazl Chaman Motlagh
Abolfazl Chaman Motlagh on 14 Dec 2021
Edited: Abolfazl Chaman Motlagh on 15 Dec 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;
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)
The Output :

Sign in to comment.

yanqi liu
yanqi liu on 15 Dec 2021
yes,sir,may be use the Circularity,such as
clc; clear all; close all;
img = imread('');
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)

Sign in to comment.

Image Analyst
Image Analyst on 15 Dec 2021
Edited: Image Analyst on 15 Dec 2021
Lots of ways to do it. Here is one:
rgbImage = imread('image.jpeg');
[rows, columns, numberOfColorChannels] = size(rgbImage)
subplot(2, 2, 1);
mask = rgb2gray(rgbImage) < 128;
subplot(2, 2, 2);
[labeledImage, numRegions] = bwlabel(mask);
props = regionprops(mask, 'Area')
allAreas = [props.Area]
index = find(allAreas == 2635)
triangle = ismember(labeledImage, index);
subplot(2, 2, 3);
Image Analyst
Image Analyst on 16 Dec 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.

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!