Bilinear upsampling
We perform bilinear upsampling of a source image $\mathcal{S}$ with height h and width w to a destination
image $\mathcal{D}$ with height H and width W :
The coordinates of the destination image pixels (orange dots) pulled back in the source image frame (with pixels in blue circles) when h=w=3 and H=4, W=5. |
- For each pixel $(i, j)$ in $\mathcal{D}$ with $0\leq i < H$ and $0\leq j < W$, pull back to the coordinates $(x, y)$ in $\mathcal{S}$ linearly. As shown in the figure, if aligned with corners: \begin{eqnarray} y = \frac{h-1}{H-1} \,i\,,\quad x = \frac{w-1}{W-1}\, j\,.\nonumber\end{eqnarray} Otherwise, aligned with boundaries: \begin{eqnarray} y = \frac{h}{H}(i+0.5)-0.5\,,\quad x = \frac{w}{W}(j+0.5)-0.5\,,\nonumber\end{eqnarray} in which pixel values are from pixel center points.
- For each computed $(x, y)$, do bilinear interpolation as in wiki: \begin{eqnarray}\mathcal{D}(i, j)=\left[\begin{array}{cc} \lceil{y}\rceil-y & y-\lfloor{y}\rfloor\end{array}\right]\left[\begin{array}{cc} \mathcal{S}\left(\lfloor{y}\rfloor_+, \lfloor{x}\rfloor_+\right) & \mathcal{S}\left(\lfloor{y}\rfloor_+, \lceil{x}\rceil_{w-1}\right) \\ \mathcal{S}\left(\lceil{y}\rceil_{h-1}, \lfloor{x}\rfloor_+\right) & \mathcal{S}\left(\lceil{y}\rceil_{h-1}, \lceil{x}\rceil_{w-1}\right)\end{array}\right] \left[\begin{array}{c}\lceil x \rceil - x \\ x-\lfloor x \rfloor\end{array}\right] \,,\end{eqnarray} where $\lfloor {z} \rfloor_+\equiv \max(0, \lfloor{z}\rfloor)$ and $\lceil {z} \rceil_c\equiv \min(\lceil{z}\rceil, c)$.
Remarks:
- When aligned with boundaries, $(x, y)$ may be out of boundaries. In this case, we do truncations as above rather than extrapolations.
- The method above provides the same results as that from Pytorch, but only agrees with Tensorflow when aligned with corners.
哇塞,顶!
ReplyDelete