QT - Identify coordinates of an image in a different size Qlabel?

Asked

Viewed 246 times

3

Hello, I recently had a question about how to track coordinates of a Qlabel using the mouse cursor. It worked fine for what I was thinking of doing. What I intend to do is the following, I have an image that project on the screen through a Qlabel, included a function to track the coordinates of this Qlabel. Below a screen Printscreen of my project:

inserir a descrição da imagem aqui

But I will need the values of these correct image coordinates to work on in the future, but the only values the cursor gets are the pixel values the size of my Qlabel. For example, Qlabel is 551x461 in size, while the image is 960x720 in size. Is there any way I can capture the correct pixel coordinates of the image on Qlabel with different size? Is there any method of converting this data into QT? I understand that this problem seems to be too complicated, but I thought it would not cost to try.

Below the Coordinates capture code:

void mainwindow::on_ButtonAddFileira_clicked(){

this->pTimer.disconnect();
QImage image;

image.load("C:/Users/Syn/Desktop/TCC/Fotos Bolsão FEI/02 Bolsao_Cheio_Foreground.jpg");

//image = MatToQImage(this->ImagemBase.capturaImgBuffer());

ui->labelScreen->setPixmap(QPixmap::fromImage(image));
ui->labelScreen->setScaledContents(true);

connect(&cursorTimer, SIGNAL(timeout()), SLOT(cursor_timer_timeout()));
cursorTimer.start(50);

}

labelScreen is the Image Display Qlabel, and in the code below label_X and label_Y are the coordinates Labels on the screen.

void mainwindow::cursor_timer_timeout(){

QPoint cursorPos = ui->labelScreen->mapFromGlobal( QCursor::pos() );

int x = cursorPos.x();
int y = cursorPos.y();

ui->label_X->setNum(x);
ui->label_Y->setNum(y);
}

Thanks in advance for the help and attention, any little help will be welcome.

1 answer

5


If I understood what you mean by "correct coordinates", you would simply like to get the coordinates in the "real image" (without adjustment), and not in the "squeezed" image (the one that was adjusted/staggered when placed inside the QLabel).

I don’t know if you realize what you’ve done, but you simply indicated that the QLAbel automatically adjust the image to its dimensions when doing:

ui->labelScreen->setScaledContents(true);

Like the QLabel is a visual component (inherited from QWidget), your mouse capture method returns the coordinates in the visual component, not in the image. Therefore, these coordinates are limited to the dimensions of the QLabel.

You could try to calculate the "maladjustment", if your QLabel had the same aspect ratio of the image (which is not the case - I don’t know if it was intentional or not, but maybe it was your mistake). Your image has an aspect ratio of 4:3 (also called 1.33). Dividing the width by the height, you have:

960/720 = 1,33  ===>  1,33:1 ou 4:3

This is the reason of traditional aspect, quadradona, used in the old Tvs, for example. There is another famous aspect ratio called 16:9 (or 1.77:1) or, as it is more popularly known, Widescreen (the widest screens used in the most modern cinema and TV and in Fullhd videos, where the width is almost twice as high).

Well, yours QLabel has a bizarre aspect reason:

551/461 = 1,195  ===>  1,195:1

It is much closer to a square area (aspect ratio 1:1) than the original image, and why I imagined that you simply did not take care in defining this aspect ratio.

The big deal is that no matter how big your QLabel, you can calculate the value of the original pixel by making a simple rule of three. For example, if your QLabel has dimensions 614x461 (now yes in aspect 4:3) for a given x (width coordinate) or y (height coordinate), just do:

To the x:

inserir a descrição da imagem aqui

For the y:

inserir a descrição da imagem aqui

Note that you could make this same account for each of the components of coordinates (x and y) without worrying about the reason of aspect, and still get an approximation of the real coordinates. The point is that keeping the aspect ratio equal this approximation is simply better, because there is no distortion in the image as she is staggered to "fit" in an area smaller than the original.

  • 1

    Thank you, it helped a lot. I was able to get the image coordinates just by the rule of three. Really, I did not take into account the reason of aspect, I am beginner in manipulation of images, so I had no idea of this concept, more an acquired learning. The label had this bizarre size because I didn’t want to create a spacious window of my project, and I created the size of the label compatible with the screen. Again, thank you so much for your help friend!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.