最近要做的一件事就是:對於一份給定的手掌圖,要定位可尋的最大內切圓。做了近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、結果思考
速度慢:智能算法沒進一步精確
圓半徑精度不足:采的迭代算法不夠細致,但過於細致會使速度下降