将CV_16SC2 Mat保存到文件OpenCV
我想将CV_16SC2矩阵保存到文件中.这是initUndistortRectifyMap的结果.我需要它以二进制形式,后来我想从文件中读取它.最好的方法是什么(YAML,XML由于所需的空间而不好)?
I want to save the CV_16SC2 matrix to a file. It is a result of initUndistortRectifyMap. I need it in a binary form, and later i want to read it from a file. What is the best method for that (YAML, XML is bad because of required space)?
If you don't want to use the provided ways XML/YMAL Input and Output. You have to write your own.
首先请确保您输入的类型正确.在您的计算机上使用cout << mat.type()
并检查下表.
First make sure you have the correct type. Use cout << mat.type()
on your and check in the table below.
C1 C2 C3 C4
CV_8U 0 8 16 24
CV_8S 1 9 17 25
CV_16U 2 10 18 26
CV_16S 3 11 19 27
CV_32S 4 12 20 28
CV_32F 5 13 21 29
CV_64F 6 14 22 30
现在,您知道Matrixelements的通道数和正确的类型(位计数).
例如,数字mat.type() == 11
表示您的类型是:CV_16SC2
,因此16位(带符号短)和2个通道.
Now you know the Number of channels and the correct type (bitcount) of your Matrixelements.
For example Number mat.type() == 11
would mean your type is: CV_16SC2
, so 16 bit (signed short) and 2 channel.
如果您现在不了解位计数和变量名之间的关系,请检查以下内容:
If you don't now the relation between bitcount and variablename check the following:
Unsigned 8bits uchar 0~255
Mat: CV_8UC1, CV_8UC2, CV_8UC3, CV_8UC4
Signed 8bits char -128~127
Mat: CV_8SC1,CV_8SC2,CV_8SC3,CV_8SC4
Unsigned 16bits ushort 0~65535
Mat: CV_16UC1,CV_16UC2,CV_16UC3,CV_16UC4
Signed 16bits short -32768~32767
Mat: CV_16SC1,CV_16SC2,CV_16SC3,CV_16SC4
Signed 32bits int -2147483648~2147483647
Mat: CV_32SC1,CV_32SC2,CV_32SC3,CV_32SC4
Float 32bits float -1.18*10-38~3.40*10-38
Mat: CV_32FC1,CV_32FC2,CV_32FC3,CV_32FC4
Double 64bits double
Mat: CV_64FC1,CV_64FC2,CV_64FC3,CV_64FC4
To access them you need to use at with the correct type!
在我们的CV_16SC2(带符号的短和2通道)示例中,这表示:Vec2s
.
In our CV_16SC2 (signed short and 2 channel) example that would mean: Vec2s
.
Vec
因为我们想要一个向量2
因为有2个通道s
因为我们想要一个带符号的短路.
Vec
because we want a vector, 2
because there are 2 channel, s
, because we want a signed short.
因此,要获取位置(5,7)处第二个通道的值到变量's'中,可以这样写:
So to get the value of the second channel at position (5,7) into variable 's', you could write:
Vec2s v = mat.at<Vec2s>(5, 7);
short s = v[1];
由于您要保存矩阵,因此您可能需要先写入矩阵的大小(行,列,通道),然后再写入所有值.
Since you want to save your Matrix you probably want to do first write the size (rows, cols, channels) of your matrix and afterwards all the values.
感谢 http://ninghang. blogspot.de/2012/11/list-of-mat-type-in-opencv.html 为我提供了最终了解一切的信息!
Thanks to http://ninghang.blogspot.de/2012/11/list-of-mat-type-in-opencv.html for providing me the information to finally understand everything!