Search code examples
c++opencvfloating-pointdoubleremap

How to use double typed maps for remapping in OpenCV


Problem with cv::remap():

I am using double values for my image coordinates and want to cv::remap() those to create an distorted image.

However, OpenCV only allows me to use CV_32FC1 (float) as map type, and not CV_64FC1 (double).

Except for typecasting my double values to float before remapping and using the float-type maps for cv::remap(), is there another way?

Code Snippet:

Eigen::Vector2d distort(Eigen::Vector2d & pointUndistorted);    

int main(int argc, char** argv) {
    cv::Mat image, dst;
    image = cv::imread(argv[1], 1);
    Eigen::Vector2d pointUndistorted;
    Eigen::Vector2d pointDistorted;
    int w = image.rows;
    int h = image.cols;
    cv::Mat map1(w,h,CV_64FC1);
    cv::Mat map2(w,h,CV_64FC1);

    for (int wIdx = 0; wIdx < w; ++wIdx)
    {
        for (int hIdx = 0; hIdx < h; ++hIdx)
        {
            pointUndistorted << (double)wIdx / (double)w -0.5, (double)hIdx / (double)h -0.5;
            pointDistorted = distort(pointUndistorted);

            map1.at<double>(wIdx,hIdx) = (pointDistorted[0] + 0.5) * h;
            map2.at<double>(wIdx,hIdx) = (pointDistorted[1] + 0.5) * w;
        }
    }

    cv::remap(image, dst, map1, map2, cv::INTER_LINEAR);
}

This gives me the following error:

OpenCV Error: Assertion failed (((map1.type() == (((5) & ((1 << 3) - 1)) + (((2)-1) << 3)) || map1.type() == (((3) & ((1 << 3) - 1)) + (((2)-1) << 3))) && map2.empty()) || (map1.type() == (((5) & ((1 << 3) - 1)) + (((1)-1) << 3)) && map2.type() == (((5) & ((1 << 3) - 1)) + (((1)-1) << 3)))) in remap, file /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/imgproc/src/imgwarp.cpp, line 1840


Solution

  • Thanks to @Yakk for the suggestion:

    I hacked a work-around with map.convertTo(map, CV_32FC1), which is working perfectly fine for me.