Ask Your Question
1

How to get pixel's value from a picture?

asked 2013-05-06 11:53:39 -0600

Jack Chung gravatar image

updated 2013-05-06 12:16:54 -0600

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 );

    IplImage* img = cvLoadImage("Koala.jpg");
    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 flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
10

answered 2013-05-06 12:41:24 -0600

HD_Mouse gravatar image

updated 2013-05-06 13:18:26 -0600

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.

edit flag offensive delete link more

Comments

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.

Jack Chung gravatar imageJack Chung ( 2013-05-06 12:47:35 -0600 )edit
4

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

StevenPuttemans gravatar imageStevenPuttemans ( 2013-05-06 13:47:37 -0600 )edit
0

answered 2017-11-16 21:42:35 -0600

Derzu gravatar image

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
edit flag offensive delete link more
-2

answered 2017-08-09 14:31:22 -0600

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 --------------
// -----------------------------------------------------

// Assuming you have a cv::Mat to start with called "input_mat"
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);
edit flag offensive delete link more

Comments

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

berak gravatar imageberak ( 2017-08-10 11:24:07 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2013-05-06 11:53:39 -0600

Seen: 120,890 times

Last updated: Aug 09 '17