博客 / 詳情

返回

[Qt學習筆記]Qt鼠標事件mouseMoveEvent實時獲取圖像的座標和像素值

@TOC

1、介紹

上一篇介紹了使用OpenCV的setMouseCallback回調函數實現獲取鼠標點擊點的圖像座標和像素值,本篇使用鼠標事件mouseMoveEvent函數來實現實時獲取鼠標的座標和對應圖像點的像素值,並將結果實時顯示在label控件上。

2、效果展示

123.gif

3、實現過程

3.1 圖像的加載和顯示

這裏加載圖像並在QLabel控件上顯示,我這裏使用OpenCV的imread函數加載了圖像,然後把圖像轉換成QPixmap顯示在QLabel上。

    img = imread("lena.png");
    cvtColor(img, img, COLOR_BGR2RGB);
    QImage disImage = QImage((const unsigned char*)(img.data), img.cols, img.rows, QImage::Format_RGB888);
    QPixmap pix = QPixmap::fromImage(disImage);

    pix.scaled(ui->lbl_pic->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->lbl_pic->setPixmap(pix);  // label 顯示圖像

這裏也可以直接用QPixmap的load函數加載圖像和顯示,然後在mouseMoveEvent函數中要通過OpenCV獲取圖像的像素值時,將QPixmap格式再轉換成Mat類型。

3.2 設置鼠標跟蹤事件激活

激活控件內鼠標跟隨屬性,調用setMouseTracking(true)激活後在鼠標點擊控件內區域進入mouseMoveEvent函數實現鼠標跟隨。
如果想不點擊鼠標在控件內移動觸發mouseMoveEvent函數,就需要同時設置控件和窗口的setMouseTracking(true),這樣鼠標在控件內移動時可以實時跟蹤鼠標事件。

    /*激活控件鼠標跟隨屬性,激活後在點擊鼠標後進入mouseMoveEvent函數*/
    /*如果不點擊鼠標時想要在控件上觸發mouseMoveEvent函數,就需要同時激活控件和窗口*/
    ui->lbl_pic->setMouseTracking(true);
    setMouseTracking(true);
void Widget::mouseMoveEvent(QMouseEvent *event)
{
    QPoint pt = event->pos();
    QRect rect = ui->lbl_pic->geometry();
    if(rect.contains(pt)){
        QPoint PicPoint = QPoint(pt.x()-rect.x(), pt.y()- rect.y());
        QString str = QString("(x:%1,y:%2)").arg(PicPoint.x()).arg(PicPoint.y());
        ui->lbl_pos->setText(str);
        if(img.channels() == 1){
            int grayValue;
            switch (img.type())
            {
            case 0:
                grayValue = static_cast<int>(img.at<uchar>(Point(PicPoint.x(), PicPoint.y())));
                break;
            case 1:
                grayValue = static_cast<int>(img.at<char>(Point(PicPoint.x(), PicPoint.y())));
                break;
            case 2:
                grayValue = static_cast<int>(img.at<ushort>(Point(PicPoint.x(), PicPoint.y())));
                break;
            case 3:
                grayValue = static_cast<int>(img.at<short>(Point(PicPoint.x(), PicPoint.y())));
                break;
            case 4:
                grayValue = static_cast<int>(img.at<int>(Point(PicPoint.x(), PicPoint.y())));
                break;
            case 5:
                grayValue = static_cast<int>(img.at<float>(Point(PicPoint.x(), PicPoint.y())));
                break;
            case 6:
                grayValue = static_cast<int>(img.at<double>(Point(PicPoint.x(), PicPoint.y())));
                break;
            }
        }
        else
        {
            int value_B = static_cast<int>(img.at<Vec3b>(Point(PicPoint.x(), PicPoint.y()))[0]);
            int value_G = static_cast<int>(img.at<Vec3b>(Point(PicPoint.x(), PicPoint.y()))[1]);
            int value_R = static_cast<int>(img.at<Vec3b>(Point(PicPoint.x(), PicPoint.y()))[2]);
            QString str = QString("B:%1, G:%2, R:%3").arg(value_B).arg(value_G).arg(value_R);
            ui->lbl_pix->setText(str);
        }
    }
}

3.3 實現代碼

widget.h
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QMouseEvent>
#include "opencv2/opencv.hpp"

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

using namespace cv;
class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

protected:
    void mouseMoveEvent(QMouseEvent *event);
private:
    Ui::Widget *ui;
    Mat img;
};
#endif // WIDGET_H
widget.cpp
#pragma execution_character_set("utf-8")
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowTitle("座標像素實時監控");
    img = imread("lena.png");
    cvtColor(img, img, COLOR_BGR2RGB);
    QImage disImage = QImage((const unsigned char*)(img.data), img.cols, img.rows, QImage::Format_RGB888);
    QPixmap pix = QPixmap::fromImage(disImage);

    pix.scaled(ui->lbl_pic->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
    // label 顯示圖像
    ui->lbl_pic->setPixmap(pix);
    /*激活控件鼠標跟隨屬性,激活後在點擊鼠標後進入mouseMoveEvent函數*/
    /*如果不點擊鼠標時想要在控件上觸發mouseMoveEvent函數,就需要同時激活控件和窗口*/
    ui->lbl_pic->setMouseTracking(true);
    setMouseTracking(true);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    QPoint pt = event->pos();
    QRect rect = ui->lbl_pic->geometry();
    if(rect.contains(pt)){
        QPoint PicPoint = QPoint(pt.x()-rect.x(), pt.y()- rect.y());
        QString str = QString("(x:%1,y:%2)").arg(PicPoint.x()).arg(PicPoint.y());
        ui->lbl_pos->setText(str);
        if(img.channels() == 1){            //單通道圖像
            int grayValue;
            switch (img.type())
            {
            case 0:
                grayValue = static_cast<int>(img.at<uchar>(Point(PicPoint.x(), PicPoint.y())));
                break;
            case 1:
                grayValue = static_cast<int>(img.at<char>(Point(PicPoint.x(), PicPoint.y())));
                break;
            case 2:
                grayValue = static_cast<int>(img.at<ushort>(Point(PicPoint.x(), PicPoint.y())));
                break;
            case 3:
                grayValue = static_cast<int>(img.at<short>(Point(PicPoint.x(), PicPoint.y())));
                break;
            case 4:
                grayValue = static_cast<int>(img.at<int>(Point(PicPoint.x(), PicPoint.y())));
                break;
            case 5:
                grayValue = static_cast<int>(img.at<float>(Point(PicPoint.x(), PicPoint.y())));
                break;
            case 6:
                grayValue = static_cast<int>(img.at<double>(Point(PicPoint.x(), PicPoint.y())));
                break;
            }
            QString str = QString("Gray Value:%1").arg(grayValue);
            ui->lbl_pix->setText(str);
        }
        else                                //多通道圖像
        {
            int value_B = static_cast<int>(img.at<Vec3b>(Point(PicPoint.x(), PicPoint.y()))[0]);
            int value_G = static_cast<int>(img.at<Vec3b>(Point(PicPoint.x(), PicPoint.y()))[1]);
            int value_R = static_cast<int>(img.at<Vec3b>(Point(PicPoint.x(), PicPoint.y()))[2]);
            QString str = QString("B:%1, G:%2, R:%3").arg(value_B).arg(value_G).arg(value_R);
            ui->lbl_pix->setText(str);
        }
    }
}

4、源碼展示

本小例程的代碼放到我的開源gitte項目裏,歡迎一起學習,也希望能收穫你的小星星。
項目源碼PixelPos_MouseFollow

user avatar
0 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.