OpenCV Remap参数及使用方法?

OpenCV Remap parameters and How to use it?

我对opencv不熟悉,需要用到remap函数来矫正图像。 我有一张 960x1280 的图像,以及一个名为“remap.bin”的重映射文件,大小为 9.8MB(等于 960x1280x4x2,这意味着两个浮点数在一个位置(x,y));

Applies a generic geometrical transformation to an image.

C++: void remap(InputArray src, OutputArray dst, InputArray map1, InputArray map2, int interpolation, int borderMode=BORDER_CONSTANT, const Scalar& borderValue=Scalar())

map1 – The first map of either (x,y) points or just x values having the type CV_16SC2 , CV_32FC1 , or CV_32FC2 . See convertMaps() for details on converting a floating point representation to fixed-point for speed.
map2 – The second map of y values having the type CV_16UC1 , CV_32FC1 , or none (empty map if map1 is (x,y) points), respectively.

根据解释, 我这样编码:

int main(int argc, char* argv[]){
    if(argc != 3){
        printf("Please enter one path of image and one path of mapdata!\n");
        return 0;
    }
    std::string image_path = argv[1];
    char* remap_path = argv[2];

    cv::Mat src = cv::imread(image_path); 
    cv::Mat dst;
    dst.create( src.size(), src.type());

    cv::Mat map2;
    map2.create( src.size(), CV_32FC1);
    map2.data = NULL;

    cv::Mat mapXY;
    mapXY.create( src.rows, src.cols, CV_64FC1);

    FILE *fp;
    fp = fopen(remap_path, "rb");
    fread(mapXY.data, sizeof(float), mapXY.cols*mapXY.rows*2, fp);
    fclose(fp);


    imshow("src", src);
    printf("remap!\n");
    cv::remap(src, dst, mapXY, map2, cv::INTER_LINEAR);
    imshow("dst", dst);

    cv::waitKey(0);
    return 0;

但是当我运行程序时我得到这个错误:

OpenCV Error: Assertion failed (((map1.type() == CV_32FC2 || map1.type() == CV_16SC2) && !map2.data) || (map1.type() == CV_32FC1 && map2.type() == CV_32FC1)) in remap, file /home/liliming/opencv-2.4.13/modules/imgproc/src/imgwarp.cpp, line 3262 terminate called after throwing an instance of 'cv::Exception' what(): /home/liliming/opencv-2.4.13/modules/imgproc/src/imgwarp.cpp:3262: error: (-215) ((map1.type() == CV_32FC2 || map1.type() == CV_16SC2) && !map2.data) || (map1.type() == CV_32FC1 && map2.type() == CV_32FC1) in function remap Aborted (core dumped)

我不知道。 谁能帮帮我?或者给一些示例代码? 非常感谢!

我把remaptable分成两个tableremapX,remapY。 像这样:

float *data_xy = (float *)malloc(sizeof(float)*960*1280*2);

FILE *fp;
fp = fopen(remap_path, "rb");
fread(data_xy, sizeof(float), 960*1280*2, fp);
fclose(fp);

for(int y=0; y<1280; ++y){
    for(int x=0; x<960; ++x){
        map_x.at<float>(y, x) = data_xy[(y*960+x)*2];
        map_y.at<float>(y, x) = data_xy[(y*960+x)*2+1];
    }
}

然后使用

cv::remap(src, dst, map_x, map_y, cv::INTER_LINEAR);

效果很好。 但是我不知道如何使用一个参数map1来完成remap。

OpenCV 3.1 的文档说:

map1    The first map of either (x,y) points or just x values having the type 
CV_16SC2 , CV_32FC1, or CV_32FC2.

断言说 map1 没有 CV_32FC2 的类型 这是因为您正在使用 CV_64FC1.

类型创建和读取它

您需要将其转换为正确的类型:CV_32FC2 类型的二维数组(每个元素两个 32 位浮点数。)

文档接着说:

See `convertMaps` for details on converting a 
floating point representation to fixed-point for speed.

可在此处找到文档:https://docs.opencv.org/3.1.0/da/d54/group__imgproc__transform.html#gab75ef31ce5cdfb5c44b6da5f3b908ea4