I am trying to modify projection of image by manipulating pixel values.
To do this, I need to reduce the size of different portions of the image by different fractions.
As an example, if the image width is 1000 pixels, I may need to reduce the pixel numbers 600 - 800 (200 pixels) to 150 pixels in the final image, and pixel numbers 800 - 1000 (200 pixels) to 100 pixels in the final image.
For the second case (reducing 200 pixels to 100 pixels), I can take a mean of 2 pixels at a time to get a pixel in the final image. But how to do the first one (reducing 200 pixels to 150 pixels)?
I am very grateful for your time.
CodePudding user response:
A fairly good quality downsampling can be obtained as follows:
Overlay an w x h grid (desired resolution) over the W x H grid of the original image (available resolution).
A low resolution cell overlaps several higher resolution cells, and you can estimate the color of the cell as a weighted average, based on the areas of the intersections (see figure).
In the case of a reduction factor 0.5, you indeed end up with 2x2 pixel averages. With factors between 0.5 and 1, you overlap four pixels and the weighted average corresponds to bilinear interpolation. For smaller factors, more pixels are involved.
Final notes:
That transformation is separable, meaning that you can implement it in two passes, first horizontally, W x H to w x H, then vertically, w x H to w x h. This simplifies the implementation.
Using these principles, you can even work with a continuously variable reduction factor, by considering the corresponding grid.