# How to get pixel's value from a picture?

Hi guys,

I intend to get the pixel value from a picture, but I don't know how to do that, can someone help me please?

I have already loaded and resized an image, now I would like to get the pixel value from the new image and print it, probably I will need to use Matrices, but I don't know how. I would like to get the RGB pixel value and differentiate it in a matrix for each color ( Red, Green and Blue).

I would appreciate it if someone could help me with a sample code.

My code to load and resize the image is that:

#include <opencv\cv.h>
#include <opencv\highgui.h>

int main()
{
cvNamedWindow( "original", CV_WINDOW_AUTOSIZE );
cvNamedWindow( "new", CV_WINDOW_AUTOSIZE );

cvShowImage("original:",img);

CvSize size = cvSize(800, 600);
IplImage* tmpsize=cvCreateImage(size, img->depth, img->nChannels);

cvResize(img,tmpsize,CV_INTER_LINEAR);
cvShowImage("new",tmpsize);

cvWaitKey(0);
return 0;
}


Thank you, Jack.

edit retag close merge delete

Sort by ยป oldest newest most voted

The pixels array is stored in the "data" attribute of Mat. Let's suppose that we have a Mat Matrix where each pixel has 3 bytes (CV_8UC3).

For this example, let's draw a RED pixel at position 100x50.

Mat foo;
int x=100, y=50;


Solution 1:

Create a macro function that obtains the pixel from the array.

#define PIXEL(frame, W, x, y) (frame+(y)*3*(W)+(x)*3)
//...
unsigned char * p = PIXEL(foo.data, foo.rols, x, y);
p[0] = 0;   // B
p[1] = 0;   // G
p[2] = 255; // R


Solution 2:

Get's the pixel using the method ptr.

unsigned char * p = foo.ptr(y, x); // Y first, X after
p[0] = 0;   // B
p[1] = 0;   // G
p[2] = 255; // R

more

Everyone posts code, none of it was really helpful. Here's how you can do it:

Also posted here on PasteBin: https://pastebin.com/tA1R8Qtm

// -----------------------------------------------------
// ---------- FIND AVG LUMINENCE OF FRAME --------------
// -----------------------------------------------------

cv::Mat grayMat;
cv::cvtColor(input_mat, grayMat, CV_BGR2GRAY);

int Totalintensity = 0;
for (int i=0; i < grayMat.rows; ++i){
for (int j=0; j < grayMat.cols; ++j){
Totalintensity += (int)grayMat.at<uchar>(i, j);
}
}

// Find avg lum of frame
float avgLum = 0;
avgLum = Totalintensity/(grayMat.rows * grayMat.cols);

more

please use sum(), writing per-pixel loops is for morons (only).

( 2017-08-10 11:24:07 -0500 )edit

I'm going to put on my StephenPuttemans hat and ask, why not use the more modern and easier to use C++ interface?

The C++ interface has a very handy function: at(), which allows for random access of pixel values in a Mat object, the OpenCV image storage structure. Example: if you have a grayscale image (meaning the stored elements are chars), you can access the values like so:

image.at<char>(49,39);


The code above accesses the char element of the object 'image' at row 49, column 39.

That said, the above method is best for random access. The most efficient way to loop over the Mat object is pointer access, and you can access the pointer with the Mat member function ptr(). But dealing with pointers can be hairy, so you can alternatively use MatIterator_ to access the elements. See this tutorial for a better breakdown of the different methods of pixel access.

HOWEVER IF YOU INSIST ON USING THE C INTERFACE, the C interface provides a macro: CV_IMAGE_ELEM(). And you can also use pointer access like before.

more

Well, I am using the C interface because I don't know how to use the C++ interface. I will try to use that macro.

Thanks, Jack.

( 2013-05-06 12:47:35 -0500 )edit
4

Lol, funny to see I already got a hat named after me!

( 2013-05-06 13:47:37 -0500 )edit

Official site

GitHub

Wiki

Documentation