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();
}
以上是 VS+opencv实现鼠标移动图片 的全部内容, 来源链接: utcz.com/z/312821.html