问题描述
我正在尝试为interp2寻找等效的OpenCV函数,并且我参考此海报以在OpenCV中使用重映射函数.
I am trying to look for an equivalent OpenCV function for interp2 and I refer to this poster to use remap function in OpenCV.
cv :: remap(在opencv中)和interp2(matlab)
但是,我意识到这两个函数之间的输出存在显着差异.这是我的Matlab代码
However, I realized that there is a significant difference of the output between these two functions. Here is my Matlab Code
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代码
This is my OpenCV code
#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
任何帮助将不胜感激.谢谢!
Any help will be appreciated. Thanks!
推荐答案
结果的差异与c ++和MATLAB的索引编制分别基于0和1的索引有关.
The difference in the results is related to difference in indexing between c++ and MATLAB that is 0-based and 1-based respectively.
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相同的结果.
interp2(U,X,Y,'linear',NaN)
is the same as interp2(1:3,1:3,U,X,Y,'linear',NaN)
changing it to interp2(0:2,0:2,U,X,Y,'linear',NaN)
you will get the same result as OpenCV.
如果希望remap
的结果与interp2
的结果相同,则可以将xmesh
和ymesh
移回一步,并搜索负数位置以生成nan
值.
If you want result of remap
to be the same as that of interp2
you can shift the xmesh
and ymesh
one step back and search for negative locations to produce nan
values.
#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
这篇关于Matlab Interp2函数行为与OpenCV Remap相比有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!