openCV - reading pixel color of video

Post Reply
Amusesmile
Posts: 10
Joined: Fri Sep 24, 2010 1:26 pm

openCV - reading pixel color of video

Post by Amusesmile » Wed Nov 03, 2010 3:52 pm

Hey, I'm trying to read just pixel information within a video. I have the code working on a still image, and I also have a program that can read what position (x,y) the mouse is in when you click within a video window, but alas when I try to include the same "cvPtr2D(frame, y, x, NULL);" command on the video image, it crashes. Any tips? Thanks. -Josh

#include "highgui.h"


void mouseHandler(int event, int x, int y, int flags, void *param);
IplImage* frame;

int main( int argc, char** argv ) {
cvNamedWindow( "example1", CV_WINDOW_AUTOSIZE );
CvCapture* capture = cvCreateCameraCapture(0);
IplImage* frame;
cvSetMouseCallback( "example1", mouseHandler, NULL );

while(1) {
frame = cvQueryFrame ( capture );
if( !frame ) break;
cvShowImage( "example1", frame );
char c = cvWaitKey(33);
if(c == 27 ) break;
}


cvReleaseCapture( &capture );
cvDestroyWindow( "example1" );
return 0;
}

void mouseHandler(int event, int x, int y, int flags, void *param)
{

uchar* ptr;
//uchar* josh[10];
char label[20];

switch(event) {
/* left button down */
case CV_EVENT_LBUTTONDOWN:

fprintf(stdout, "Left button down");
fprintf(stdout, "Left button down (%d, %d).\n", x, y);

/* ptr = cvPtr2D(frame, y, x, NULL); <--THIS crashes the program, but it works with a still image =[ */
/*sprintf(label, "(%d, %d, %d)", ptr[0], ptr[1], ptr[2]);
fprintf(stdout, label);*/
break;
}}

Javier
Posts: 11
Joined: Tue Sep 21, 2010 6:01 pm

Re: openCV - reading pixel color of video

Post by Javier » Sun Nov 28, 2010 10:00 pm

I tried your code and it work after deleting the local variable call frame, and using only the global one with the same name.
The working code will be:



#include "highgui.h"
void mouseHandler(int event, int x, int y, int flags, void *param);
IplImage* frame;

int main( int argc, char** argv ) {
cvNamedWindow( "example1", CV_WINDOW_AUTOSIZE );
CvCapture* capture = cvCreateCameraCapture(0);
//IplImage* frame;
cvSetMouseCallback( "example1", mouseHandler, NULL );

while(1) {
frame = cvQueryFrame ( capture );
if( !frame ) break;
cvShowImage( "example1", frame );
char c = cvWaitKey(33);
if(c == 27 ) break;
}


cvReleaseCapture( &capture );
cvDestroyWindow( "example1" );
return 0;
}

void mouseHandler(int event, int x, int y, int flags, void *param)
{

uchar* ptr;
//uchar* josh[10];
char label[20];

switch(event) {
/* left button down */
case CV_EVENT_LBUTTONDOWN:

fprintf(stdout, "Left button down");
fprintf(stdout, "Left button down (%d, %d).\n", x, y);

ptr = cvPtr2D(frame, y, x, NULL);
sprintf(label, "(%d, %d, %d)", ptr[0], ptr[1], ptr[2]);
fprintf(stdout, label);
break;
}}

Post Reply