How can I recognize shapes of simple geometrical objects using optimization tools

1 回表示 (過去 30 日間)
TOKEN
TOKEN 2019 年 4 月 21 日
回答済み: Image Analyst 2019 年 4 月 21 日
Here is the sample code to recognize a circle, looks like I need to create different functions to define a square, rectangle and a triangle first if I want recognize them. Can someone help me with it? Thanks.
clc
clear
M0=zeros(1024,1024); % Background of zeros
x=1:1024;
y=1:1024;
[X,Y]=meshgrid(x,y); % grid of pixel positions
for in=11
A=double(imread([num2str(in,'%4.3d'),'.jpg']))/255; % Load image and scale to 1
M=@(p) M0+double((X-p(1)).^2+(Y-p(2)).^2<(p(3))^2); % define your teplate
% You may need a separate funtion file
fun=@(p) sum(sum(abs(A-M(p)).^2)); % The objective funtion to minimize
% find the avergae center position and ditribution of the nonzero
% pixels in image
pos=find(A>0);
x0=mean(X(pos));
y0=mean(Y(pos));
d=(range(X(pos))+range(Y(pos)))/2;
trl=[x0 y0 d/2]; % The trial solution % number of parameters depend on the shape
lb=[0 0 0];% lower bounds of the prameters
ub=[1024 1024 512];% upper bounds of the prameters
% Optimization process
%You dont have to change the below lines
opts=optimoptions('ga');
opts.InitialPopulationMatrix=trl;
opts.Display='iter';
[sol,fval]=ga(fun,length(trl),[],[],[],[],lb,ub,[],opts);
if fval<100
'circle'
else
'unknown'
end
figure(1)
clf
surf(M(sol),'linestyle','none')
view(2)
daspect([1 1 1])
figure(2)
clf
surf(A,'linestyle','none')
view(2)
daspect([1 1 1])
end
  2 件のコメント
Walter Roberson
Walter Roberson 2019 年 4 月 21 日
Are the objects to be recognized the only things in the image? Are they outlines or filled ? Are they dark on light or light on dark or bright color or dark color ? I take it that part of the challenge is that they might be rotated.
TOKEN
TOKEN 2019 年 4 月 21 日
They are the only things in the image, they are white in the image with the black background. Yes, the rotation should be easy. I just dont know how to define a rectangle for now

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

回答 (1 件)

Image Analyst
Image Analyst 2019 年 4 月 21 日
Attach an image. In the meantime, see my attached shape recognition demo.

カテゴリ

Help Center および File ExchangeFeature Detection and Extraction についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by