Ask Your Question
0

Return RGB values of the the current Pixel ?

asked 2013-02-18 11:43:19 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >50 is welcome to improve it.

How to return the RGB values of the current Pixel i stop on it by the mouse cursor??

My Code:

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

void mouseEvent(int evt, int x, int y, int flags, void* param){
    evt==CV_EVENT_MOUSEMOVE;
    printf("Current Position: x= %d y= %d\n",x,y);
}

int main() 
{
cvNamedWindow("rgb"); 
CvCapture*capture =cvCaptureFromCAM(0);
IplImage* rgb ;

while(1){
rgb= cvQueryFrame( capture ); 
cvShowImage("rgb",rgb);
cvSetMouseCallback("rgb",  mouseEvent, 0);



char c = cvWaitKey(33);
if( c == 27 ) break;
}

cvReleaseCapture( &capture );
cvDestroyWindow( "rgb" );

}
edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
0

answered 2013-02-18 12:08:10 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >50 is welcome to improve it.

You need to pass your image as a parameter to the callback and read it from the image. Please read the tutorials on docs.opencv where a clear explanation about accessing pixel values is described!

edit flag offensive delete link more
0

answered 2013-02-18 13:20:36 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >50 is welcome to improve it.

change your code:

IplImage* rgb ;
void mouseEvent(int evt, int x, int y, int flags, void* param)
{
    evt==CV_EVENT_MOUSEMOVE;
    char* data = rgb->imageData + rgb->widthStep * y + x * 3;
    printf("Current Position: x= %d y= %d B=%d G=%d R=%d\n",x,y,data[0],data[1],data[2]);
}
edit flag offensive delete link more

Comments

you can type cast IplImage as void and pass it to the function, That way you dont need to have it as global

benzun gravatar imagebenzun ( 2013-02-18 23:42:25 -0600 )edit

Does not matter.It is also true method.

Mostafa Sataki gravatar imageMostafa Sataki ( 2013-02-19 03:09:59 -0600 )edit
0

answered 2013-02-18 23:48:44 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >50 is welcome to improve it.

void mouseEvent(int evt, int x, int y, int flags, void* param)
{
    IplImage* rgb = (IplImage*) param;
    if(evt==CV_EVENT_MOUSEMOVE)
    {
          char* data = rgb->imageData + rgb->widthStep * y + x * 3;
          printf("Current Position: x= %d y= %d B=%d G=%d R=%d\n",x,y,data[0],data[1],data[2]);
    }
}


void main()
{
    IplImage *image;
.... load image....
     cvSetMouseCallback(name, my_mouse_callback, (void*) image);
}
edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-02-18 11:43:19 -0600

Seen: 6,229 times

Last updated: Feb 18 '13