osanchezmon
osanchezmon

Reputation: 544

How to know coordinates in a real image from a scaled image

First of all thanks for your time reading my question :-)

I have an original image (w': 2124, h': 3204) and the same image scaled (w: 512, h: 768). The ratio for width is 4.14 (rw) and the ratio for height is 4.17 (rh).

I'm trying to know the coordinates (x', y') in the original image when I receive the coordinates in the scaled image (x, y). I'm using the formula: x' = x * rw and y' = y * rh. But when I'm painting a line, or a rectangle always appears a shift that is incremented when x or y is higher.

Please anybody knows how do I transform coordinates without losing accuracy?

Thanks in advance! Oscar.

Upvotes: 4

Views: 1397

Answers (3)

Stephen Chu
Stephen Chu

Reputation: 12832

Or you can use QTransform::quadToQuad to create a transform and use it to map points, rects, lines, etc.:

QVector<QPointF>    p1;
p1 << scaledRect.topLeft() << scaledRect.topRight() << scaledRect.bottomRight() << scaledRect.bottomLeft();
QVector<QPointF>    p2;
p2 << originalRect.topLeft() << originalRect.topRight() << originalRect.bottomRight() << originalRect.bottomLeft();
QTransform::quadToQuad(p1, p2, mappingTransform);
...
QPointF originalPoint = mappingTransform.map(scalePoint);

Upvotes: 4

prashant
prashant

Reputation: 361

Always use decimal points.Else you will get the shift Here also you can see

for x' = 512 * 4.14 = 2119.68 and y' = 768 * 4.17 = 3202.56

Here you are losing the coordinates. On which image you are drawing the line original or scaled? Thanks hope will help you...

Upvotes: 1

agsamek
agsamek

Reputation: 9074

Use more decimal points, eg. 4.1484375 and 4.171875, otherwise you get 5px difference.

Upvotes: 2

Related Questions