[SOLVED] nearest neighbour downsize w/ averaged ties

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: [SOLVED] downsize w/ Point staying aligned w/ nearest t

Post by anthony »

NicolasRobidoux wrote:Here is a really compact solution:

Code: Select all

magick INPUT.IMG -filter Box -define filter:blur=0 -distort Resize WIDTHxHEIGHT OUTPUT.IMG
It will only do what I want when WIDTH divides the width of the input image exactly, and likewise with the HEIGHT, but this is the situation that I care about.
Of course it relies on ImageMagick enlarging the blur just enough to make sure that there is at least one point in the "Box" (which here is a disk).
Well that isn't 'point' (unless I get a different image to you) looks more like a triangle filter (bilinear interpolation)
But then that is what you are getting... blur =0, means support = 0, so no pixels ever get 'hit' and you get a fall back to interpolation type 'point' lookup.

Really Resize with filter point probably should use the new IMv7 operator -interpolative-resize (resize using interpolation only)
But historically filter point (actually a near zero support impulse filter) produces a nearest neighbour interpolation.

ASIDE: I do want to add some other 'unusual' interpolations.

For example a mixed nearest-neighbour and average interpolator. That is if the sample point is 'close' to a pixel, use that, other-wise use average of the nearest pixels.

Another idea is, exact matches uses pixels, otherwise use background color. This can be used to integer 'scale' an image such that the result is much like splicing extra rows and columns of pixels to spread the original pixel. This also would be useful for the unusual EWA Box filter with two small a support.
http://www.imagemagick.org/Usage/resize ... terpolated
uses this interpolation filter in this situation would result in the areas between 'dots' bein the user given background color, rather than the interpolated blended color.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: [SOLVED] downsize w/ Point staying aligned w/ nearest t

Post by NicolasRobidoux »

In my limited testing,

Code: Select all

magick INPUT.IMG -filter triangle -interpolative-resize WIDTHxHEIGHT OUTPUT.IMG
and

Code: Select all

magick INPUT.IMG -filter Box -define filter:blur=0 -distort Resize WIDTHxHEIGHT OUTPUT.IMG
give exactly the same results when "exact integer ratio" downsampling. And the former does not require abusing IM internals.

Thank you Anthony!
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: [SOLVED] downsize w/ Point staying aligned w/ nearest t

Post by NicolasRobidoux »

anthony wrote:...
For example a mixed nearest-neighbour and average interpolator. That is if the sample point is 'close' to a pixel, use that, other-wise use average of the nearest pixels.
...
"Nearest neighbour with ties resolved by averaging" (which, again, is not what you want if you are using nearest neighbour to resize etc without adding new colours) can be described this way.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: [SOLVED] nearest neighbour downsize w/ averaged ties

Post by anthony »

Not exactly ties. more like "Nearest Neighbour, but with averaged 'near-ties'.

That is, using a 'tensor' method, if within 0.25 of the real pixel, use that pixels value, otherwise use average of the two pixels. One pass in X one in Y. So you get spots of real color, surrounded by 50% blended averages.

The other variation would replace the average areas with 'background' color.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply