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

1 view (last 30 days)
TOKEN on 21 Apr 2019
Answered: Image Analyst on 21 Apr 2019
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
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

Walter Roberson on 21 Apr 2019
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 on 21 Apr 2019
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