Is it similar to the cv::remap reprojection mapping function in opencv in matlab?

41 views (last 30 days)
As far as I know, I know interp2,interp,griddata,scatteredInterpolant and other functions can achieve my non-aligned regular grid data for mapping, but the efficiency is very low, on the contrary, the remap function in opencv is very fast and only does mapping projection. For example, I have the following non-gridded data points, known v = F(x,y), where x,y are non-regular data, I try to use scatteredInterpolant function for interpolation, but the computation time is up to several seconds.
My (x,y) coordinates data is in the following form:
load data.mat
validPtsX = double(validPtsX);
validPtsY = double(validPtsY);
[qX,qY] = meshgrid(1:W,1:H);
F = scatteredInterpolant(validPtsX(:),validPtsY(:),oriImg(:));
undistortImg1 = F(qX,qY); % took too long !
undistortImg2 = interp2(validPtsX,validPtsY,oriImg,qX,qY,"linear",0);% error: Grid arrays must have NDGRID structure.
However, I can quickly get my interpolated mapped image in OpenCV by using the following statement:
cv::Mat undistortImg;
cv::remap(oriImg, undistortImg, mapX, mapY, cv::INTER_LINEAR); // Supports both grid and non-grid data mapping
It is verified that the interp2 function has equivalent mapping power and execution speed to the cv::remap function when the mapping matrix mapX,mapY (or called queryX,queryY) is computed in advance!
undistortImg = interp2(img,mapX,mapY,"linear",0);% 等价OpenCV的cv::remap函数,速度在各自环境下等价一致!

Accepted Answer

cui on 14 Oct 2022
After checking, there is an interpolation function in matlab for RGB images/other types of images, currently only implemented internally, which is more efficient than the generic interp2 function. The premise is that when the interpolated coordinate points mapX,mapY are known, the function "images.internal.interp2d" can be used (the function signature may be changed in a future version), which is as efficient as the mex library file that generates the C code.

More Answers (1)

cui on 25 Jun 2022
Edited: cui on 25 Jun 2022
use "interp2" function!
The similarities and differences between "cv::remap" in opencv and "interp2" in matlab:
Main common point: both can interpolate (retrieval location does not belong to the source index) and map (retrieval location belongs to the source index) the image array, indexed with the same grid belonging to the image array.
The main difference: cv::remap is designed only for image arrays, that is, the known source index x,y must be non-negative positive integers incrementing from 0 (corresponding to matlab 1); while matlab's interp2 is designed for general array matrices, that is, both for image arrays and for other arbitrary arrays, the known source index does not have to start from 1 (corresponding to opencv for 0), if you do not specify the first two input parameters x,y, then by default and cv::remap function function function the same.
In summary, opencv "cv::remap" is only a small part of matlab's interp2 function, they can both interpolate and remap, but interp2 can complete all cv::remap all function, and vice versa.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!