simple code for image coordinate to screen coordinate.


Arslan Ahmed, this is the post for you. ^^

< gist code start >

< gist code end >




#Tags
mouse event, setMouseCallback, opencv, Total,

opencv mouse event example code


...
void CallBackFunc(int event, int x, int y, int flags, void* userdata)
{

    if (event == EVENT_LBUTTONDOWN)
    {
        printf("lLBUTTONDOWN down %d, %d \n", x, y);

        circle((*(Mat*)userdata), Point(x, y), 2, CV_RGB(255, 0, 0), 3);
    }
    else if (event == EVENT_RBUTTONDOWN)
    {
        printf("RBUTTONDOWN down %d, %d \n", x, y);
    }
    else if (event == EVENT_MBUTTONDOWN)
    {
        printf("MBUTTONDOWN down %d, %d \n", x, y);
    }
    else if (event == EVENT_MOUSEMOVE)
    {
        printf("move %d, %d \n", x, y);
    }

    //imshow("img", (*(Mat*)userdata)); //show
}


int main(int, char)
{
    namedWindow("img", 0);
    Mat img = imread("gh.jpg");
    setMouseCallback("img", CallBackFunc, &img);

    while (1)
    {
        imshow("img", img); //show
        if (waitKey(10) > 0)
            break;
    }

    destroyAllWindows();

    return 0;
}
...



OpenCV Mouse event example code

You can capture mouse event very easy using callback function.
"setMouseCallback” function is that callback function setting.

And many another events, so we can do various of application.
EVENT_MOUSEMOVE      = 0, 
EVENT_LBUTTONDOWN    = 1, 
EVENT_RBUTTONDOWN    = 2, 
EVENT_MBUTTONDOWN    = 3, 
EVENT_LBUTTONUP      = 4, 
EVENT_RBUTTONUP      = 5, 
EVENT_MBUTTONUP      = 6, 
EVENT_LBUTTONDBLCLK  = 7, 
EVENT_RBUTTONDBLCLK  = 8, 
EVENT_MBUTTONDBLCLK  = 9, 
EVENT_MOUSEWHEEL     = 10, 
EVENT_MOUSEHWHEEL    = 11 








< gist start >

< gist end >


#tags
setMouseCallback,

example source code - Region of interest image capture from video using opencv

Region of interest image capture from video using opencv

p key is pause, for capture of roi image patch
esc key is stop


..
#include < stdio.h>
#include < iostream>

#include < opencv2\opencv.hpp>

#ifdef _DEBUG        
#pragma comment(lib, "opencv_core249d.lib")
#pragma comment(lib, "opencv_highgui249d.lib")
#else
#pragma comment(lib, "opencv_core249.lib")
#pragma comment(lib, "opencv_highgui249.lib")
#endif 

using namespace std;
using namespace cv;

bool selectObject = false;
Rect selection;
Point origin;
Mat image;
bool pause =false;

Rect PatchRect;
Mat PatchImg;

static void onMouse( int event, int x, int y, int, void* )
{
 if( selectObject & pause)
 {
  
  selection.x = MIN(x, origin.x);
  selection.y = MIN(y, origin.y);
  selection.width = std::abs(x - origin.x);
  selection.height = std::abs(y - origin.y);
  selection &= Rect(0, 0, image.cols, image.rows);
 }

 switch( event )
 {
 case CV_EVENT_LBUTTONDOWN:
  origin = Point(x,y);
  selection = Rect(x,y,0,0);
  selectObject = true;
  break;
 case CV_EVENT_LBUTTONUP:
  if(selectObject && pause)
  {
   if(selection.width > 30 && selection.height > 30 )
   {
    PatchRect = selection;
    image( PatchRect ).copyTo( PatchImg );
    imshow("Selected Img", PatchImg );
   }else
    selection = Rect(0,0,0,0);
  }
  selectObject = false;
  pause = false;
  
  break;
 }
}


int main (void)  
{  


 VideoCapture cap(0);
 Mat frame;
 namedWindow( "Demo", 0 );
 setMouseCallback( "Demo", onMouse, 0 );
 printf("P key is pause, ESC key is exit.\n");

 for(;;)
 {
  if(!pause)
   cap >> frame;
  if( frame.empty() )
   break;
  frame.copyTo(image);


  if( pause && selection.width > 0 && selection.height > 0 )
  {
   rectangle(image, Point(selection.x-1, selection.y-1), Point(selection.x+selection.width+1, selection.y+selection.height+1), CV_RGB(255,0,0) );
  }
  
  imshow( "Demo", image );

  char k = waitKey(10);

  if( k == 27 )
   break;
  else if(k == 'p' || k=='P' )
   pause=!pause;
 }

 return 0;  
}  
--