0
$\begingroup$

How to map a point $(x,y)$ in $720\times1280$ rectangle to a point $(x_1,y_1)$ in a square $1080\times1080$?

I try with the following formula, but seems there's something wrong.

Let $y_{offset} = \frac{1280 - 720}{2}$. \begin{align} x_1 &= x \frac{1080}{720}\\ y_1 &= y \frac{1080}{720} - y_{offset} \end{align}

The above formula works if the square is $1440\times1440$ but $1080\times1080$.

demo

$\endgroup$
2
  • $\begingroup$ Because the aspect ratios of your source rectangle and target rectangle are different, you need to decide between three options: 1)scale the whole source to the whole target, which will change shapes. Circles will become ellipses. 2)Fit the whole source into the target, maintaining the aspect ratio. If the 720 is horizontal you will have vertical strips that are blank. 3) Cover the whole target with source material, maintaining the aspect ratio and allowing the edges of the source to fall outside the target and be lost. Any of the three is possible, but you need to specify which you want $\endgroup$ Apr 2, 2017 at 5:06
  • $\begingroup$ I think i want option 2. $\endgroup$ Apr 2, 2017 at 7:53

2 Answers 2

0
$\begingroup$

If your goal is to make it so the rectangle of dimensions $(w, h)$, transformed, is entirely contained within the square of dimensions $(w_1, h_1)$, you should multiply by $$S = \min\left(\frac{h_1}{h}, \frac{w_1}{w}\right)$$

which makes the offsets

$$x_{offset} = \frac{w_1 - S \times w}{2}$$

and similarly for $y$.

In this case, $S = \frac{1080}{1280} = \frac{27}{32}$, and the offsets are $x_{offset} = \frac{1080 - \frac{27}{32} \times 1280}{2}=\frac{1080-1080}{2}=0$ and $y_{offset} = \frac{1080 - \frac{27}{32} \times 720}{2}=\frac{1080-607.5}{2}=236.25$.

If you instead want the image to cover the square, use $\max$ instead of $\min$ above.

Your final equation here then is

$$x_1 = \frac{27}{32}x$$ $$y_1 = \frac{27}{32}y + 236.25$$

$\endgroup$
17
  • $\begingroup$ I have a list of points (a face shape) which are captured in 1280*720, but after translated those points using your formular, the face shape' scale seems increased, be larger $\endgroup$ Mar 31, 2017 at 10:35
  • $\begingroup$ Larger? That's quite surprising indeed. Since the scaling factor $27/32 < 1$, you should get a smaller result. $\endgroup$ Mar 31, 2017 at 11:18
  • $\begingroup$ I use max for calculating S. Sorry, i type the image size wrongly, it is actually 720*1280, i don't know if this is reason $\endgroup$ Mar 31, 2017 at 16:59
  • $\begingroup$ If you want it to fit in the square (so in this case it's smaller), use $min$. If you want the square to be completely filled (bigger), use $max$. $\endgroup$ Mar 31, 2017 at 19:11
  • $\begingroup$ So it is impossible that the face shape in 720*1280 screen can exactly map in the 1080*1080 screen? Because i found that the face shape can exactly mapped in the 1440*1440 screen, so i have no idea why it can't map for 1080*1080. $\endgroup$ Apr 1, 2017 at 3:03
0
$\begingroup$

For your specific case, the range of $y$ must be reduced from $1280$ to $1080$. To maintain the aspect ratio, the $x$ range must be reduced by the same ratio. You can just map $(x,y)$ to $(\frac {1080 x}{1280},\frac {1080 y}{1280})$. That will leave the image along one side of the target. As the $x$ direction of the image is small, you might want to add half the gap to the $x$ coordinates. This is $\frac 12(1080-\frac {1080 \cdot 720}{1280})=236.25$ which will center the image in $x$

$\endgroup$
3
  • $\begingroup$ i don't understand "just map (x,y)to (1080x / 1280,1080x / 1280)", why x and y are same? $\endgroup$ Apr 2, 2017 at 16:06
  • $\begingroup$ @akabc: because I made a typo. Fixed. $\endgroup$ Apr 2, 2017 at 16:15
  • $\begingroup$ i tried newX = x * 1080 / 1280 + 236.5 and newY = y * 1080 / 1280, the aspect ratio is correct, but the shape is scaled down. $\endgroup$ Apr 2, 2017 at 16:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .