- 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 get the value of a specific pixel in OpenCV using C++?
To read the value of a specific pixel, we can use either 'at' or 'direct access' method. Here, we will learn both of the approaches.
Let's start with 'at' method. The following program reads the pixel value located at (10, 29) of an RGB image.
Example
#include<iostream> #include<opencv2/highgui/highgui.hpp> using namespace std; using namespace cv; int main() { Mat image;//taking an image matrix// image = imread("sky.jpg");//loading an image// int x = image.at<Vec3b>(10, 29)[0];//getting the pixel values// int y = image.at<Vec3b>(10, 29)[1];//getting the pixel values// int z = image.at<Vec3b>(10, 29)[2];//getting the pixel values// cout << "Value of blue channel:" << x << endl;//showing the pixel values// cout << "Value of green channel:" << x << endl;//showing the pixel values// cout << "Value of red channel:" << x << endl;//showing the pixel values// system("pause");//pause the system to visualize the result// return 0; }
Output
The result of the program will show up in the console window. Here using the following three lines, we are getting the pixel form values of three different channels.
int x = image.at<Vec3b>(10, 29)[0]; int y = image.at<Vec3b>(10, 29)[1]; int z = image.at<Vec3b>(10, 29)[2];
In the first line, we read the pixel's value located at (10, 29) of the first channel (blue) and storing the value at 'x' variable. The second and third line is storing the value of 2nd and 3rd channel, respectively. Now let's learn how to read a pixel value using the 'direct access' method.
The following program reads the pixel value located at (10, 29) directly −
Example
#include<iostream> #include<opencv2/highgui/highgui.hpp> using namespace std; using namespace cv; int main() { Mat_<Vec3b>image;//taking an image matrix// image = imread("sky.jpg");//loading an image// Vec3b x = image(10, 29);//getting the pixel values// cout << x << endl;//showing the pixel values// system("pause");//pause the system to visualize the result// return 0; }
Output
- Related Articles
- How to read the pixel value of a single channel image in OpenCV using C++?
- 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 FPS value in OpenCV using C++?
- 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 get the pixel height of a character in the specified font using the imagefontheight() function in PHP?
- How to get the pixel width of a character in the specified font using the imagefontwidth() function in PHP?
- How to get the count of a specific value in a column with MySQL?
- Get the count of a specific value in MongoDB
- How to get items with a specific value from documents using MongoDB shell?
- How to get the pixel depth and color depth of a screen in JavaScript?
- How to get the position of the current frame in OpenCV using C++?
- Get the count of a specific value in MongoDB quickly
