2017-10-11 98 views
0

我正在嘗試爲interp2尋找一個等效的OpenCV函數,我參考這張海報以在OpenCV中使用重映射函數。Matlab Interp2的功能行爲與OpenCV相比有所不同

cv::remap (in opencv) and interp2 (matlab)

然而,我意識到,在這兩個功能之間的輸出的顯著差異。這是我的MATLAB代碼

U = [0.1 0.1 0.1; 0.2 0.2 0.2; 0.3 0.3 0.3]; 
X = [0 0 0; 0.5 0.5 0.5; 1 1 1]; 
Y = [0 0.5 1;0 0.5 1;0 0.5 1]; 
V = interp2(U,X,Y,'linear',NaN) 

我得到的輸出V矩陣

NaN  NaN  NaN 
    NaN  NaN  NaN 
    NaN  NaN 0.1000 

這是我的OpenCV代碼

#include "highgui.h" 
#include "cv.h" 
using namespace cv; 
using namespace std; 

int main(int argc, char** argv) 
{ 
    //generate flowmap model 
    CvFileStorage* fx = cvOpenFileStorage("result.txt", 0, CV_STORAGE_WRITE);//ask storage for save file 
    Mat xmesh = cvCreateMat(3, 3, 5); 
    Mat ymesh = cvCreateMat(3, 3, 5); 

    for (int i = 0; i < xmesh.rows; i++) 
     for (int j = 0; j < xmesh.cols; j++) 
     { 
      xmesh.at<float>(i, j) = i*0.5; 
      ymesh.at<float>(i, j) = j*0.5; 
     } 

    //generate optical flow folder 
    Mat u = cvCreateMat(3, 3, 5); 

    for (int i = 0; i <u.rows; i++) 
     for (int j = 0; j < u.cols; j++) 
     { 
      u.at<float>(i, j) = (i + 1)*0.1; 
     } 
    Mat v = Mat::zeros(u.size(), u.type()); 

    remap(u, v, xmesh, ymesh, INTER_LINEAR, 0, cvScalarAll(0)); 

    //convert mat to Iplimage 
    IplImage* xmesh_a = cvCloneImage(&(IplImage)xmesh); 
    IplImage* ymesh_a = cvCloneImage(&(IplImage)ymesh); 
    IplImage* u_a = cvCloneImage(&(IplImage)u); 
    IplImage* v_a = cvCloneImage(&(IplImage)v); 

    //save end to txt 
    cvWrite(fx, "xmesh", xmesh_a, cvAttrList()); 
    cvWrite(fx, "ymesh", ymesh_a, cvAttrList()); 
    cvWrite(fx, "u", u_a, cvAttrList()); 
    cvWrite(fx, "v", v_a, cvAttrList()); 
    cvReleaseFileStorage(&fx); 

    waitKey(); 

} 

的輸出V矩陣我得到的是

1.00000001e-001, 1.50000006e-001, 2.00000003e-001, 
1.00000001e-001, 1.50000006e-001, 2.00000003e-001, 
1.00000001e-001, 1.50000006e-001, 2.00000003e-001 

任何幫助將不勝感激。謝謝!

+0

在這種'U'握着你的數據,是沒有的情況下函數調用是這樣的:'interp2(X,Y,U,...)'? – Irreducible

+0

@簡縮感謝您的回覆,但我認爲網格插值沒有區別。閱讀此海報https://stackoverflow.com/questions/32235379/how-to-do-grid-interpolation-interp2-in-opencv – SimaGuanxing

回答

1

結果的差異與C++和MATLAB之間的索引差異有關,它們分別是基於0和基於1的索引。

interp2(U,X,Y,'linear',NaN)interp2(1:3,1:3,U,X,Y,'linear',NaN)相同更改爲interp2(0:2,0:2,U,X,Y,'linear',NaN)您將獲得與OpenCV相同的結果。

如果你想的remap結果是一樣的,即interp2您可以在xmeshymesh一步移回並搜索負位置產生nan值。

#include <cmath> 
//... 
//... 
for (int i = 0; i < xmesh.rows; i++) 
    for (int j = 0; j < xmesh.cols; j++) 
    { 
     xmesh.at<float>(i, j) = i*0.5-1; 
     ymesh.at<float>(i, j) = j*0.5-1; 
    } 
//... 
//... 
remap(u, v, xmesh, ymesh, INTER_LINEAR, 0, cvScalarAll(NAN)); 

結果:

nan nan nan 
nan nan nan 
nan nan 0.1 
+0

謝謝! @ rahnema1您能否告訴我如何修改OpenCV代碼以實現與Matlab函數相同的結果?謝謝! – SimaGuanxing

+0

我的意思是,如果我想要改變OpenCV代碼來實現在Matlab中有意義的interp2函數,我需要使用當前網格之外的網格值,對嗎?在這一點上,我需要使用NaN來劃分我的xmesh和ymesh嗎?我正在嘗試打NaA,但不知道如何在OpenCV中分配NaN值。 – SimaGuanxing

+0

您需要將網格向後退一步。我的意思是爲(int i = -1; i '並設置最後一個參數'重映射'功能轉換爲'NAN'。 – rahnema1

相關問題