2012-01-22 42 views
2

我有两张图片,需要通过翻译和旋转将一张图片转换为另一张图片。为此我有一个这样的功能:如何调整fminunc的初始步骤?

function [differences] = cost_function(transformation) 

disp(transformation); 

offset_x = transformation(1); % one of the images will be shifted by these many 
offset_y = transformation(2); % pixels in either direction 
angle = transformation(3); % and rotated by this much 

% snip: 
% * transform the second image 
% * otsu treshold both 
% * xor the results 
% * count pixels that xored 

然后我试着找到它的最小值!

best_transform = fminunc(@cost_function, [0 0 0]); 

然而,解算器日志显示一个很大的问题:

1.0e-007 * 
    0.1490   0   0 
    1.0e-007 * 
     0 0.1490   0 
    1.0e-007 * 
     0   0 0.1490 
     0   0   1 
    0.0000   0 1.0000 
     0 0.0000 1.0000 
     0   0 1.0000 
     0   0 0.3333 
    0.0000   0 0.3333 

求解程序试图在每个维度有一点点微调,找到最佳线路进行的,但很明显偏移图像通过0.1490像素确实没有太大的作用,而将其自然移动0.1490弧度。然而,我不知道,其中0.1490实际上来自哪里。

The documentation在这里似乎没有建议。我如何增加求解器的初始步骤?

回答

0

我现在有这样的杂牌组装电脑:

best_score_so_far = 9999999999; 
best_so_far = [0 0 0]; 
optimset('display', 'off', 'MaxIter', 100); 
for dist = 1:1:10 
    for angle = 0:2*pi/4/dist:2*pi 
     x = cos(angle)*dist; 
     y = sin(angle)*dist; 
     disp([dist angle x y]); %heh 
     [best, cost] = fminunc(@(angle)cost_function([x,y,angle]), 0); 
     if(cost < best_score_so_far) 
      best_score_so_far = cost; 
      best_so_far = best; 
     end 
    end 
end 

...但它的丑陋,超慢,而且,还有,一个杂牌。

2

fminunc设计用于查找最小连续函数,正如您所指出的那样,由于您将图像的像素数量改为无穷小,因此不会产生任何结果,因此您并不是这种情况。

您可以通过正确地缩放您的目标函数来解决此问题,愚弄fminunc使您相信您的功能确实是连续的。为了实现这个目标,只需通过一个相当大的标量乘以你的补偿参数,如:

offset_x = transformation(1)*1000; 
offset_y = transformation(2)*1000; 
angle = transformation(3); 

和除以你的由同一组标量的最终解决方案,使像素数偏移。

通常,在非线性优化问题中适当缩放变量是至关重要的,即使您的问题不会遇到不连续问题。

相关问题