如何在Qt Creator 2.8.1中使用鼠标左键单击事件和鼠标拖动事件在视频上绘制矩形基于Qt 5.1.1?(How to draw rectangle on video using mouse

编程入门 行业动态 更新时间:2024-10-11 21:28:15
如何在Qt Creator 2.8.1中使用鼠标左键单击事件和鼠标拖动事件在视频上绘制矩形基于Qt 5.1.1?(How to draw rectangle on video using mouse left button click event and mouse drag event in Qt Creator 2.8.1 Based on Qt 5.1.1?)

我需要帮助,我想在Qt creator中使用鼠标左键单击在视频上绘制矩形。我在Qlabel中加载视频。 我希望当我按下鼠标左键时,那些点应该成为矩形的起点(如x1,y1),然后我继续鼠标拖动而不离开鼠标左键,最后当我离开鼠标键时那些点应该成为我的矩形的终点(如x2,y2)。

我已经在visual studio 2008中使用visual C ++做了很多尝试在Qt创建者中尝试但是无法获得成功。 所以请帮助我。

提前致谢

这是我的鼠标事件将被调用的行。

cvSetMouseCallback("Motion Detector", mouseEvent, 0);

而bellow是我的鼠标事件完整代码:

void mouseEvent(int evt, int x, int y, int flags, void* param) { if(evt==CV_EVENT_LBUTTONDOWN) { p1.x = x; p1.y = y; drag = 1; } if(evt==CV_EVENT_LBUTTONUP) { p2.x = x; p2.y = y; drag = 0; if(p1.x > p2.x) { int temp = p1.x; p1.x = p2.x; p2.x = temp; } if(p1.y > p2.y) { int temp = p1.y; p1.y = p2.y; p2.y = temp; } posx = p1.x; posy = p1.y; } if(evt==CV_EVENT_MOUSEMOVE && drag==1) { cvCopyImage(frame,img1,0); cvDrawRect(img1,p1,cvPoint(x,y),cvScalar(255,0,0,0),1,8,0); cvShowImage("Motion Detector",img1); } }

这是我简单的Qt程序,它是QLabel中的显示图像,我想使用鼠标左键单击在该图像上绘制矩形并拖动矩形的大小,当我得到适当大小的矩形时,留下鼠标按钮。

Mainwindow.cpp

#include "mainwindow.h" #include "ui_mainwindow.h" #include <opencv2/opencv.hpp> #include <QFileDialog> #include "QMouseEvent" #include "QMoveEvent" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); //openImage(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::openImage() { int x1 = 1,x2 = 640,y1 = 100; iplImg = cvLoadImage("E://img2.jpg"); //cvLine(iplImg, cvPoint(x1,y1), cvPoint(x2,y1), cvScalar(255,0,0), 2, 0); qimgNew = QImage((const unsigned char*)iplImg->imageData,iplImg->width,iplImg->height,QImage::Format_RGB888).rgbSwapped(); ui->lblImage->setPixmap(QPixmap::fromImage(qimgNew)); } void MainWindow::on_btnOpen_clicked() { openImage(); }

main.cpp中

#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }

mainwindow.h

#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <opencv/cv.h> #include <opencv/highgui.h> #include <QMainWindow> //#include <QtGui/QWidget> #include<QMouseEvent> #include<QGraphicsLineItem> #include<QGraphicsScene> #include<QGraphicsView> #include<QGraphicsItem> #include<QHBoxLayout> namespace Ui { class MainWindow; } namespace converter { IplImage* QImage2IplImage(const QImage* qimg); QImage* IplImage2QImage(const IplImage* iplImg); } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void openImage(); //void mousePressEvent(); void on_btnOpen_clicked(); private: Ui::MainWindow *ui; //QString fileName; IplImage *iplImg; //char* charFileName; QImage qimgNew; //QImage qimgGray; }; #endif // MAINWINDOW_H

.pro文件

QT += core gui QT += widgets greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = Display_image_and_video TEMPLATE = app SOURCES += main.cpp\ mainwindow.cpp HEADERS += mainwindow.h FORMS += mainwindow.ui INCLUDEPATH += E:\\ImageProcessing\\opencv_cmake_binaries\\install\\include LIBS += -LE:\\ImageProcessing\\opencv_cmake_binaries\\install\\lib \ -lopencv_core244.dll \ -lopencv_highgui244.dll \ -lopencv_imgproc244.dll \ -lopencv_features2d244.dll \ -lopencv_calib3d244.dll

I need help, I want to draw rectangle on video using mouse left button click in Qt creator.I load the video in Qlabel. I wants when I press mouse left button at that time those points should become the starting point of rectangle (like x1,y1) and then I continue mouse drag without leave the mouse left button and last when I leave mouse button at that time those points should become the end points of my rectangle(like x2,y2).

I already did it in visual C++ in visual studio 2008 and lots of try to do it in Qt creator but can't get success. So please help me.

Thanks in advance

This is the line where my mouse event will be called.

cvSetMouseCallback("Motion Detector", mouseEvent, 0);

And bellow is my mouse event full code:

void mouseEvent(int evt, int x, int y, int flags, void* param) { if(evt==CV_EVENT_LBUTTONDOWN) { p1.x = x; p1.y = y; drag = 1; } if(evt==CV_EVENT_LBUTTONUP) { p2.x = x; p2.y = y; drag = 0; if(p1.x > p2.x) { int temp = p1.x; p1.x = p2.x; p2.x = temp; } if(p1.y > p2.y) { int temp = p1.y; p1.y = p2.y; p2.y = temp; } posx = p1.x; posy = p1.y; } if(evt==CV_EVENT_MOUSEMOVE && drag==1) { cvCopyImage(frame,img1,0); cvDrawRect(img1,p1,cvPoint(x,y),cvScalar(255,0,0,0),1,8,0); cvShowImage("Motion Detector",img1); } }

This is my simple Qt program which is display image in QLabel and I want to draw rectangle on that image using mouse left button click and drag the size of rectangle and leave the button of mouse when I got the appropriate size of rectangle.

Mainwindow.cpp

#include "mainwindow.h" #include "ui_mainwindow.h" #include <opencv2/opencv.hpp> #include <QFileDialog> #include "QMouseEvent" #include "QMoveEvent" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); //openImage(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::openImage() { int x1 = 1,x2 = 640,y1 = 100; iplImg = cvLoadImage("E://img2.jpg"); //cvLine(iplImg, cvPoint(x1,y1), cvPoint(x2,y1), cvScalar(255,0,0), 2, 0); qimgNew = QImage((const unsigned char*)iplImg->imageData,iplImg->width,iplImg->height,QImage::Format_RGB888).rgbSwapped(); ui->lblImage->setPixmap(QPixmap::fromImage(qimgNew)); } void MainWindow::on_btnOpen_clicked() { openImage(); }

main.cpp

#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }

mainwindow.h

#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <opencv/cv.h> #include <opencv/highgui.h> #include <QMainWindow> //#include <QtGui/QWidget> #include<QMouseEvent> #include<QGraphicsLineItem> #include<QGraphicsScene> #include<QGraphicsView> #include<QGraphicsItem> #include<QHBoxLayout> namespace Ui { class MainWindow; } namespace converter { IplImage* QImage2IplImage(const QImage* qimg); QImage* IplImage2QImage(const IplImage* iplImg); } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void openImage(); //void mousePressEvent(); void on_btnOpen_clicked(); private: Ui::MainWindow *ui; //QString fileName; IplImage *iplImg; //char* charFileName; QImage qimgNew; //QImage qimgGray; }; #endif // MAINWINDOW_H

.pro file

QT += core gui QT += widgets greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = Display_image_and_video TEMPLATE = app SOURCES += main.cpp\ mainwindow.cpp HEADERS += mainwindow.h FORMS += mainwindow.ui INCLUDEPATH += E:\\ImageProcessing\\opencv_cmake_binaries\\install\\include LIBS += -LE:\\ImageProcessing\\opencv_cmake_binaries\\install\\lib \ -lopencv_core244.dll \ -lopencv_highgui244.dll \ -lopencv_imgproc244.dll \ -lopencv_features2d244.dll \ -lopencv_calib3d244.dll

最满意答案

那么,你可以从覆盖QWidget中的虚函数开始:

mouseDoubleClickEvent mousePressEvent

看到事件被捕获(你应该为你想要绘制的标签QWidget做这个),然后在事件中改变显示的图片。

在你为你的问题做了代码更新之后我会说你可以创建自己的小部件QLabel子类,添加上面提到的鼠标事件覆盖,并使用QLabel的类实例insteat作为lblImage变量。 然后更改这些事件处理程序中的像素映射。

Well, you could start by overriding the virtual functions in QWidget:

mouseDoubleClickEvent mousePressEvent

to see that the events are captured (you should do this for the QWidget that is the label where you wan't to draw) and then in the event changed the picture displayed.

After the code update you did for your question i'd say that you can subclass QLabel you make your own widget, add the mouse event overrides mentioned above to it and use your class instance insteat of QLabel for the lblImage variable. And then change the pixel map in these event handlers.

更多推荐

本文发布于:2023-07-30 13:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338748.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:事件   鼠标   矩形   拖动   鼠标左键

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!