2011-02-07 139 views
0

如下因素片段是來自OpenCV的 find_obj.cpp這是使用SURF演示,OpenCV的SURF比較描述

 

double 
compareSURFDescriptors(const float* d1, const float* d2, double best, int length) 
{ 
    double total_cost = 0; 
    assert(length % 4 == 0); 
    int i; 
    for(i = 0; i best) 
      break; 
    } 
    return total_cost; 
} 

 

至於我可以告訴它檢查歐氏距離,我不明白這是爲什麼它是以4人一組的方式進行的呢?爲什麼不一次計算整件事?

回答

3

通常情況下,這樣做是爲了使SSE優化成爲可能。 SSE寄存器的長度是128位,可以包含4個浮點數,因此可以使用一條指令並行執行4個減法運算。

另一個好處:你必須檢查循環計數器後,只有每第四個差異。即使編譯器不使用機會來生成SSE代碼,這也會使代碼更快。例如,VS2008沒有,甚至沒有-O2:

  
     double t0 = d1[i] - d2[i]; 
00D91666 fld   dword ptr [edx-0Ch] 
00D91669 fsub  dword ptr [ecx-4] 
     double t1 = d1[i+1] - d2[i+1]; 
00D9166C fld   dword ptr [ebx+ecx] 
00D9166F fsub  dword ptr [ecx] 
     double t2 = d1[i+2] - d2[i+2]; 
00D91671 fld   dword ptr [edx-4] 
00D91674 fsub  dword ptr [ecx+4] 
     double t3 = d1[i+3] - d2[i+3]; 
00D91677 fld   dword ptr [edx] 
00D91679 fsub  dword ptr [ecx+8] 
     total_cost += t0*t0 + t1*t1 + t2*t2 + t3*t3; 
00D9167C fld   st(2) 
00D9167E fmulp  st(3),st 
00D91680 fld   st(3) 
00D91682 fmulp  st(4),st 
00D91684 fxch  st(2) 
00D91686 faddp  st(3),st 
00D91688 fmul  st(0),st 
00D9168A faddp  st(2),st 
00D9168C fmul  st(0),st 
00D9168E faddp  st(1),st 
00D91690 faddp  st(2),st 
1

我認爲這是因爲對於每個分區域我們都會得到4個數字。完全4x4x4子區域製作64個長度矢量。所以它基本上獲得了2個子區域之間的差異。