How to detect the shape in matlab?

159 ビュー (過去 30 日間)
saravanakumar D
saravanakumar D 2013 年 12 月 27 日
編集済み: DGM 2023 年 2 月 13 日
I can't understand the technique how to analyse the shape. So any please help me to understand this concept.
Code is below
function W = Classify(ImageRead)
RGB = imread('test.bmp');
figure,
imshow(RGB),
title('Original Image');
GRAY = rgb2gray(RGB);
figure,
imshow(GRAY),
title('Gray Image');
threshold = graythresh(GRAY);
BW = im2bw(GRAY, threshold);
figure,
imshow(BW),
title('Binary Image');
BW = ~ BW;
figure,
imshow(BW),
title('Inverted Binary Image');
[B,L] = bwboundaries(BW, 'noholes');
STATS = regionprops(L, 'all'); % we need 'BoundingBox' and 'Extent'
% Step 7: Classify Shapes according to properties
% Square = 3 = (1 + 2) = (X=Y + Extent = 1)
% Rectangular = 2 = (0 + 2) = (only Extent = 1)
% Circle = 1 = (1 + 0) = (X=Y , Extent < 1)
% UNKNOWN = 0
figure,
imshow(RGB),
title('Results');
hold on
for i = 1 : length(STATS)
W(i) = uint8(abs(STATS(i).BoundingBox(3)-STATS(i).BoundingBox(4)) < 0.1);
W(i) = W(i) + 2 * uint8((STATS(i).Extent - 1) == 0 );
centroid = STATS(i).Centroid;
switch W(i)
case 1
plot(centroid(1),centroid(2),'wO');
case 2
plot(centroid(1),centroid(2),'wX');
case 3
plot(centroid(1),centroid(2),'wS');
end
end
return

採用された回答

Image Analyst
Image Analyst 2013 年 12 月 27 日
What are the kinds of shapes you have there?
  1. polygons (everything is a polygon)
  2. quadrilaterals, polygons, and ellipsoids
  3. quadrilaterals, rectangles, polygons, and ellipsoids
  4. quadrilaterals, rectangles, polygons, circles, and ellipsoids
  5. quadrilaterals, rectangles, squares, polygons, circles, and ellipsoids
You might look at the solidity, area, and perimeter. And the circularity = perimeter.^2 ./ (4*pi*area).
You may also find this useful to determine how many sides a polygon has: http://matlab.wikia.com/wiki/FAQ#How_do_I_find_.22kinks.22_in_a_curve.3F
  7 件のコメント
José Hugo Soares de Jesus
José Hugo Soares de Jesus 2022 年 5 月 12 日
Good Afternoon, I want to to do the code by myself, I want just to know what´s the range of circularity of a triangle, pentagon and hexagon?
Image Analyst
Image Analyst 2022 年 5 月 12 日
Then run the demo I attached. It shows you the circularity of a variety of shapes and sizes. Because you're asking I assume you didn't run it or else you'd know the answer.

サインインしてコメントする。

その他の回答 (4 件)

Shawn Fernandes
Shawn Fernandes 2018 年 3 月 21 日
編集済み: DGM 2023 年 2 月 13 日
Hi All,
Bounding box gives the smallest possible rectangle / cuboid that fits the given shape, and would support n dimensions. [x_cordinate,y_cordinate,z_cordinate,....nth_cordinate,x_width,y_width,z_width.....nth_width] in this 2 D image, we have bounding box defined for each shapes as [x_cordinate,y_cordinate,x_width,y_width]
Extent gives the ratio of area of the bounding box to area of the region. For squares and rectangles, as the bounding box matches the shape, extent = 1. For circles and ellipses, the ratio of area of region to bounding box is always a constant = pi/4, [ (pi * a * b) / (2*x * 2 * y) is extent of circular region, for circle, a = b = x =y, for ellipse, a=x and b =y ]
So
(1)for Circles, we have x_width = y_width,extent = pi/4
(2)for squares, we have x_width = y_width,extent =1,
(3)for rectangles, we have x_width != y_width,extent =1
(4)For ellipse, we have we have x_width != y_width,extent = pi/4
Reference:-
the below code has been tested and it works
for i = 1 : length(STATS)
centroid = STATS(i).Centroid;
if((STATS(i).BoundingBox(3)~=STATS(i).BoundingBox(4)) && (STATS(i).Extent<1))
plot(centroid(1),centroid(2),'w+');
text(centroid(1),centroid(2),num2str(i),'Color','y');
end
if((STATS(i).BoundingBox(3)==STATS(i).BoundingBox(4)) && (STATS(i).Extent==1))
plot(centroid(1),centroid(2),'wS');
text(centroid(1),centroid(2),num2str(i),'Color','y');
end
if((STATS(i).BoundingBox(3)==STATS(i).BoundingBox(4)) && (STATS(i).Extent > 0.76 && STATS(i).Extent < .795))
plot(centroid(1),centroid(2),'wO');
text(centroid(1),centroid(2),num2str(i),'Color','y');
end
if((STATS(i).BoundingBox(3)~=STATS(i).BoundingBox(4)) && (STATS(i).Extent==1))
plot(centroid(1),centroid(2),'wX');
text(centroid(1),centroid(2),num2str(i),'Color','y');
end
if((STATS(i).BoundingBox(3)~=STATS(i).BoundingBox(4)) && (STATS(i).Extent > 0.76 && STATS(i).Extent < .795))
plot(centroid(1),centroid(2),'w*');
text(centroid(1),centroid(2),num2str(i),'Color','y');
end
end
Hope this helps..
  9 件のコメント
