C++ CV_8U 和 CV_32F 之间有什么区别,在它们之间转换时我应该担心什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8377091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:27:41  来源:igfitidea点击:

What are the differences between CV_8U and CV_32F and what should I worry about when converting between them?

c++opencv

提问by mic

I have some code that is acting up and I suspect it's because I'm operating on the wrong types of data or converting between them poorly.

我有一些正在运行的代码,我怀疑这是因为我对错误类型的数据进行了操作,或者它们之间的转换很差。

It is mixing cv::Matobjects of types CV_8U(which is what is created when reading a jpg as grayscale with cv::imread), CV_32F, and CV_32S.

它混合cv::Mat类型的对象CV_8U(这是在将 jpg 作为灰度读取时创建的cv::imreadCV_32F、 和CV_32S

What are the differences between these data types, and what do I need to be sure of when converting between them?

这些数据类型之间有什么区别,在它们之间进行转换时我需要确定什么?

回答by Martin Beckett

CV_8U is unsigned 8bit/pixel - ie a pixel can have values 0-255, this is the normal range for most image and video formats.

CV_8U 是无符号 8 位/像素 - 即像素可以具有 0-255 的值,这是大多数图像和视频格式的正常范围。

CV_32F is float - the pixel can have any value between 0-1.0, this is useful for some sets of calculations on data - but it has to be converted into 8bits to save or display by multiplying each pixel by 255.

CV_32F 是浮点数 - 像素可以具有 0-1.0 之间的任何值,这对于某些数据集计算很有用 - 但它必须转换为 8 位才能通过将每个像素乘以 255 来保存或显示。

CV_32S is a signed 32bit integer value for each pixel - again useful of you are doing integer maths on the pixels, but again needs converting into 8bits to save or display. This is trickier since you need to decide how to convert the much larger range of possible values (+/- 2billion!) into 0-255

CV_32S 是每个像素的有符号 32 位整数值 - 再次对您对像素进行整数数学有用,但再次需要转换为 8 位以保存或显示。这比较棘手,因为您需要决定如何将更大范围的可能值(+/- 20 亿!)转换为 0-255

回答by Christian Rau

Basically they just describe what the individual components are:

基本上,它们只是描述了各个组件是什么:

  • CV_8U: 1-byte unsigned integer (unsigned char).

  • CV_32S: 4-byte signed integer (int).

  • CV_32F: 4-byte floating point (float).

  • CV_8U: 1 字节无符号整数 ( unsigned char)。

  • CV_32S: 4 字节有符号整数 ( int)。

  • CV_32F: 4 字节浮点 ( float)。

What you always have to keep in mind is that you cannot just cast them from one into the other (or it probably won't do what you want), especially between differently sized types.

您必须始终牢记的是,您不能只是将它们从一个转换为另一个(或者它可能不会做您想要的),尤其是在不同大小的类型之间。

So always make sure you use real conversion functions for converting between them, like cv::convertor cv::Mat::convertTo. Don't just try to access the elements of e.g. a cv::Matof CV_8Utype using e.g. cv::Mat::at<float>or cv::Mat_<float>.

因此,请始终确保使用真正的转换函数在它们之间进行转换,例如cv::convertcv::Mat::convertTo。不要只尝试访问的例如元素cv::MatCV_8U使用如类型cv::Mat::at<float>cv::Mat_<float>

Or if you just want to convert individual elements and don't want to create a new matrix of the other type, access the elements using the appropriate function (in the example cv::Mat::at<unsigned char>) and convert the result to float.

或者,如果您只想转换单个元素而不想创建其他类型的新矩阵,请使用适当的函数(在示例中cv::Mat::at<unsigned char>)访问元素并将结果转换为float.

Likewise is there also a difference between the number of components and a cv::Matof CV_8UC3type is different from an image of CV_8UC1type and should (usually) not be accessed by cv::Mat::at<unsigned char>, but by cv::Mat::at<cv::Vec3b>.

同样,组件数量之间也存在差异cv::MatCV_8UC3类型的a与类型的图像不同,CV_8UC1并且(通常)不应由 访问cv::Mat::at<unsigned char>,而是由访问cv::Mat::at<cv::Vec3b>

EDIT:Seeing Martin's answer it may be that you are aware of this all and his explanations are more what you have been looking for.

编辑:看到马丁的回答,您可能已经意识到这一切,而他的解释更符合您的要求。