參考: Getting MouseMoveEvents in Qt
請加入下列至 mainwindow.h
bool eventFilter(QObject *obj,QEvent *event);//事件偵測(滑鼠)
請加入下列至mainwindow.cpp
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
QString str;
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
QPoint pt = mouseEvent->pos();
if (event->type() == QEvent::MouseMove)
{
mouseEvent = static_cast<QMouseEvent*>(event);
str = "MouseMove" + QString::number(pt.x()) +", " + QString::number(pt.y());
setWindowTitle(str); // 顯示座標
}else if(event->type() == QEvent::MouseButtonPress){
str = "MouseButtonPress" + QString::number(pt.x()) +", " + QString::number(pt.y());
setWindowTitle(str); // 顯示座標
}
return false;
}
並且在mainwindow constructor加入
qApp->installEventFilter(this);
測試mouse move
----------------------------------------------------------------------------------------------------------------
以上搞定滑鼠偵測事件(mouse move), 現在才是進入正題抓像素正題
在ImageUtility.h加入下列
QRgb getPixel(cv::Mat3b &src, const unsigned int row,const unsigned int col); //取得(row,col)的像素值
在ImageUtility.cpp加入下列
//取得彩色影像(row,col)的像素值
QRgb getPixel(cv::Mat3b &src,const unsigned int row,const unsigned int col)
{
QRgb pixel;
const cv::Vec3b *srcrow = src[row];
pixel = qRgba(srcrow[col][2],srcrow[col][1],srcrow[col][0],255);
returnpixel;
}
在Basler.h加入下列
QRgb getRGBPixel(const unsigned int row,const unsigned int col);//取得(row,col)的像素值
在Basler.cpp加入下列
//取得(row,col)的像素值
QRgbBasler::getRGBPixel(constunsignedintrow,constunsignedintcol)
{
QRgb pixel;
if(_Running)
{
pixel = getPixel(_img_rgb,row,col);
return pixel;
}else{
return 0;
}
}
修改mainwindow.cpp中eventFilter
// 事件偵測
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
QString str;
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
QPoint pt = mouseEvent->pos();
if (event->type() == QEvent::MouseMove)
{
mouseEvent = static_cast<QMouseEvent*>(event);
str = "(" + QString::number(pt.x()) +", " + QString::number(pt.y()) + "), 倍率: " + QString::number(scaleFactor);
unsigned int x, y;
unsigned int r, g, b;
x = pt.x()/scaleFactor;
y = pt.y()/scaleFactor;
if( ui->actionCloseBaslerCCD->isEnabled() & x>0 & y>0)
if( basler.getRunningStatus())
{
QRgb pixel = basler.getRGBPixel(y, x);
try{
r = qRed(pixel);
g = qGreen(pixel);
b = qBlue(pixel);
str += ", RGB = (" + QString::number(r) + "," +
QString::number(g) + "," +
QString::number(r) + ")";
}catch(...){}
}
setWindowTitle(str); // 顯示座標
}else if(event->type() == QEvent::MouseButtonPress){
str = "MouseButtonPress" + QString::number(pt.x()) +", " + QString::number(pt.y());
//setWindowTitle(str); // 顯示座標
}else if(event->type() == QEvent::MouseButtonRelease){
str = "MouseButtonRelease" + QString::number(pt.x()) +", " + QString::number(pt.y());
//setWindowTitle(str); // 顯示座標
}
return false;
}
Youtube操作:
留言列表