繁体   English   中英

OpenCV Remap参数及其使用方法?

[英]OpenCV Remap parameters and How to use it?

我不熟悉opencv,但是我需要使用功能“重新映射”来纠正图像。 我有一个分辨率为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错误:断言失败((((map1.type()== CV_32FC2 || map1.type()== CV_16SC2)&&!map2.data)||(map1.type()== CV_32FC1 && map2.type() == CV_32FC1))在重新映射中,文件/home/liliming/opencv-2.4.13/modules/imgproc/src/imgwarp.cpp中的第3262行在抛出'cv :: Exception'what()实例之后终止调用what():/首页/liliming/opencv-2.4.13/modules/imgproc/src/imgwarp.cpp:3262:错误:(-215)((map1.type()== CV_32FC2 || map1.type()== CV_16SC2)&& !map2.data)|| 函数重映射中的(map1.type()== CV_32FC1 && map2.type()== CV_32FC1)已中止(核心已转储)

我对此一无所知。 有人可以帮我吗?或提供一些示例代码?非常感谢!

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_32FV2类型的二维数组(每个元素两个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

我将重映射表分为两个表remapX,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完成重新映射。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM