用matlab识别形状的过程,如何使用optimization工具来识别几何图形的形状

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

附上识别圆的code,请高手解释这个code的步骤,以及怎么添加其他function去识别正三角形,长方形和正方形,感谢

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

% 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

%Square

% 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值