opencv显示大图--利用鼠标在固定窗口内显示大图

来源:互联网 发布:php tmp目录 编辑:程序博客网 时间:2024/05/19 23:57

opencv显示大图–利用鼠标在固定窗口内显示大图

  opencv没有直接用于显示大图的函数,当显示图像大于屏幕尺寸的时候就不好显示。于是就想到利用setMouseCallback()函数来利用鼠标移动图片,效果见代码,暂时未加入放大缩小功能。

#pragma execution_character_set("utf-8")#include <opencv2/opencv.hpp>#include <iostream>using namespace cv;using namespace std;Mat test = imread("test.jpg"); //测试图像void on_mouse(int event, int x, int y, int flags, void *userdata);int main(){    int window_w = 800;    int window_h = 800;    if (!test.data)    {        cout << "load image failed !" << endl;        return 0;    }    namedWindow("test", 1);    Mat temp = test(Rect(0, 0, window_w, window_h));    int udata[4] = { window_w, window_h, test.cols, test.rows };    setMouseCallback("test", on_mouse, udata);    imshow("test", temp);    moveWindow("test", 40, 40);    waitKey();    return 0;}void on_mouse(int event, int x, int y, int flags, void *userdata){    int *window_param = (int *)userdata;    static int start_x = 0;    static int start_y = 0;    int w = *window_param;    int h = *(window_param + 1);    int img_w = *(window_param + 2);    int img_h = *(window_param + 3);    static Point ld = Point(0, 0);    static Point ru = Point(0, 0);    if (event == EVENT_LBUTTONDOWN)    {        ld = Point(x, y);    }    if (event == CV_EVENT_LBUTTONUP)    {        ru = Point(x, y);        //cout << "origin:" << ld << endl;        //cout << "now:" << ru << endl;        start_x = (ld.x - ru.x) * 1 + start_x;        start_y = (ld.y - ru.y) * 1 + start_y;        start_x = start_x > (img_w - w - 1) ? (img_w - w - 1) : start_x < 0 ? 0 : start_x;        start_y = start_y >(img_h - h - 1) ? (img_h - h - 1) : start_y < 0 ? 0 : start_y;        //cout << start_x << endl;        //cout << start_y << endl;        Rect roi(start_x, start_y, w, h);        Mat temp = test(roi);        imshow("test", temp);    }}
0 0
原创粉丝点击