我正在嘗試爲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
任何幫助將不勝感激。謝謝!
在這種'U'握着你的數據,是沒有的情況下函數調用是這樣的:'interp2(X,Y,U,...)'? – Irreducible
@簡縮感謝您的回覆,但我認爲網格插值沒有區別。閱讀此海報https://stackoverflow.com/questions/32235379/how-to-do-grid-interpolation-interp2-in-opencv – SimaGuanxing