Vs + OpenCV to realize mouse moving picture


Based on the console application + opencv, the left mouse button can be clicked to drag the picture to display the area of interest.

#include <opencv2/highgui/highgui.hpp>

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

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

Void moveimage() // realize moving image
 Mat image? ROI = image (rect? IMG); // define ROI of interest area of source image (area to be displayed)
 Image? ROI. Convertto (win? Image, image? ROI. Type()); // image? ROI is copied to win? Image
 // // is too laggy to use Mat win_image=image (rect_img); // but very card Why?
 Imshow ("splicing result", win_image);

Void on mouse (int event, int x, int y, int flags, void * USTC) // int x, int y, which represents that the mouse is located in the (x, y) coordinate position of the window. The top left corner of the window is the origin by default, the right is the X axis, and the down is the Y axis.
  //Static declares a static local variable. The value does not disappear after the function call and retains the original value.
  //That is to say, the storage unit it occupies is not released, and the value at the end of the last function call will be retained in the next function call.
  static Point p0; 
  Static int xrect · img; // when the left key is pressed, the upper left corner of the image in the source image will be displayed in the window.
  static int yrect_img;
  P0 = point (x, y); // obtain the starting point when the left mouse button is pressed
 If (event = = CV ﹣ event ﹣ MouseMove & & (Flags & CV ﹣ event ﹣ lbuttondown)) // when the left button is pressed, the mouse moves
   int dx=x-p0.x;
   int dy=y-p0.y;  
   If (x > = 0 & & x < = win & width-1 & & Y > = 0 & & Y < = win & height-1) // judge whether the mouse is in the window image area
    Rect ﹣ img = Rect (xrect ﹣ img DX, yrect ﹣ img-dy, rect ﹣ img. Width, rect ﹣ img. Height); // the window shows that the image moves DX and Dy (relative to the left mouse button).
    if(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; 


void main()
 //Int win_width = 1400, win_height = 700; // fixed window size 1400 x 700
 //Rect_win = Rect (0,0, win_width, win_height); // the window displays a rectangular area.
 Rect [img = Rect (0,0, win [width, win [height); // the rectangular area corresponding to the window image
 //Mat TMP = image (rect? Win); // the window image corresponds to the region in the source image
 //TMP. Convertto (win_image, TMP. Type()); // copy the ROI of one image to the specified area of another image
 Mat win_image=image(rect_img);
 Namedwindow ("splicing result", 1);
 Imshow ("splicing result", win_image);

 Setmousecallback ("splicing result", on mouse); 

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.

Recommended Today

Query SAP multiple database table sizes

Query SAP multiple database table sizes https://www.cnblogs.com/ken-yu/p/12973009.html Item code db02 Here are two approaches, In the first graphical interface, the results of the query data table are displayed in MB, and only one table can be queried at a time. SPACE—Segments—Detailed Analysis—Detailed Analysis  In the pop-up window segment / object, enter the name of the […]