Home > Technology peripherals > AI > Understand the meaning of various parameters of CvType in OpenCV and related methods

Understand the meaning of various parameters of CvType in OpenCV and related methods

WBOY
Release: 2023-04-14 13:13:03
forward
1671 people have browsed it

1. Preface

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

2. CvType

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:

  1. Channels: That is, the value returned by mat.channels() can only be the three parameters 1~4.
  2. Depth depth: That is, the storage value range of each pixel parameter in mat. The value is stored in a byte[] array in each channel, and the range of values ​​in each array is determined by the depth.

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.

2.1 Depth-depth

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;
Copy after login

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.

  • U: unsigned int, unsigned integer, also a positive integer
  • S: signed int, signed integer, including negative and positive numbers, but all integers
  • F: float, single-precision floating point type, that is, with decimal point. (PS: The Float type itself supports negative numbers)

So we can understand it by combining the definition:

  • CV_8U : It is an 8-bit positive integer, representing the value range of the parameter 0~255
  • CV_8S: It is an 8-bit positive and negative number, representing the value range of the parameter -128~127
  • CV_16U : It is a 16-bit positive integer, representing the value range of the parameter 0~65535
  • CV_16S: It is a 16-bit positive and negative number, representing the value range of the parameter -32768~32767
  • CV_16F : It is a 16-bit floating point number, representing the value range of the parameter -65504 ~ 65504
  • CV_32S: It is a 32-bit positive integer, representing the value range of the parameter 2147483648~2147483647
  • CV_32F: It is a 32-bit floating point number, representing the parameter value range 1.18x10^-38^~3.40x10^38^
  • CV_64F: It is a 64-bit floating point number, representing the parameter value range 2.23x10^- 308^~1.79x10^308^

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.

2.2 Channels

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.

  • C1: Represents single channel
  • C2: Represents dual channel
  • C3: Represents tee to
  • C4: Represents four channel

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); //通常用来表示彩色图,颜色值范围更广。
Copy after login

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.

3. Others

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
Copy after login

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
Copy after login

3.1 Deprecated CV_USRTYPE1

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.

3.2 ELEM_SIZE method

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.

4. Summary

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!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template