views:

78

answers:

3

Hi folks,

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.

+1  A: 

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

agsamek
@agsamek Thanks so munch, I tried to use a double data type in all the operations and works fine :-)
ocell
No problem - easy rep. Just remember to accept the answer :)
agsamek
@agsamek Sorry but another answer is better than this for implementing it in Qt. Thanks for all!!!
ocell
+1  A: 

Hi 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...

prashant
+2  A: 

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);
Stephen Chu
@Stephen Chu Wow... really your solution is perfect for me :-) I use it and works really very good. Thanks!!!
ocell
You are welcome. Be careful if you use quadToQuad to create some exotic transformation like twisting or flip. It may fail to create the transformation. Be sure to check the function return which I skipped in my example.
Stephen Chu
@Stephen Chu Thanks for the advisement but I'm doing very simple transformations: from a scaled image to real image.
ocell