PDA

View Full Version : Show grey level value of pixel in Qt when the mouse is over it



benz6699
23rd July 2014, 11:50
Currently I am working on the display of gray level image with zoom feature. I am able to get the position of the pixel and the zoom feature is working well. However I encountered two problems:

1.) How can I get the grey level value of the pixel that is pointed by the mouse? I only managed to obtain the rgb value through “QRgb rgbValue = pix.toImage().pixel(x,y)”. How can I convert it into grey level value? Or is there any direct way to get the grey level value of the pixel.

2.) I have implemented “mouseMoveEvent(QMouseEvent *event)” and “setMouseTracking(true)”. However the function of “mouseMoveEvent(QMouseEvent *event)” is not functioning when I move the mouse. What is wrong with my code?

mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsItem>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void on_pushButton_clicked();

protected:
void mouseMoveEvent(QMouseEvent * event);

private:
Ui::MainWindow *ui;
QGraphicsScene* scene;
QGraphicsItem* item;
QPixmap pix;
};

#endif // MAINWINDOW_H

mainwindow..cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//QImage image("E:/ori.jpg");
QImage image("E:/image_00002.bmp");
pix = QPixmap::fromImage(image);
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
scene->addPixmap(pix);


}

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

void MainWindow::on_pushButton_clicked()
{
ui->graphicsView->setMouseTracking(true);
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
QPoint local_pt = ui->graphicsView->mapFromGlobal(event->globalPos());
QPointF img_coord_pt = ui->graphicsView->mapToScene(local_pt);

double x = img_coord_pt.x();
double y = img_coord_pt.y();

/* How can I get a gray level image here */
QRgb rgbValue = pix.toImage().pixel(x,y);

ui->label_X->setText(QString::number(x));
ui->label_Y->setText(QString::number(y));
ui->label_Value->setText(QString::number(rgbValue));
}

^NyAw^
23rd July 2014, 15:01
Hi,



1.) How can I get the grey level value of the pixel that is pointed by the mouse? I only managed to obtain the rgb value through “QRgb rgbValue = pix.toImage().pixel(x,y)”. How can I convert it into grey level value? Or is there any direct way to get the grey level value of the pixel.


GrayValue = 0.299*R + 0.587*G + 0.114*B



2.) I have implemented “mouseMoveEvent(QMouseEvent *event)” and “setMouseTracking(true)”. However the function of “mouseMoveEvent(QMouseEvent *event)” is not functioning when I move the mouse. What is wrong with my code?


You have to use the "mouseMoveEvent" of the QGraphicsItem instead of MainWindow.

wysota
23rd July 2014, 15:46
GrayValue = 0.299*R + 0.587*G + 0.114*B

Or just qGray(rgbValue).

benz6699
24th July 2014, 06:53
Or just qGray(rgbValue).

This is working as well!!! Thanks

Added after 6 minutes:


Hi,
GrayValue = 0.299*R + 0.587*G + 0.114*B
You have to use the "mouseMoveEvent" of the QGraphicsItem instead of MainWindow.

Thanks a lot. The gray value is working!!!

However for the "mouseMoveEvent" of the QGraphicsItem I am not very sure how to implement it. I am still very new in Qt. I have tried the following code but failed. It give me the following error messages

1.) E:\SelfLearning\3D_GUI\ViewZoomIn\mainwindow.cpp:3 2: error: C2027: use of undefined type 'QGraphicsSceneMouseEvent')
2.) E:\SelfLearning\3D_GUI\ViewZoomIn\mainwindow.cpp:3 2: error: C2227: left of '->globalPos' must point to class/struct/union/generic type

My code in mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsItem>
#include <QGraphicsView>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();


private slots:
void on_pushButton_clicked();

protected:
//void mousePressEvent(QMouseEvent * event);
void mouseMoveEvent(QGraphicsSceneMouseEvent * event);

private:
Ui::MainWindow *ui;
QGraphicsScene* scene;
QGraphicsItem* item;
QPixmap pix;
};

#endif // MAINWINDOW_H

snippet of code in mainwindow.cpp


void MainWindow::mouseMoveEvent(QGraphicsSceneMouseEven t *event)
{
// QGraphicsItem::mouseMoveEvent(event);
QPoint local_pt = ui->graphicsView->mapFromGlobal(event->globalPos());
QPointF img_coord_pt = ui->graphicsView->mapToScene(local_pt);

double x = img_coord_pt.x();
double y = img_coord_pt.y();

/* How can I get a gray level image here */
QRgb rgbValue = pix.toImage().pixel(x,y);
int greyvalue = qGray(rgbValue);

ui->label_X->setText(QString::number(x));
ui->label_Y->setText(QString::number(y));
ui->label_Value->setText(QString::number(greyvalue));
}

^NyAw^
24th July 2014, 08:44
Hi,

You have to create a class that inherits from QGraphicsItem(in your case QGraphicsPixmapItem). Then redefine the "mouseMoveEvent" method on this class.


class MyPixmapItem : public QGraphicsPixmapItem
...
...
void mouseMoveEvent()
{
// your code here
}

Also you will need to connect some kind of SIGNALs and SLOTs from MyPixmapItem to the main window if you want to show the gray value into a QLabel or QSpinBox(just an example).

When you add the Item in the scene you have to create an object of this class instead of the base class

This:


scene->addPixmap(pix);


Have to be something like this:


MyPixmapItem *item = new MyPixmapItem();
myScene->addItem(item);

benz6699
30th July 2014, 11:31
Hello,

Your suggestion is working. I have create a class CustomView that inherits from QGraphicsView. I have redefined the "mouseMoveEvent" in this class.

Snipper in CustomView.cpp

void CustomView::mouseMoveEvent(QMouseEvent *event)
{
QPoint local_pt = mapFromGlobal(event->globalPos());
QPointF img_coord_pt = mapToScene(local_pt);

double x = img_coord_pt.x();
double y = img_coord_pt.y();
emit positionMoved(x, y);
}

The code above is working perfectly to display the pixel position. However after I redefine the "mouseMoveEvent" in the new class, my panning function is not working anymore. Before that I used the following code in mainwindow to enable panning feature.

Snippet in mainwindow.cpp


ui->graphicsView->setDragMode(QGraphicsView::ScrollHandDrag);
ui->graphicsView->setInteractive(false);