Pages

Monday, September 19, 2011

Select and Move Objects/Points using OpenCV


This post explains how to use the  OpenCV to select and move the objects.
 Before the explanations, watch this video to get the idea.


So first need to implement the mouse-handler that comes with OpenCV

  • As you can see, the controls are based on the mouse movements and clicks.
      • Left button down – select the object of clicked point - findImg( x, y)
      • Left button up – release the object if there any objects has been picked - releaseImg(selectedImg,x,y)
      • On mouse move – move the selected object with mouse
    • void mouseHandler(int event, int x, int y, int flags, void *param)
      {
      
      	switch(event) {
      	case CV_EVENT_LBUTTONDOWN:		//left button press
      		selectedImg=findImg( x, y);
      		break;
      
      	case CV_EVENT_LBUTTONUP:	//left mouse button release
      		if((selectedImg!=NULL)&&point!=-1){
      			releaseImg(selectedImg,x,y);
      			selectedImg=NULL;
      		}
      		break;
      
      	case CV_EVENT_MOUSEMOVE:
      		/* draw a rectangle*/
      		if(point!=-1){
      			if(selectedImg!=NULL){
      				temp = cvCloneImage(selectedImg);
      				cvRectangle(temp,
      					cvPoint(x - 1, y - 1),
      					cvPoint(x + 1, y + 1),
      					cvScalar(0, 0,  255,0), 2, 8, 0);
      
      				//adjust the lines
      				for(int i=0;i<nop;i++){
      					if(i!=point){
      						cvLine(temp,
      							cvPoint(x,y ),
      							cvPoint(globalCoordinateX[i] , globalCoordinateY[i] ),
      							cvScalar(0, 0,  255,0), 1,8,0);
      					}
      				}
      				cvShowImage("Drag & Drop", temp);
      			}
      			break;
      		}
      		cvReleaseImage(&temp);
      	}
      }

    • Then implement the logic for the findImg( x, y) & releaseImg(selectedImg,x,y)  methods according to your requirements.
      •  Here i used an integer array to store the initial position of each object.
      • Then search for object using the x & y coordinates when there is a click.
      • when releasing the object update the integer array with released coordinate.
    • Finally call the cvSetMouseCallback( "Drag & Drop", mouseHandler, NULL ) to activate the mouse handler to the window. where;
      • "Drag & Drop" - the OpenCV window name
      • mouseHandler - the mouse handler function name.
    Check the links for demo and source

1 comment: