getting a pixel value with opencv

Hi guys,

is there anyone who knows how to get the value of a pixel (grayscale) in opencv? I used to use iplGetPixel when I was still doing pascal, but in OF I can’t get it done.


This is a good intro:–…-index.html

Your Q is answered under “Accessing image elements”

I mostly used this kind of double loop:

    int myPixelValue;  
    for( int i=0; i<height; i++ ) {   
        unsigned char* ptr = (unsigned char*)(cvImage->imageData   
                                    + i*cvImage->widthStep);   
        for( int j=0; j<width; j++ ) {   
            myPixelValue  = ptr[j];   

haven’t tried but:

double cvGetReal2D( const CvArr* arr, int idx0, int idx1 );  

for grayscale or:

CvScalar cvGet2D( const CvArr* arr, int idx0, int idx1 );  

for color should do.

if it’s a ofxCvGrayscaleImage() you could do:

myImage.getPixels()[i * w + j];  

i = x position you want
j = y position you want
w = width of the image

since getPixels() returns a continuous image. I know it’s not exactly what you asked, but thought I’d throw it out there as an alternative.

have fun!

Thanks everybody, I am now trying them all.