Home > Article > Technology peripherals > Understand the meaning of various parameters of CvType in OpenCV and related methods
This article is based on the Java environment and introduces the CvType parameter passed when creating a Mat object in OpenCV 4.6.0v.
If you don’t quite understand the role and meaning of CvType.CV_8UCX, CvType.CV_8SCX, CvType.CV_16UCX, CvType.CV_16SCX and other
parameters.
So, this article can help you understand the role of channels in OpenCV Mat.
The following content is based on OpenCV SDK 4.6.0v
This type is mainly used to define the data type in Mat. A common usage scenario is to define it when creating Mat.
So what parameters does the CvType type define? Very simply, it determines the two basic indicators of the image data in Mat:
Regarding the concept of channels, I introduced it in a previous article https://zinyan.com/?p=493. If you don’t know much about the concept of channels, you can read the previous article.
We can see from the CvType source code that OpenCV has defined 8 depth parameters:
public static final int CV_8U = 0, CV_8S = 1, CV_16U = 2, CV_16S = 3, CV_32S = 4, CV_32F = 5, CV_64F = 6, CV_16F = 7;
The numbers in front of 8U, 8S, 16U, 16S, 32S, 64F, 16F, etc. in the above names represent the number of bits
That is to say: 8bite, 16bite, 32bite ,64bite. Used to define the value range, the following letters U, S, F represent the symbol and precision.
So we can understand it by combining the definition:
PS: Regarding the precision and range issues of floating point numbers, you can search for more details. The above parameter range is obtained through network summary.
In the OpenCV definition, the maximum number of channels is 4 and the minimum is 1. This is reflected in the code, which is C1, C2 defined in the CvType class. C3, C4.
In OpenCV, the color value of a pixel is stored in a double[] double-precision floating point array.
The channel defines the length of this double[] array.
For example, if the picture is a color picture in RGB format, then a pixel in the picture needs to be mixed with three values of R, G, and B to determine the specific color.
We need an array of double[3] to record the values of R, G, and B below the pixel.
So this picture uses C3 three channels.
And RGB is usually an integer in the range of 0~255.
OpenCV uses the depth we introduced above to represent the range of color values.
The combination of the two is:
CvType.CV_8UC3: represents three channels. The value range of the parameters in each channel is an 8-bit positive integer, which is 0 ~255
Finally, we combine the parameters when Mat was created to understand:
//zinyan:创建了一个4*4尺寸的图片。每个像素点存储了一个double[1]的数组,该数组中值的范围为0~255 Mat mat = new Mat(4, 4, CvType.CV_8UC1); //通常用来表示灰度图或黑白图 //zinyan:创建了一个5*5尺寸的图片。每个像素点存储了一个double[3]的数组,该数组中值的范围为0~255 Mat mat1 = new Mat(5, 5, CvType.CV_8UC3); //通常用来表示彩色图 //zinyan:创建了一个6*6尺寸的图片。每个像素点存储了一个double[3]的数组,该数组中值的范围为0~65535 Mat mat1 = new Mat(6, 6, CvType.CV_16UC3); //通常用来表示彩色图,颜色值范围更广。
To summarize, CvType What is defined are the types of data storage in Mat.
defines how many pixel values Mat stores and what is the range of each pixel value.
Because various algorithms for Mat process the value of each pixel. To process numerical values and perform calculations, you need to tell the algorithm what the value range of each numerical value is.
If Mat is passed in from outside. How do we determine the number of Mat channels and the value range of each value?
CvType provides related query methods, which allow us to convert to corresponding type values through type type.
The example is as follows:
Mat mat = new Mat(4, 4, CvType.CV_8UC3); int depth = CvType.depth(mat.type()); //输出结果值为 0 == CvType.CV_8U Mat mat1 = new Mat(4, 4, CvType.CV_16SC1); depth = CvType.depth(mat1.type()); //输出结果值为 3 == CvType.CV_16S
In addition, you can also query the number of channels .
Mat mat1 = new Mat(4, 4, CvType.CV_16SC1); int channels = CvType.channels(mat1.type());//该值为1
The value of CvType.CV_USRTYPE1 is already equivalent to CV_16F. So this parameter is also marked with @deprecated annotation. Represents that it has been abandoned
It is recommended that you do not use it.
The other methods in CvType are relatively easy to understand. Finally, there is a public static final int ELEM_SIZE(int type) method
This method passes the type value. That is the so-called CvType.CV_8UC1 and other parameter values.
Then what is returned is an int variable.
The variable returned by this method is to represent the number of bytes of parameters in each channel. 1 byte represents 8 bits, which is 8 bits.
So if we are CV_8U, CV_8S, just return the channel number directly.
If it is 16-bit data, it needs to return 2*channels, 32-bit data needs 4*channels, and 64-bit data needs 8*channels.
This concludes the introduction to CvType in OpenCV.
If we encounter an error regarding CvType during use. So in most cases it is caused by our unfamiliarity with CvType.
Or the algorithm that has requirements for channel and depth is used to cause errors.
The above is the detailed content of Understand the meaning of various parameters of CvType in OpenCV and related methods. For more information, please follow other related articles on the PHP Chinese website!