MATLAB:定位手掌最大内切圆
最近要做的一件事就是:对于一份给定的手掌图,要定位可寻的最大内切圆。做了近12个小时,才终于解决了这个问题,下面记录一下过程。
1、图片在MATLAB里是个矩阵,如何在矩阵作圆:
function [] = DrawCircle( Image, Centre, Radius )
figure, imshow(Image), hold on;
t = 0:0.01:2*pi;
x = round(Radius*cos(t) + Centre(1));
y = round(Radius*sin(t) + Centre(2));
%fill(x, y, 'w');
plot(x, y, '-r','LineWidth', 1);
plot(Centre(1), Centre(2), '*r');
end
2、如何实现内切圆心的定位:
% GA algorithm
optionsOrigin = gaoptimset('Generations', 50,...
'PopInitRange',[0;min(Width,Length)],...
'PopulationSize',ceil(min(Width,Length)/10));
[x, fval] = ga(@fitnessfcn, 3, optionsOrigin);
Centre = [x(1), x(2)];
Radius = fval;
str = sprintf('\nCentre = %f, Radius = %f', Centre, Radius);
disp(str);
3、如何实现内切圆圆半径的确定;
function [ Result ] = CalRadius( Centre, Radius)
global BinaryImage
r = floor(linspace(Radius, 0, ceil(log(Radius+1)+1)));
N = length(r);
t = 0:0.01:2*pi;
n = length(t);
for i = 1:1:N
disp(r(i));
x = floor(Centre(1) + r(i)*sin(t));
y = floor(Centre(2) + r(i)*cos(t));
BlackPointFound = false;
for j = 1:1:n
if BinaryImage(y(j), x(j))==0
BlackPointFound = true;
break;
else
continue;
end
end
if false == BlackPointFound
Result = r(i);
return;
end
end
Result=0;
end
4、运行结果检验
5、结果思考
速度慢:智能算法没进一步精确
圆半径精度不足:采的迭代算法不够细致,但过于细致会使速度下降