Tuesday, October 11, 2005

Drag mouse to get rectangular region

void on_mouse( int event, int x, int y, int flags, void* param )
{
if( !image )
return;

if( image->origin )
y = image->height - y;

if( select_object )
{
selection.x = MIN(x,origin.x);
selection.y = MIN(y,origin.y);
selection.width = selection.x + CV_IABS(x - origin.x);
selection.height = selection.y + CV_IABS(y - origin.y);

selection.x = MAX( selection.x, 0 );
selection.y = MAX( selection.y, 0 );
selection.width = MIN( selection.width, image->width );
selection.height = MIN( selection.height, image->height );
selection.width -= selection.x;
selection.height -= selection.y;
}

switch( event )
{
case CV_EVENT_LBUTTONDOWN:
origin = cvPoint(x,y);
selection = cvRect(x,y,0,0);
select_object = 1;
break;
case CV_EVENT_LBUTTONUP:
select_object = 0;
if( selection.width > 0 && selection.height > 0 )
track_object = -1;
break;
}
}

///////////////// show sub-image
myImg = cvCloneImage(image);
cvSetImageROI( myImg, selection );
cvNamedWindow("help", 1);
cvShowImage("help", myImg);

///////////// or
CvMat *sub=cvCreateMat(selection.width,selection.height,myImg->depth);
cvGetSubRect(image, sub, selection);
IplImage *res=cvGetImage(sub, myImg);
cvShowImage("Histogram", res);



// mouse_rect.cpp : Defines the entry point for the console application.
//

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

IplImage *image = 0, *hsv = 0, *myImg = 0, *mask = 0, *backproject = 0, *histimg = 0;
CvHistogram *hist = 0;

int backproject_mode = 0;
int select_object = 0;
int track_object = 0;
int show_hist = 1;
CvPoint origin;
CvRect selection;
CvRect track_window;
CvBox2D track_box;
CvConnectedComp track_comp;
int hdims = 16;
float hranges_arr[] = {0,180};
float* hranges = hranges_arr;
int vmin = 10, vmax = 256, smin = 30;

void on_mouse( int event, int x, int y, int flags, void* param )
{
if( !image )
return;

if( image->origin )
y = image->height - y;

if( select_object )
{
selection.x = MIN(x,origin.x);
selection.y = MIN(y,origin.y);
selection.width = selection.x + CV_IABS(x - origin.x);
selection.height = selection.y + CV_IABS(y - origin.y);

selection.x = MAX( selection.x, 0 );
selection.y = MAX( selection.y, 0 );
selection.width = MIN( selection.width, image->width );
selection.height = MIN( selection.height, image->height );
selection.width -= selection.x;
selection.height -= selection.y;
}

switch( event )
{
case CV_EVENT_LBUTTONDOWN:
origin = cvPoint(x,y);
selection = cvRect(x,y,0,0);
select_object = 1;
break;
case CV_EVENT_LBUTTONUP:
select_object = 0;
if( selection.width > 0 && selection.height > 0 )
track_object = -1;
break;
}
}



int _tmain(int argc, _TCHAR* argv[])
{
IplImage *frame = cvLoadImage("cd.bmp",3);

image = cvCreateImage( cvGetSize(frame), 8, 3 );
//image->origin = 1;
cvCopy( frame, image, 0 );
cvNamedWindow( "Histogram", 1 );
cvNamedWindow( "CamShiftDemo", 1 );
cvShowImage("CamShiftDemo", image);
cvSetMouseCallback( "CamShiftDemo", on_mouse, 0 );
cvWaitKey(0);

myImg = cvCloneImage(image);
cvSetImageROI( myImg, selection );
cvNamedWindow("help", 1);
cvShowImage("help", myImg);
CvMat *sub=cvCreateMat(selection.width,selection.height,myImg->depth);
cvGetSubRect(image, sub, selection);
IplImage *res=cvGetImage(sub, myImg);
cvShowImage("Histogram", res);
cvWaitKey(0);

return 0;
}


No comments:

Post a Comment