Today I'm going to show you how to assessing
the pixel values of an image using OpenCv.
Digital color images are made out of pixels.
First we look at what is a pixel.
Pixel
Pixel or a dot means a small physical point or an
addressable element/point in an image. A pixel has its own coordinates which
means that a pixel is corresponds to any one value. If we considering an 8 bit
gray scale image the value of a pixel in that image must be a value between 0
and 255. This value is tallied to the intensity of the light photons striking
at that point. Each pixel stores a value which is proportional to the light
intensity at a given location.
Let’s see how to get the number of pixels
in an image
Total number of pixels = number of rows x number of
columns
Or the number of pixels in the image will equal to the
total number of (x,y) coordinate pairs of pixels. (Address of an image can be
written in x,y coordinates).
Following example will guide you to get the number of channels
in an image.
Example 1
============================================================
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "iostream"
using namespace cv;
using namespace std;
int
main()
{
Mat img;
img = imread("C:/Users/hashan/Desktop/progtpoint.jpg", CV_LOAD_IMAGE_GRAYSCALE);
//img =
imread("C:/Users/hashan/Desktop/progtpoint.jpg",
CV_LOAD_IMAGE_COLOR);
if (img.empty()) {
cout << "Error" << endl;
return -1;
}
namedWindow("New_image", CV_WINDOW_AUTOSIZE);
imshow("New_image",
img);
// print number of channels in image
cout << "image channels: " <<
img.channels() << endl;
// check if image is a single channel
if (img.channels() == 1) {
Scalar pixel = img.at<uchar>(10,
10);
cout << "Pixel
value at 5,6 cordinate : " << pixel.val[0]
<< endl;
}
// check if image is a 3 channel
else if (img.channels() == 3) {
Vec3b pixel = img.at<Vec3b>(10,
10);
int blue = pixel.val[0];
int green = pixel.val[1];
int red = pixel.val[2];
}
else {
cout << "this
is not a single channel image" << endl;
}
waitKey(0);
return 0;
}
=============================================================
Explanation
This code is carried out for a grey
scale image which only have single channel. The coordinates of a pixel is 10,
10.
pixel.val[0] contains a value from 0 to 255.
So the output is,
Let’s try the code for a color image.
You need to comment
following line in the code
img = imread("C:/Users/hashan/Desktop/progtpoint.jpg", CV_LOAD_IMAGE_GRAYSCALE);
And
uncomment the following line
//img = imread("C:/Users/hashan/Desktop/progtpoint.jpg",
CV_LOAD_IMAGE_COLOR);
Then run the code
I got the output as this.
Let’s accessing all
pixels of a gray color image and a RGB image
Example 2
==============================================================
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "iostream"
using namespace cv;
using namespace std;
int
main()
{
Mat img;
img = imread("C:/Users/hashan/Desktop/pixel.jpg", CV_LOAD_IMAGE_GRAYSCALE);
//img = imread("C:/Users/hashan/Desktop/pixel.jpg",
CV_LOAD_IMAGE_ANYCOLOR);
if (img.empty()) {
cout << "Error" << endl;
return -1;
}
namedWindow("New_image", CV_WINDOW_NORMAL);
imshow("New_image",
img);
// print number of channels in image
cout << "image channels: " << img.channels()
<< endl;
cout << "Pixel value: " << endl;
// check if image is a single channel
if (img.channels() == 1) {
for (int y =
0; y < img.cols; y++) {
for (int x =
0; x < img.rows; x++) {
Scalar pixel
= img.at<uchar>(x, y);
cout <<
pixel.val[0] << ",";
}
cout << endl;
}
}
else if (img.channels() == 3) {
for (int y =
0; y < img.cols; y++) {
for (int x =
0; x < img.rows; x++) {
Vec3b pixel
= img.at<Vec3b>(x, y);
int blue
= pixel.val[0];
int green
= pixel.val[1];
int red =
pixel.val[2];
cout << "[" << blue << "
" << green << " " << red << "],";
}
cout << endl;
}
}
else {
cout << "this
is not a single channel image" << endl;
}
waitKey(0);
return 0;
}
=============================================================
My outputs are follows
·
When I used a gray color image
When I used a RGB image
- 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
Thanks lots..
ReplyDeletenice blog, thx
ReplyDelete