将 16 位深度 CvMat* 转换为 8 位深度

发布于 2024-11-27 08:31:07 字数 366 浏览 0 评论 0原文

我正在使用 Kinect 和 OpenCV。我已经在这个论坛中搜索过,但没有找到与我的问题类似的内容。 我保留来自 Kinect(16 位)的原始深度数据,将其存储在 CvMat* 中,然后将其传递给 cvGetImage 以从中创建 IplImage*:

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

但现在我需要处理该图像才能执行 cvThreshdold并找到轮廓。这两个函数需要输入 8 位深度的图像。如何将 CvMat* heightMetersMat 转换为 8 位深度-CvMat* ?

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);

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* ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野稚 2024-12-04 08:31:07

@SSteve 给出的答案几乎对我有用,但是对 convertTo 的调用似乎只是砍掉了高位字节,而不是实际将值缩放到 8 位范围。由于@Sirnino 没有指定想要哪种行为,我想我应该发布将执行(线性)缩放的代码,以防其他人想要这样做。

SSteve 的原始代码:

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

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

m2.convertTo(m2, CV_8U, 0.00390625);

您还可以添加第四个参数 (delta),该参数将在乘以 alpha 后添加到每个值。有关详细信息,请参阅文档

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's original code:

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

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);

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.

三生一梦 2024-12-04 08:31:07

这应该有效:

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

但是根据OpenCV文档,CvMat是过时的。你应该使用 Mat 来代替。

This should work:

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

But according to the OpenCV docs, CvMat is obsolete. You should use Mat instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文