VS+opencv 마우스 이동 그림 구현

콘 솔 프로그램+opencv 기반 으로 마우스 왼쪽 단 추 를 누 르 면 관심 영역 을 표시 하기 위해 그림 을 드래그 할 수 있 습 니 다.

#include <opencv2/highgui/highgui.hpp>

//#include <iostream> 
//using namespace std; 
using namespace cv;

int win_width=1400,win_height=700;
Mat image,win_image;       //      
Rect rect_win,rect_img;

void moveImage()//      
{ 
 Mat image_ROI=image(rect_img);  //           ROI(       )
 image_ROI.convertTo(win_image,image_ROI.type());  // image_ROI     win_image
 ////       Mat win_image=image(rect_img); //     Why?
 imshow("    ",win_image);
}

void on_mouse( int event, int x, int y, int flags, void* ustc) //int x,int y,         (x,y)    ,          ,   x ,   y 
{
  // static        ,                 ,
  //            ,         ,                 
  static Point p0; 
  static int xrect_img;  //     ,              x、y
  static int yrect_img;
 if(event==CV_EVENT_LBUTTONDOWN)
 {
  p0=Point(x,y); //             
   xrect_img=rect_img.x;
   yrect_img=rect_img.y;
 }
 if(event==CV_EVENT_MOUSEMOVE&& (flags & CV_EVENT_LBUTTONDOWN)) //    ,     
 {  
   int dx=x-p0.x;
   int dy=y-p0.y;  
   if(x>=0 && x<=win_width-1 && y>=0 && y<=win_height-1) //              
   {   
    rect_img=Rect(xrect_img-dx,yrect_img-dy,rect_img.width,rect_img.height);  //        dx、dy(          )
    if(rect_img.x<0) 
    { 
     rect_img.x=0; 
    } 
    if(rect_img.y<0) 
    { 
     rect_img.y=0; 
    }   
    if(rect_img.x > image.cols-rect_img.width-1) 
    { 
     rect_img.x=image.cols-rect_img.width-1; 
    } 
    if(rect_img.y > image.rows - rect_img.height-1) 
    { 
     rect_img.y=image.rows - rect_img.height-1; 
    } 

    moveImage();
   }  
 } 
}

void main()
{  
 image=imread("im.jpg");
 //int win_width=1400,win_height=700;     //       1400 x 700
 //rect_win=Rect(0,0,win_width,win_height);   //       
 rect_img=Rect(0,0,win_width,win_height);   //          
 //win_image.create(win_height,win_width,image.type());
 //Mat tmp=image(rect_win);  //               
 //tmp.convertTo(win_image,tmp.type());  //       ROI            
 Mat win_image=image(rect_img);
 namedWindow("    ", 1);
 imshow("    ",win_image);

 setMouseCallback("    ", on_mouse); 
 waitKey();
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기