1顾画、介紹
上一篇介紹了使用OpenCV的setMouseCallback回調(diào)函數(shù)實(shí)現(xiàn)獲取鼠標(biāo)點(diǎn)擊點(diǎn)的圖像坐標(biāo)和像素值泣侮,本篇使用鼠標(biāo)事件mouseMoveEvent函數(shù)來實(shí)現(xiàn)實(shí)時(shí)獲取鼠標(biāo)的坐標(biāo)和對(duì)應(yīng)圖像點(diǎn)的像素值昔榴,并將結(jié)果實(shí)時(shí)顯示在label控件上。
2卵酪、效果展示
3的诵、實(shí)現(xiàn)過程
3.1 圖像的加載和顯示
這里加載圖像并在QLabel控件上顯示,我這里使用OpenCV的imread函數(shù)加載了圖像匪补,然后把圖像轉(zhuǎn)換成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函數(shù)加載圖像和顯示,然后在mouseMoveEvent函數(shù)中要通過OpenCV獲取圖像的像素值時(shí)夯缺,將QPixmap格式再轉(zhuǎn)換成Mat類型蚤氏。
3.2 設(shè)置鼠標(biāo)跟蹤事件激活
激活控件內(nèi)鼠標(biāo)跟隨屬性,調(diào)用setMouseTracking(true)激活后在鼠標(biāo)點(diǎn)擊控件內(nèi)區(qū)域進(jìn)入mouseMoveEvent函數(shù)實(shí)現(xiàn)鼠標(biāo)跟隨踊兜。
如果想不點(diǎn)擊鼠標(biāo)在控件內(nèi)移動(dòng)觸發(fā)mouseMoveEvent函數(shù)竿滨,就需要同時(shí)設(shè)置控件和窗口的setMouseTracking(true),這樣鼠標(biāo)在控件內(nèi)移動(dòng)時(shí)可以實(shí)時(shí)跟蹤鼠標(biāo)事件。
/*激活控件鼠標(biāo)跟隨屬性于游,激活后在點(diǎn)擊鼠標(biāo)后進(jìn)入mouseMoveEvent函數(shù)*/
/*如果不點(diǎn)擊鼠標(biāo)時(shí)想要在控件上觸發(fā)mouseMoveEvent函數(shù)毁葱,就需要同時(shí)激活控件和窗口*/
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 實(shí)現(xiàn)代碼
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("坐標(biāo)像素實(shí)時(shí)監(jiān)控");
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);
/*激活控件鼠標(biāo)跟隨屬性,激活后在點(diǎn)擊鼠標(biāo)后進(jìn)入mouseMoveEvent函數(shù)*/
/*如果不點(diǎn)擊鼠標(biāo)時(shí)想要在控件上觸發(fā)mouseMoveEvent函數(shù)贰剥,就需要同時(shí)激活控件和窗口*/
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項(xiàng)目里,歡迎一起學(xué)習(xí)蚌成,也希望能收獲你的小星星前痘。
項(xiàng)目源碼PixelPos_MouseFollow