Opencv,重新映射:从文件保存并加载map1和map2
为了获得更好的性能,我只想计算一次map1和map2,然后将其与remap()一起使用。两个映射的类型均为 CV_32FC1。我尝试将地图保存为 .bmp 图像或 .exr 文件,然后像这样加载它:
cv::Mat map1, map2, out_img;
map1 = cv::imread("map1.bmp", IMREAD_GRAYSCALE);
map2 = cv::imread("map2.bmp", IMREAD_GRAYSCALE);
map1.convertTo(map1, CV_32FC1);
map2.convertTo(map2, CV_32FC1);
cv::remap(in_img, out_img, map1, map2, cv::INTER_CUBIC, cv::BORDER_CONSTANT, cv::Scalar(0, 0, 0));
但我得到了黑色图像(out_img)。如果我在运行时计算map1和map2,一切都会正常,但它比从文件加载花费的时间要多得多。 我还尝试将地图存储为 xml 文件,但速度要慢得多。 所以我的问题是:有什么方法可以在每次我想使用 remap() 时不计算map1和map2?
For better performance i want to calculate map1 and map2 only once and then use it with remap(). Both maps are of type CV_32FC1. I tried to save the maps as .bmp images or .exr files and then load it like this:
cv::Mat map1, map2, out_img;
map1 = cv::imread("map1.bmp", IMREAD_GRAYSCALE);
map2 = cv::imread("map2.bmp", IMREAD_GRAYSCALE);
map1.convertTo(map1, CV_32FC1);
map2.convertTo(map2, CV_32FC1);
cv::remap(in_img, out_img, map1, map2, cv::INTER_CUBIC, cv::BORDER_CONSTANT, cv::Scalar(0, 0, 0));
But i get a black image (out_img). Everything works fine if i calculate map1 and map2 in runtime, but it tooks much more time than loading from file.
I also tried to store maps as xml files, but it is much more slowly.
So my question is: is there any way to not to calculate map1 and map2 everytime i want to use remap()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有类似的问题,这是我的解决方案:
使用 .png 而不是 .bmp。请注意,从 CV_32FC1 到 CV_8UC1 再回到 CV_32FC1 会使图像更平滑。
性能比使用 unactor 快约 7 倍。
希望有帮助!
Had a similar problem, here is my solution:
Used .png instead of .bmp. Beware that going from CV_32FC1 to CV_8UC1 and back to CV_32FC1 makes the image smoother.
Performance was about 7x faster than using undistort.
Hope it helps!
还有其他一些选择。
一般来说,
imread
需要IMREAD_UNCHANGED
标志,否则它有助于将内容转换为 3 通道 8 位。另存为 TIFF,它本身支持 float32
这要求您单独保存每个贴图,因为
imwrite
不想保存 2 通道图像 (np.dstack)。或者您插入第三个通道,其中没有任何重要内容。这还假设 OpenCV 是在支持 TIFF 的情况下构建的。我听说情况并非总是如此,但这种情况很少见。
使用
convertMaps
获取 16 位值并以任何可以无损存储 16 位数据的格式存储(PNG、TIFF)。 ConvertMaps 返回一个包含整数部分的
16SC2
映射和一个包含小数部分的16UC1
映射(对于 5+5 位插值查找表来说,实际上是 10 位)。There are a few other options.
Generally,
imread
requires theIMREAD_UNCHANGED
flag or else it helpfully converts things into 3-channel 8-bit.Save as TIFF, which natively supports float32
That requires you to save each map individually because
imwrite
doesn't want to save a 2-channel image (np.dstack). Or you insert a third channel with nothing important in it.This also assumes that OpenCV was built with support for TIFF. I hear that's not always the case but it's rare.
use
convertMaps
to obtain 16-bit valuesAnd store in any format that can store 16 bit data without loss (PNG, TIFF). convertMaps returns one
16SC2
map containing the integer parts and a16UC1
map containing fractional parts (practically 10 bit for 5+5 bit interpolation lookup table).