- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to read the pixel value of a single channel image in OpenCV using C++?
Digital images are made of pixels. Using OpenCV, it is easy to read the value of pixels. However, if we want to get the pixel values, we have to handle a single channel separately.
Here we are loading an image in the matrix named 'cimage', and then it converts the image using 'cvtColor(cimage, img, COLOR_BGR2GRAY); ' and store it in the matrix named 'img'.
The following program read the pixel value of an image and shows the values in console window.
Example
#include<iostream> #include<opencv2/highgui/highgui.hpp> #include<opencv2/opencv.hpp> using namespace std; using namespace cv; int main() { int x;//Declaring an integer variable to hold values of pixels// Mat cimage = imread("colors.jpg");//loading an image// Mat img;//Declaring an empty matrix to store converted image// cvtColor(cimage, img, COLOR_BGR2GRAY);//Converting loaded image to grayscale image// for (int i = 0; i < img.rows; i++)//loop for rows// { for (int j = 0; j < img.cols; j++)//loop for columns// { x = (int)img.at<uchar>(i, j);//storing value of (i,j) pixel in variable// cout << "Value of pixel" << "(" << i << "," << j << ")" << "=" << x << endl;//showing the values in console window// } } imshow("Show", img);//showing the image// waitKey();//wait for keystroke from keyboard// return 0; }
Output
- Related Articles
- How to read the pixel value from the multichannel 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 set a single pixel using imagesetpixel() function in PHP?
- How to change Pixel values using Direct Access Method in OpenCV?
- How to convert a pixel value to a number value using JavaScript?
- 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 create a binary image in OpenCV using C++?
- How to invert a binary 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 read a single character using Scanner class in Java?
- How to write an image to a file using OpenCV?

Advertisements