Given two cv::Mat matrices that maps every pixel in the source image to a pixel in the destination image (R2 to R2), I would like to transform a source image to a destination image. I’ve sucessfuly done so using for loops but it is too slow:
cv::Mat srcImg(100,100,CV_8U);
//fill...
cv::Mat dstImg(100,100,CV_8U);
//dst2src ->backprojection
//these matrices indicates for each pixel in the destination image, where to map it from the source image
cv::Mat x_dst2src(100,100,CV_64F);
cv::Mat y_dst2src(100,100,CV_64F);
//fill...
for(int ydst=0; ydst!=100;++ydst)
{
for(int xdst=0; xdst!=100;++xdst)
{
double xsrc = x_dst2src.at<double>(ydst,xdst);
double ysrc = y_dst2src.at<double>(ydst,xdst);
double val = getBicubic(srcImg,xsrc,ysrc);
dstImg.at<double>(ydst,xdst) = val;
}
}
this basic code works, but VERY slow (my images are bigger than 100×100, and I must use bicubic).
Thanks,
-O-
There’s a nice OpenCV function that’s called remap(). And it does, surprisingly, exactly this transform.
To speed it up, you should look for another function that prepares the maps in a format that’s a bit more processor-friendly than a simple position map. (It’s something like mapTransform(), look for it in the
see alsopart of the remap() docs)Happy remapping!