


Understand the meaning of various parameters of CvType in OpenCV and related methods
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:
- Channels: That is, the value returned by mat.channels() can only be the three parameters 1~4.
- 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;
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); //通常用来表示彩色图,颜色值范围更广。
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
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
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!

Introduction In prompt engineering, “Graph of Thought” refers to a novel approach that uses graph theory to structure and guide AI’s reasoning process. Unlike traditional methods, which often involve linear s

Introduction Congratulations! You run a successful business. Through your web pages, social media campaigns, webinars, conferences, free resources, and other sources, you collect 5000 email IDs daily. The next obvious step is

Introduction In today’s fast-paced software development environment, ensuring optimal application performance is crucial. Monitoring real-time metrics such as response times, error rates, and resource utilization can help main

“How many users do you have?” he prodded. “I think the last time we said was 500 million weekly actives, and it is growing very rapidly,” replied Altman. “You told me that it like doubled in just a few weeks,” Anderson continued. “I said that priv

Introduction Mistral has released its very first multimodal model, namely the Pixtral-12B-2409. This model is built upon Mistral’s 12 Billion parameter, Nemo 12B. What sets this model apart? It can now take both images and tex

Imagine having an AI-powered assistant that not only responds to your queries but also autonomously gathers information, executes tasks, and even handles multiple types of data—text, images, and code. Sounds futuristic? In this a

Introduction The finance industry is the cornerstone of any country’s development, as it drives economic growth by facilitating efficient transactions and credit availability. The ease with which transactions occur and credit

Introduction Data is being generated at an unprecedented rate from sources such as social media, financial transactions, and e-commerce platforms. Handling this continuous stream of information is a challenge, but it offers an


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use