Basics of OpenCV API

Data Types for Arrays

Data type of an array defines the number of bits allocated for each element of array (pixels in an image) and how the value is represented using those bits. Any array elements should have one of following data types.

For single channel arrays : 

  • CV_8U (8 bit unsigned integer)
  • CV_8S (8 bit signed integer)
  • CV_16U (16 bit unsigned integer)
  • CV_16S (16 bit signed integer)
  • CV_32S (32 bit signed integer)
  • CV_32F (32 bit floating point number)
  • CV_64F (64 bit float floating point number)
e.g. : Here I have illustrated a single channel array with 8 bit unsigned integers. As the datatype of this array is 8 bit unsigned integers, each element should have a value from 0 to 255.
Single Channel Array with 8 bit unsigned integers
Single Channel Array
For multi channel arrays :
We can define all of above data types for multi channel arrays (supports up to 512 channels). Here I am going to show you how to define CV_8U data type for multi channel arrays.
  • CV_8UC1 (single channel array with 8 bit unsigned integers)
  • CV_8UC2 (2 channel array with 8 bit unsigned integers)
  • CV_8UC3 (3 channel array with 8 bit unsigned integers)
  • CV_8UC4 (4 channel array with 8 bit unsigned integers)
  • CV_8UC(n) (n channel array with 8 bit unsigned integers (n can be from 1 to 512) )
e.g. 1 : Here I have illustrated a 3 channel array with 8 bit unsigned integers. As the datatype is 8 bit unsigned integers, each element should have a value from 0 to 255. Because this is a 3 channel array, array consists of tuples with 3 elements. The first tuple is {54, 0, 34}, second tuple is {58, 78, 185} and so on.
3 - Channel Array for 8 bit unsigned integers
3 Channel Arrays

 

e.g. 2 : Here I have illustrated a 2 channel array with 8 bit signed integers. As the datatype is 8 bit signed integers, each element should have a value from -128 to 127. Because this is a 2 channel array, array consists of tuples with 2 elements. The first tuple is {-85, -127}, second tuple is {25, 23} and so on.
2 Channel Array with 8 bit signed integers
2 Channel Array
Note : CV_8U = CV_8UC1 = CV_8UC(1)
Example Usage :

 

    • Mat img1(3, 5, CV_32F ); //3 x 5 single-channel array with 32 bit floating point numbers
    • Mat img2(23, 53, CV_64FC(5) ); //23 x 53 5-channel array with 64 bit floating point numbers
    • Mat img3(Size(100, 200), CV_16UC2 ); //100 x 200 2-channel array with 16 bit unsigned integers
Remember :
Some OpenCV functions can handle only a subset of above data types. So, be careful, when using OpenCV functions.

Leave a Reply

Your email address will not be published. Required fields are marked *