<h1>Explanation</h1>
<p>With a non-linear scale, you apply different weights for each pixel (or whatever unit you are using). You can use Euclidean distance towards the closest pixels in both directions to determine the weights.</p>
<h1>Example</h1>
<h2>Normalization</h2>
<p>For example, in the image below, the red image is the downsampled image (3x3) and the black image is the original image (4x4). The dots represent the sample points (center of the pixels).</p>
<p>You can align the two images by treating each pixel coordinate as a set of two values (x and y) between 0 and 1 (inclusive). Pixels should not be treated as square, so you should treat each pixel as a point rather than a square (when aligning the two images).</p>
<p><a href="https://i.sstatic.net/dPoxG.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/dPoxG.png" alt="Larger and downsampled image" /></a></p>
<p>In the above image, the four corners would all carry the weight of the original image's four corners, for example. The points in the middle would carry different weights depending on the difference between the four neighboring points.</p>
<h2>With numbers:</h2>
<p>This is a hypothetical area to sample in larger images (not related to the image above). The distances are between original pixels are normalized from 0 to 1 to simplify the math.</p>
<p>The black dots represent the sample points (pixels) of the original image. The larger red dots represent the sample points of the downsampled image. The red dot in the top left is closer to the black dot in the top left than the other three surrounding dots, so that pixel will have a heavier weight for determining the color of the downsampled pixel.</p>
<p><a href="https://i.sstatic.net/B2wip.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/B2wip.png" alt="Closeup of sampling region" /></a></p>
<p>Horizontal distance from:</p>
<ul>
<li>Right 2 original (green) pixels = 0.1 pixels</li>
<li>Left 2 original (green) pixels = 0.9 pixels</li>
</ul>
<p>Vertical distance from:</p>
<ul>
<li>Top 2 original (green) pixels = 0.1 pixels</li>
<li>Bottom 2 original (green) pixels = 0.9 pixels</li>
</ul>
<p>Weight for final pixel:
<span class="math-container">$$weight(0, 0) = (1.0 - 0.1)(1.0 - 0.1) = 0.81$$</span>
<span class="math-container">$$weight(0, 1) = (1.0 - 0.1)(1.0 - 0.9) = 0.09$$</span>
<span class="math-container">$$weight(1, 0) = (1.0 - 0.9)(1.0 - 0.1) = 0.09$$</span>
<span class="math-container">$$weight(1, 1) = (1.0 - 0.9)(1.0 - 0.9) = 0.01$$</span></p>
<p>Subtract from 1 both the horizontal and vertical distance (because closer pixels should carry more weight). The scale factor in this example is 0.75 in each direction.</p>
<h1>Scaling Up vs. Scaling Down</h1>
<p>Scaling up in the same  process as scaling down. By normalizing both the original image and the new image to between (0, 0) and (1, 1), you can do either with the same process. As an exercise, in the first figure, if the red points were from the original image and the black points were from the enlarged image, you should be able to see that the process is the same.</p>

CC BY-SA 3.0
2016-09-19T14:57:51.080Z
@justanothercoder While an ok answer. It does nothing for dispelling the myth that pixels are squares. You are dealing with point samples (that are not squares). And that resampling is in fact consisting of 2 distinct phases rolled into one, making the datafield continious and then sampling said continious function.
joojaa
CC BY-SA 3.0
2016-09-19T02:53:45.657Z
2016-09-20T05:30:08.800Z
2020-06-18T08:32:12.800Z
aces
<p>Suppose I have to use bilinear interpolation to rescale image. I more or less understand how enlargement works in case of integer scale factor. </p>

<p>But what to do with non-integer scale and especially when you need to downsample an image?</p>

<algorithm><image-processing><interpolation>
Image rescaling algorithm
justanothercoder
# Image rescaling algorithm

(Asked by: username_0 on 2016-09-18)

Suppose I have to use bilinear interpolation to rescale image. I more or less understand how enlargement works in case of integer scale factor.

But what to do with non-integer scale and especially when you need to downsample an image?

**Tags:** algorithm, image-processing, interpolation


---

## 1 answers

### Answer by username_1 on 2016-09-19. Score: 5 | Accepted answer

# Explanation

With a non-linear scale, you apply different weights for each pixel (or whatever unit you are using). You can use Euclidean distance towards the closest pixels in both directions to determine the weights.

# Example

## Normalization

For example, in the image below, the red image is the downsampled image (3x3) and the black image is the original image (4x4). The dots represent the sample points (center of the pixels).

You can align the two images by treating each pixel coordinate as a set of two values (x and y) between 0 and 1 (inclusive). Pixels should not be treated as square, so you should treat each pixel as a point rather than a square (when aligning the two images).

[![Larger and downsampled image](https://i.sstatic.net/dPoxG.png)](https://i.sstatic.net/dPoxG.png)

In the above image, the four corners would all carry the weight of the original image's four corners, for example. The points in the middle would carry different weights depending on the difference between the four neighboring points.

## With numbers:

This is a hypothetical area to sample in larger images (not related to the image above). The distances are between original pixels are normalized from 0 to 1 to simplify the math.

The black dots represent the sample points (pixels) of the original image. The larger red dots represent the sample points of the downsampled image. The red dot in the top left is closer to the black dot in the top left than the other three surrounding dots, so that pixel will have a heavier weight for determining the color of the downsampled pixel.

[![Closeup of sampling region](https://i.sstatic.net/B2wip.png)](https://i.sstatic.net/B2wip.png)

Horizontal distance from:

* Right 2 original (green) pixels = 0.1 pixels
* Left 2 original (green) pixels = 0.9 pixels

Vertical distance from:

* Top 2 original (green) pixels = 0.1 pixels
* Bottom 2 original (green) pixels = 0.9 pixels

Weight for final pixel:
$$weight(0, 0) = (1.0 - 0.1)(1.0 - 0.1) = 0.81$$
$$weight(0, 1) = (1.0 - 0.1)(1.0 - 0.9) = 0.09$$
$$weight(1, 0) = (1.0 - 0.9)(1.0 - 0.1) = 0.09$$
$$weight(1, 1) = (1.0 - 0.9)(1.0 - 0.9) = 0.01$$

Subtract from 1 both the horizontal and vertical distance (because closer pixels should carry more weight). The scale factor in this example is 0.75 in each direction.

# Scaling Up vs. Scaling Down

Scaling up in the same process as scaling down. By normalizing both the original image and the new image to between (0, 0) and (1, 1), you can do either with the same process. As an exercise, in the first figure, if the red points were from the original image and the black points were from the enlarged image, you should be able to see that the process is the same.

> **Comments:**
> **username_2**: @username_0 While an ok answer. It does nothing for dispelling the myth that pixels are squares. You are dealing with point samples (that are not squares). And that resampling is in fact consisting of 2 distinct phases rolled into one, making the datafield continious and then sampling said continious function.  

---

