QT - Identify coordinates of an image in a different sized QLabel?

3

Hello, I recently had a question about how to trace coordinates of a QLabel using the mouse cursor. It worked well for what I thought of doing. What I want to do is the following, I have an image that I project on the screen through a QLabel, I include a function to trace the coordinates of this QLabel. Below a screen PrintScreen of my project:

ButIwillneedthevaluesofthesecorrectimagecoordinatestoworkonitinthefuture,buttheonlyvaluesthecursorgetsarethepixelvaluesofthesizeofmyQLabel.Forexample,QLabelhassize551x461,theimageisalready960x720.IsthereanywayIcancapturethecorrectpixelcoordinatesoftheimageintheQLabelwithdifferentsize?IsthereanymethodofconvertingthisdataintoQT?Iunderstandthatthisproblemseemstobetoocomplicated,butIthoughtitwouldnotbedifficulttotry.

BelowthecapturecodeoftheCoordinates:

voidmainwindow::on_ButtonAddFileira_clicked(){this->pTimer.disconnect();QImageimage;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 coordinate 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 your help and attention, any little help will be welcome.

    
asked by anonymous 07.08.2016 / 03:19

1 answer

5

If I understood correctly what you mean by "correct coordinates," you would simply like to get the coordinates in the "real image" (not fit), not the "squeezed" image (the one that was adjusted / placed within QLabel ).

I do not know if you noticed what you did, but you simply instructed that QLAbel automatically adjust the image in its dimensions when doing:

ui->labelScreen->setScaledContents(true);

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

You could try to calculate the "mismatch", if your QLabel had the same aspect ratio of the image (which is not the case - I do not know if it was intentional or not, but maybe it was an error on your part). Your image has a 4: 3 aspect ratio (also called 1.33). Dividing the width by height, we have:

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

This is the traditional aspect ratio, square, used on 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 most modern movies and TV and in FullHD videos, where the width is nearly double the height).

Well, your QLabel has a bizarre aspect ratio:

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

It's a lot closer to a square area (1: 1 aspect ratio) than the original image, so I figured you just did not care about setting that aspect ratio.

The big balcony is that, no matter the size of your QLabel , you can calculate the original pixel value by doing a simple rule of three . For example, if your QLabel is 614x461 (now 4: 3 aspect ratio), for a given x (width coordinate) or y (height coordinate), simply do: p>

For x:

Forthey:

  

Pleasenotethatyoucouldmakethissameaccountforeachofthe  componentsofthecoordinates(xandy)withoutworryingabouttheratioof  aspect,andstillgetanapproximationoftherealcoordinates.  Thepointisthatkeepingtheaspectratioequal,thisapproximationis  simplybetter,becausethereisnodistortionintheimageasit  isstaggeredto"fit" into a smaller area than the original.

    
07.08.2016 / 06:27