如何在OpenCV中进行网格插值interp2 [英] How to do grid interpolation interp2 in OpenCV
问题描述
我正在尝试找出如何在OpenCV中完成与该matlab代码等效的工作,有一些地方可以使用cv::remap
替代interp2
,但是它给我的结果与matlab不同.>
如果有帮助,它可以在执行分段仿射变换的函数内部,作为拟合主动外观"模型的一部分.
[XI, YI] = meshgrid(1:img_Col, 1:img_Row);
imNew=zeros(nRow,nCol,nChannels);
for i=1:nChannels
imNew(:,:,i) = interp2(XI, YI, double(img(:,:,i)),Ix(: , :),Iy(: , :));
end
img_Col
和img_Row
只是img
的大小,我还包括了Ix
,Iy
,img
和imNew
的示例值(在代码运行之后)在 cv :: remap(在opencv中)和interp2(在matlab中)和 http://imgur.com/a/09QgM
感谢您的帮助!
您只需要这样做:
remap(img, imgNew, Ix, Iy, CV_INTER_LINEAR);
认真地,我对其进行了测试,它为您的MATLAB代码(以及附加的图像)提供了相同的结果.
人们提到的不规则"网格是指采样点网格(在您的情况下为XI
和YI
).虽然在MATLAB中这些图像上允许有任意值,但在OpenCV中,这些必须只是目标图像中的像素网格(在您的情况下为imgNew
):
XI = 1 2 ... n YI = 1 1 ... 1
1 2 ... n 2 2 ... 2
... ...
1 2 ... n m m ... m
这就是为什么在OpenCV中您甚至没有通过XI
和YI
矩阵传递remap
函数的原因,因为假定Ix
和Iy
对应于上面的采样点.
幸运的是,您相应地计算了Ix
和Iy
矩阵,因此开箱即用.
这都是由于remap
通过类似以下方式实现的事实
for x <- 1...n
for y <- 1...m
imgNew(x,y) = interpolate the value of img at the point (Ix(x,y), Iy(x,y))
end
end
I'm trying to figure out how to do the equivalent of this matlab code in OpenCV, A few places to use cv::remap
as an alternative to interp2
, but it is giving me different results than matlab.
In case it helps this is inside of a function that is performing a piecewise-affine transform, as part of fitting an Active Appearance Model.
[XI, YI] = meshgrid(1:img_Col, 1:img_Row);
imNew=zeros(nRow,nCol,nChannels);
for i=1:nChannels
imNew(:,:,i) = interp2(XI, YI, double(img(:,:,i)),Ix(: , :),Iy(: , :));
end
img_Col
and img_Row
are just the size of img
I've also included what sample values of Ix
, Iy
, img
, and imNew
(after the code has run) in this Google Drive folder (I only bothered to include the first channel of the images). I've seen it mentioned on a few other questions that remap
only works on a regular grid, which frankly I don't exactly know what that means (those questions are cv::remap (in opencv) and interp2 (matlab) and remap irregular to regular grid.
These images demonstrate what the goal is to make. http://imgur.com/a/09QgM
Thanks for any help!!
You just need to do:
remap(img, imgNew, Ix, Iy, CV_INTER_LINEAR);
Seriously, I tested it and it gives identical results to your MATLAB code (and the images you attached).
The 'irregular' grid people are mentioning refer to the sample points grid (XI
and YI
in your case). While in MATLAB these are allowed arbitrary values on the image, in OpenCV these have to be simply the grid of pixels in the target image (imgNew
in your case):
XI = 1 2 ... n YI = 1 1 ... 1
1 2 ... n 2 2 ... 2
... ...
1 2 ... n m m ... m
This is why in OpenCV you do not even pass the remap
function the XI
and YI
matrices since Ix
and Iy
are assumed to correspond to the sample points above.
Luckily you calculated your Ix
and Iy
matrices accordingly so it works just out of the box.
This is all due to the fact that remap
is implemented by something like:
for x <- 1...n
for y <- 1...m
imgNew(x,y) = interpolate the value of img at the point (Ix(x,y), Iy(x,y))
end
end
As mentioned in the remap
theory and documentation.
这篇关于如何在OpenCV中进行网格插值interp2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!