[英]OpenCV Remap parameters and How to use it?
I'm not familiar with opencv, but I need to use the function 'remap' to rectify the image. 我不熟悉opencv,但是我需要使用功能“重新映射”来纠正图像。 I have an image with 960x1280, and a remap file called 'remap.bin' with 9.8MB(is equaled to 960x1280x4x2, which means the two floats in one position(x,y)); 我有一个分辨率为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.
According to the explain, I code like this: 根据解释,我这样编码:
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;
But when I run the program I get this error: 但是,当我运行程序时,出现此错误:
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) || 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) in function remap Aborted (core dumped) 函数重映射中的(map1.type()== CV_32FC1 && map2.type()== CV_32FC1)已中止(核心已转储)
I have no idea about it. 我对此一无所知。 Could anyone help me? or give some sample codes? Thank you very much! 有人可以帮我吗?或提供一些示例代码?非常感谢!
The documentation for OpenCV 3.1 says: 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.
The assert says that map1 doesn't have a type of CV_32FC2
This is because you are creating and reading it with a type of CV_64FC1. 断言说map1没有CV_32FC2
类型,这是因为您正在使用CV_64FC1类型创建和读取它。
You need to convert it to the correct type: array of two dimensions of type CV_32FV2 (two 32-bit floats per element.) 您需要将其转换为正确的类型:CV_32FV2类型的二维数组(每个元素两个32位浮点数)。
The documentation goes on to say: 该文档继续说:
See `convertMaps` for details on converting a
floating point representation to fixed-point for speed.
Documentation can be found here: https://docs.opencv.org/3.1.0/da/d54/group__imgproc__transform.html#gab75ef31ce5cdfb5c44b6da5f3b908ea4 可以在此处找到文档: https : //docs.opencv.org/3.1.0/da/d54/group__imgproc__transform.html#gab75ef31ce5cdfb5c44b6da5f3b908ea4
I separate the remap table into two tables remapX, remapY. 我将重映射表分为两个表remapX,remapY。 Like this: 像这样:
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];
}
}
And then use the 然后使用
cv::remap(src, dst, map_x, map_y, cv::INTER_LINEAR);
It works well. 它运作良好。 But I don't know how to use one parameter map1 to finish remap. 但是我不知道如何使用一个参数map1完成重新映射。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.