Friday, November 26, 2004

Matrix Operation, mouse

array = cvCreateMat(row, col, CV_32FC1); // 32bits float, one Chanel
array->data.i[k]=(k+1);

Show position of mouse click. void On_mouse(int event, int x, int y, int flags), cvSetMouseCallback( "Lena2", On_mouse )

#ifdef _CH_
#pragma package <opencv>
#endif

#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include <stdio.h>


char wndname1[]="Lena";
IplImage *Lena = 0;
IplImage *planes[3];
IplImage *Lena_Result = 0;


void On_mouse(int event, int x, int y, int flags)
{
int Left_Click = 0;

switch(event)
{
case CV_EVENT_LBUTTONDOWN:
{
printf("Left Mouse click at %d, %d\n", x, y);
int i = 0;
int width, height;
CvPixelPosition8u pos_src;
uchar *val;
CvPoint pt;

CV_INIT_PIXEL_POS (pos_src,(unsigned char *) planes[0]->imageData,
Lena->widthStep,cvGetSize(Lena),0,0,
planes[0]->origin);
Lena_Result = cvLoadImage( "C:\\lena.jpg",1);

width = Lena->width;
height = Lena->height;
val = (uchar *)CV_MOVE_TO(pos_src,x,y,1);
uchar *val2 = 0 ;
printf("%3d\n",*val);

for(y=0; y<height; y++)
{
for(x=0; x<width; x++)
{
val2 = (uchar *)CV_MOVE_TO(pos_src,x,y,1);
if( (*val == *val2) ){
pt = cvPoint(x,y);
cvCircle( Lena_Result, pt, 1, 255, 1 );
}
}
}
cvNamedWindow("Result",0);
cvShowImage("Result",Lena_Result );
Left_Click = 1;
}
break;
case CV_EVENT_MOUSEMOVE:
{
if( Left_Click ){
printf("MOVE: %d, %d\n", x, y);
Left_Click = 0;
}
}
break;
}
}

int main( int argc, char** argv )
{
cvNamedWindow( "Lena",0);
cvNamedWindow( "Lena2",0);

cvSetMouseCallback( "Lena2", On_mouse );

Lena = cvLoadImage( "C:\\lena.jpg",1);
Lena_Result = cvLoadImage( "C:\\lena.jpg",1);

for( int i = 0; i < 3; i++ )
{
planes[i] = cvCreateImage( cvSize(Lena->width,Lena->height), 8, 1 );
}

cvCvtPixToPlane( Lena, planes[0], planes[1], planes[2], 0 );

cvShowImage( "Lena", Lena);
cvShowImage( "Lena2", planes[0]); //one channel image

cvWaitKey( -1 );

return 0;
}

#ifdef _EiC
main(1,"main.cpp");
#endif

No comments:

Post a Comment