How can I recognize shapes of simple geometrical objects using optimization tools
조회 수: 1 (최근 30일)
이전 댓글 표시
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
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.
답변 (1개)
Image Analyst
2019년 4월 21일
Attach an image. In the meantime, see my attached shape recognition demo.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Feature Detection and Extraction에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!