Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在OpenCV C+中将16位图像转换为8位+;_Opencv - Fatal编程技术网

如何在OpenCV C+中将16位图像转换为8位+;

如何在OpenCV C+中将16位图像转换为8位+;,opencv,Opencv,我一直在使用connectedComponents功能 const uint16_t* depth_frame_data = (const uint16_t*)(rs2_get_frame_data(frame, &e)); Mat image(Size(WIDTH, HEIGHT), CV_16UC1, (void *)depth_frame_data, Mat::AUTO_STEP); threshold(image, image_th, one_meter, 255, THRESH

我一直在使用
connectedComponents
功能

const uint16_t* depth_frame_data = (const uint16_t*)(rs2_get_frame_data(frame, &e));
Mat image(Size(WIDTH, HEIGHT), CV_16UC1, (void *)depth_frame_data, Mat::AUTO_STEP);
threshold(image, image_th, one_meter, 255, THRESH_BINARY_INV);
Mat image_grayscale = image.clone();
image_grayscale.convertTo(image_grayscale, CV_8U, 1 / 256.0);
我认为这对于连接的组件来说已经足够了,但我刚刚意识到我应该输入二进制矩阵:

Mat image_bin(image_th, true);
image_bin.convertTo(image_bin, CV_8U, 0.00390625);
但是当我建造它的时候

OpenCV(3.4.2)错误:断言失败


通过使用另一个函数来解决它

const uint16_t* depth_frame_data = (const uint16_t*)(rs2_get_frame_data(frame, &e));
Mat image(Size(WIDTH, HEIGHT), CV_16UC1, (void *)depth_frame_data, Mat::AUTO_STEP);
threshold(image, image_th, one_meter, 255, THRESH_BINARY_INV);
Mat image_grayscale = image.clone();
image_grayscale.convertTo(image_grayscale, CV_8U, 1 / 256.0);

我猜克隆将8位图像转换为16位图像时出现问题

image_16bit = image_8bit * 257 ;
用于16位图像到8位图像

image_8bit = image_16bit / 257 ;

我希望这能有所帮助,谢谢。

请发布失败的断言……谢谢大家的关注,但实际上我得到了解决方案!我是应该删除这个问题还是就这样离开?离开它,接受你自己的答案。如果可能的话,你应该补充答案,为什么第一个版本失败了,有什么区别。我看不出.convertTo函数在使用上有什么真正的区别,所以答案对我没有帮助,可能还有其他原因。不幸的是,这会使图片变暗10-20%。肯定有更好的方法吗?这会将每个像素从16位整数转换为双精度,乘以
1/256.0
,然后再转换回8位整数。整数除以256,然后直接进行整数到整数的转换,速度会快得多。