How to calculate the number of channels of an image in OpenCV using C++?

OpenCVC++Server Side ProgrammingProgramming

In this topic, we will understand how to find out the number of channels of an image. After running the program, the number of the channel will be shown in the console window.

To get the number of the channel, we have used a class of OpenCV named 'channels()'. When we pass the image matrix as an object of the class 'channels()', it gives the channel an integer value.

The following program counts the number of the channels and show it in the console window.

Example

#include<iostream>
#include<opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main(int argc, char** argv) {
   Mat image_load;//Declaring a matrix to load the image//
   image_load = imread("colors.jpg");//Loading image in the matrix//
   int number_of_channel = image_load.channels();//Storing the number of channels in the variable//
   cout << "The number of channel(s)=" << number_of_channel << endl;//Showing the number of channels//
   system("pause");//Pausing the system to check the number of channel//
   waitKey(0);
   return 0;
}

Output

raja
Published on 10-Mar-2021 12:18:19
Advertisements