Joman
Joman 2022 年 12 月 30 日
how to show the result
complete begginer here
Image Analyst
Image Analyst 2022 年 12 月 30 日
@Joman depends on what you want the result to show. You could use a marker symbol and plot to put a marker at the centroid of the shape. Or you could use text to put the word for the shape at the centroid. Or you could extract each type of shape (by color or number of vertices) to its own separate image.

サインインしてコメントする。


sss
sss 2016 年 12 月 26 日
編集済み: Image Analyst 2016 年 12 月 26 日
what is the meaning of this for loop? -----
for i = 1 : length(STATS)
W(i) = uint8(abs(STATS(i).BoundingBox(3)-STATS(i).BoundingBox(4)) < 0.1);
W(i) = W(i) + 2 * uint8((STATS(i).Extent - 1) == 0 );
centroid = STATS(i).Centroid;
switch W(i)
case 1
plot(centroid(1),centroid(2),'wO');
case 2
plot(centroid(1),centroid(2),'wX');
case 3
plot(centroid(1),centroid(2),'wS');
end
  1 件のコメント
Image Analyst
Image Analyst 2016 年 12 月 26 日
It plots w0, wx, or xS at the centroid of blobs in a binary image. If the blob is roughly square it puts a wS at the centroid. If it's a rectangle it will put up wX. Otherwise it will put up w0 for arbitrarily-shaped blobs that fit in a bounding box that is roughly square. I don't see anything being put up for arbitrarily-shaped blobs that have a rectangular bounding box.

サインインしてコメントする。


Syukri Yazed
Syukri Yazed 2021 年 5 月 17 日
移動済み: Image Analyst 2022 年 12 月 30 日
Hi,
I've tested your code and improved it with the code that have been answered previously..
%https://ch.mathworks.com/matlabcentral/answers/110855-how-to-detect-the-shape-in-matlab
%https://ch.mathworks.com/matlabcentral/answers/245026-shape-detection-in-image
clc
%clear all
close all
%function W = Classify(ImageRead)
baseFileName = 'F:\PhD\MATLAB CODING\BlobsDemo\shape.png';
RGB = imread(baseFileName);
subplot(3, 3, 1);
imshow(RGB),
title('Original Image');
GRAY = rgb2gray(RGB);
subplot(3, 3, 2);
imshow(GRAY),
title('Gray Image');
threshold = graythresh(GRAY);
BW = imbinarize(GRAY, threshold);
subplot(3, 3, 3);
imshow(BW),
title('Binary Image');
BW = ~ BW;
subplot(3, 3, 4);
imshow(BW),
title('Inverted Binary Image');
[B,L] = bwboundaries(BW, 'noholes');
STATS = regionprops(L, 'all'); % we need 'BoundingBox' and 'Extent'
% Step 7: Classify Shapes according to properties
% Square = 3 = (1 + 2) = (X=Y + Extent = 1)
% Rectangular = 2 = (0 + 2) = (only Extent = 1)
% Circle = 1 = (1 + 0) = (X=Y , Extent < 1)
% UNKNOWN = 0
subplot(3, 3, 5);
imshow(RGB),
title('Results');
hold on
for i = 1 : length(STATS)
centroid = STATS(i).Centroid;
if((STATS(i).BoundingBox(3)~=STATS(i).BoundingBox(4)) && (STATS(i).Extent<1))
plot(centroid(1),centroid(2),'w+');
text(centroid(1),centroid(2),num2str(i),'Color','y');
end
if((STATS(i).BoundingBox(3)==STATS(i).BoundingBox(4)) && (STATS(i).Extent==1))
plot(centroid(1),centroid(2),'wS');
text(centroid(1),centroid(2),num2str(i),'Color','y');
end
if((STATS(i).BoundingBox(3)==STATS(i).BoundingBox(4)) && (STATS(i).Extent > 0.76 && STATS(i).Extent < .795))
plot(centroid(1),centroid(2),'wO');
text(centroid(1),centroid(2),num2str(i),'Color','y');
end
if((STATS(i).BoundingBox(3)~=STATS(i).BoundingBox(4)) && (STATS(i).Extent==1))
plot(centroid(1),centroid(2),'wX');
text(centroid(1),centroid(2),num2str(i),'Color','y');
end
if((STATS(i).BoundingBox(3)~=STATS(i).BoundingBox(4)) && (STATS(i).Extent > 0.76 && STATS(i).Extent < .795))
plot(centroid(1),centroid(2),'w*');
text(centroid(1),centroid(2),num2str(i),'Color','y');
end
end
%return
Could you please share with us your succesful code in detecting the shapes? Because I don't know the result numbering for.. what is wO, wX, w*, wS, w+?
or maybe Shawn Fernandes and ImageAnalyst can comment something regarding this matter.
  1 件のコメント
Image Analyst
Image Analyst 2021 年 5 月 18 日
移動済み: Image Analyst 2022 年 12 月 30 日
what is wO, wX, w*, wS, w+?
Those are plot colors and marker shapes. See the plot() function documentation.
  • wo = white circles
  • wx = white x's
  • w* = white stars
  • ws = white squares
  • w+ = white plus signs.

サインインしてコメントする。


Beenish Ishtiaq
Beenish Ishtiaq 2021 年 8 月 3 日
How to detect the shape using GUI
  1 件のコメント
Image Analyst
Image Analyst 2021 年 8 月 3 日
I'm attaching my shape recognition demos. Adapt as needed.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by