将16位深度的CvMat *转换为8位深度 [英] Convert 16-bit-depth CvMat* to 8-bit-depth

查看:901
本文介绍了将16位深度的CvMat *转换为8位深度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Kinect和OpenCV.我已经在这个论坛中搜索了,但是没有发现类似我的问题的东西. 我保留了Kinect的原始深度数据(16位),将其存储在CvMat *中,然后将其传递到cvGetImage以从中创建IplImage *:

I'm working with Kinect and OpenCV. I already search in this forum but I didn't find anything like my problem. I keep the raw depth data from Kinect (16 bit), I store it in a CvMat* and then I pass it to the cvGetImage to create an IplImage* from it:

CvMat* depthMetersMat = cvCreateMat( 480, 640, CV_16UC1 );
[...]
cvGetImage(depthMetersMat,temp);

但是现在我需要处理此图像,以便执行cvThreshdold并找到轮廓.这两个功能在输入中需要一个8位深度图像.如何将CvMat * depthMetersMat转换为8位深度的CvMat *?

But now I need to work on this image in order to do cvThreshdold and find contours. These 2 functions need an 8-bit-depth-image in input. How can I convert the CvMat* depthMetersMat in an 8-bit-depth-CvMat* ?

推荐答案

@SSteve给出的答案几乎可以解决我的问题,但是对convertTo的调用似乎只是切断了高位字节,而不是实际缩放值到8位范围.由于@Sirnino没有指定想要的行为,因此我想我会发布将进行(线性)缩放的代码,以防其他人想要这样做.

The answer that @SSteve gave almost did the trick for me, but the call to convertTo seemed to just chop off the high order byte instead of actually scaling the values to an 8-bit range. Since @Sirnino didn't specify which behavior was wanted, I thought I'd post the code that will do the (linear) scaling, in case anyone else is wanting to do that.

SSteve的原始代码:

 CvMat* depthMetersMat = cvCreateMat( 480, 640, CV_16UC1 );  
 cv::Mat m2(depthMetersMat, true);  
 m2.convertTo(m2, CV_8U);

要缩放这些值,您只需要在调用convertTo的调用中添加比例因子(1/256或0.00390625,从16位到8位缩放)作为第三个参数(alpha)

To scale the values, you just need to add the scale factor (1/256, or 0.00390625, for 16-bit to 8-bit scaling) as the third parameter (alpha) to the call to convertTo

m2.convertTo(m2, CV_8U, 0.00390625);

您还可以添加第四个参数(增量),将其乘以alpha后将添加到每个值.有关更多信息,请参见文档.

You can also add a fourth parameter (delta) that will be added to each value after it is multiplied by alpha. See the docs for more info.

这篇关于将16位深度的CvMat *转换为8位深度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