user2269556
user2269556

Reputation: 173

c++ How to take pixel coordinates from an image?

I have an image in C++ and I want to take the coordinates of a pixel (relatives to the image) clicking on it (with my picture available and open to click on it). I don't know how to define the signal and the slot required to do this. Thanks.

Upvotes: 0

Views: 2574

Answers (2)

Houssam Badri
Houssam Badri

Reputation: 2509

eventFilter(QObject *obj, QEvent *event){
//blabla
if (event->type() == QEvent::MouseButtonPress) {
QMouseEvent* MouseEvGrip = (QMouseEvent*)event;
Point2f clickePixel((float)MouseEvGrip->x(), (float)MouseEvGrip->y());
//blabla
}
}

Upvotes: 1

Peopleware
Peopleware

Reputation: 1419

There are events offered for your case. Read this post for further instructions.

I assume you are using a control to display the image that is derived from QWidget, e.g. QImage. QWidget-derived classes can handle mouse events like clicking and send a QMouseEvent. This contains the x- and y-coordinates relative to the widget that received the event. Use these to read the pixel value by calling QImage::pixel(x, y) (returning a QRgb).

Upvotes: 1

Related Questions