2012-01-22 43 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); 

和除以你的由同一組標量的最終解決方案,使像素數偏移。

通常,在非線性優化問題中適當縮放變量是至關重要的,即使您的問題不會遇到不連續問題。

相關問題