
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
How to read the pixel value from the multichannel image in OpenCV using C++?
We have declared three variables named-'blue_Channel','green_channel' and 'red_channel'. The goals of these variables is to save the pixel values. We have used these variables inside the 'for loops'. Then we declared a matrix named 'color_Image_Matrix'.
The syntax of this method is:
blue_Channel = color_image_Matrix.at<Vec3b>(i, j)[0];
We used a BGR image. It has three channels. These channels maintain specific sequence, color_image_Matrix.at<Vec3b> (i, j) represents the pixel values located at (i,i) and [0] represents the first channel. For example, if we write the line as follows:
blue_Channel=color_image_Matrix.at<Vec3b> (30, 35) [0];
It means the variable 'blue_Channel' will have the first channel's pixel value located at(30, 35). This is how we can access the pixel values using OpenCV.
The following program reads pixel values of different RGB images and displays the different channel pixel's value in a console window.
Example
#include<iostream> #include<opencv2/highgui/highgui.hpp> using namespace std; using namespace cv; int main() { int blue_Channel; int green_Channel; int red_Channel; Mat color_image_Matrix; //Declaring a matrix to load the image// color_image_Matrix = imread("colors.jpg"); //loading image in the matrix// //Beginning of for loop to read pixel values of blue channel// for (int i = 0; i < color_image_Matrix.rows; i++)//loop for rows// { for (int j = 0; j < color_image_Matrix.cols; j++) { //loop for columns// blue_Channel = color_image_Matrix.at<Vec3b>(i, j)[0]; //To read the value of first channel.Here the blue channel is first channel// cout << "Value of pixel of blue channel" << "(" << i << "," << j << ")" << "=" << blue_Channel << endl; //showing the values in console window// } } //End of for loop to read pixel values of blue channel// //Beginning of for loop to read pixel values of green channel// for (int i = 0; i < color_image_Matrix.rows; i++)//loop for rows// { for (int j = 0; j < color_image_Matrix.cols; j++)//loop for columns// { green_Channel = color_image_Matrix.at<Vec3b>(i, j)[1]; //To read the value of first channel.Here the green channel is first channel// cout << "Value of pixel of green channel" << "(" << i << "," << j << ")" << "=" << blue_Channel << endl;//showing the values in console window// } } //End of for loop to read pixel values of green channel// //Beginning of for loop to read pixel values of red channel// for (int i = 0; i < color_image_Matrix.rows; i++)//loop for rows// { for (int j = 0; j < color_image_Matrix.cols; j++)//loop for columns// { red_Channel = color_image_Matrix.at<Vec3b>(i, j)[2]; //To read the value of first channel.Here the red channel is first channel// cout << "Value of pixel of red channel" << "(" << i << "," << j << ")" << "=" << blue_Channel << endl; //showing the values in console window// } } //End of for loop to read pixel values of red channel// if (waitKey(0)==27); cout << "Image read successfully…!"; return 0; }
Output
Image read successfully...
This program takes several minutes to run. It reads each pixel values from different channels. That's why it takes a few minutes to show the complete result.
- Related Articles
- How to read the pixel value of a single channel image in OpenCV using C++?
- How to access and modify pixel value in an image using OpenCV Python?
- How to get the value of a specific pixel in OpenCV using C++?
- How to read an image in Python OpenCV?
- How to change Pixel values using Direct Access Method in OpenCV?
- How to get the FPS value in OpenCV using C++?
- How to save an Image in OpenCV using C++?
- How to rotate an image in OpenCV using C++?
- How to change the brightness of an image in OpenCV using C++?
- How to decrease the Brightness of an image in OpenCV using C++?
- How to change Pixel Values using 'at' Method in OpenCV?
- How to view the pixel values of an image using scikit-learn in Python?
- How to load and show image in OpenCV using C++?
- How to create a binary image in OpenCV using C++?
- How to invert a binary image in OpenCV using C++?
