Tutorial 4 -Depth, No. of Channels & Type of Image OpenCV C++

Today I'm going to discuss about Depth, Number of Channels & Type of Image







Let’s learn about depth and channel using in image processing.

  •           Depth: The depth is a type of date in image.
  •     Channels:  It can be 1, 2, 3 or 4 channels.
  •      Type: image type provides details of both Depth and number of channels of image.


Depth
Number of Channels
Type
CV_8UC1
0
1
0
CV_8UC2
0
2
8
CV_8UC3
0
3
16
CV_8UC4
0
4
24
CV_8SC1
1
1
1
CV_8SC2
1
2
9
CV_8SC3
1
3
17
CV_8SC4
1
4
25
CV_16UC1
2
1
2
CV_16UC2
2
2
10
CV_16UC3
2
3
18
CV_16UC4
2
4
26
CV_16SC1
3
1
3
CV_16SC2
3
2
11
CV_16SC3
3
3
19
CV_16SC4
3
4
27
CV_32SC1
5
1
4
CV_32SC2
4
2
12
CV_32SC3
4
3
20
CV_32SC4
4
4
28
CV_32FC1
5
1
5
CV_32FC2
5
2
13
CV_32FC3
5
3
21
CV_32FC4
5
4
29
CV_64FC1
6
1
6
CV_64FC2
6
2
14
CV_64FC3
6
3
22
CV_64FC4
6
4
30

We will discuss the topic in examples.

Example 1


Try this code. Compile and run the code as usual. 

==============================================================
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace std;
using namespace cv;

int main(int argc, const char argv[]) {


     int channel,depth;
    
     Mat img(500, 500, CV_16S);  // Creat an image

     namedWindow("New",CV_WINDOW_AUTOSIZE); 
     imshow("New", img);

     String type2str(const Mat &img);
     float  type = img.type();
     channel = img.channels();  
     depth = img.depth();


     cout << "Type:" << type << endl;
     cout << "Channel:" << channel << endl;
     cout << "Depth:" << depth << endl;

     waitKey(0);
     destroyWindow("New");

}

===============================================================

  • ·        String type2str(const Mat &img);

     float  type = img.type();

This returns the type of the image from available image types those are included in above table.


  • ·        channel = img.channels();


Returns the number of channels in image


  • ·        depth = img.depth();


Returns the image depth


Example 2

=============================================================

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace std;
using namespace cv;

int main(int argc, const char argv[]) {
          const int format[] = { CV_8UC1 ,CV_8UC1,CV_8UC3,CV_8UC4,CV_8SC1,CV_8SC3,
                    CV_8SC4,CV_16UC1,CV_16UC3,CV_16UC4,CV_16SC1,CV_16SC3,CV_16SC4
                    ,CV_32SC1,CV_32SC3,CV_32SC4,CV_32FC1,CV_32FC3,CV_32FC4,CV_64FC1,
                   CV_64FC3,CV_64FC4 };

          int channel,depth;
         
          for (int x = 0; x < sizeof(format)/4; x++) {

                   Mat img(500, 500, format[x]);  // Creat an image
                   namedWindow("New", CV_WINDOW_AUTOSIZE);
                   imshow("New", img);
                   cout << "load " << x << "=";
                   String type2str(const Mat &img);
                   float  type = img.type();
                   channel = img.channels();
                   depth = img.depth();

                   cout << "Type:" << type;
                   cout << ",   Channel:" << channel;
                   cout << ",   Depth:" << depth << endl;

                   waitKey(2000);
                   destroyWindow("New");
          }
          waitKey();

}

==============================================================


 Explanation




From this code we create an integer type array and store the image formats. These formats are stored as an integers. You can point to an element and see how these elements are stored.


  • ·          for (int x = 0; x < sizeof(format)/4; x++) {


Creates a for loop to access all array elements.

  • ·        sizeof(format)

This returns the size of array in bytes. An integer has 4 bytes therefore the return value is equal to (4 x number of element in array). So the value is divided by 4.

In this code I’m not included in 2 channel image format. Reason is imshow can handle only 1-channel gray-scale and 3-4 channel BRG/BGRA images. If you need to use 2 channel then you should have convert it in to single channel image or 3-4 channel image.

We will discuss image conversion part in upcoming tutorials. 


  • Thank you very much for reading this tutorial.
  • If you have problems regarding this tutorial feel free to make a comment below or contact us by sending message through our facebook page or by sending email to this address progtpoint@gmail.com 
  • Follow us on facebook and Twitter also subcirbe us youtube

4 comments:

Social