Dear developers,
matplotlib offers high quality interpolation filters which give excellent results if you scale up an image. However, for downscaling an image the results are worse. Depending on the precise scaling fine details (e.g., thin horizontal lines, single pixel points) disappear and aliasing effects are visible. After studying the source and the docs for Agg I figured out how to improve this. Agg provides all possibilities, just use them.
I attached a patch against the current svn version that adds a 'resample' argument to imshow. Additionally, this patch supports a 'image.resample' entry in the rc file. Setting this to false (default), the behaviour is unchanged.
I also attached a simple test script (test_imshow.py) to show the difference between image display with and without resampling. To see the difference it might be necessary to zoom out.
Gregor Thalhammer
resample.diff (8.11 KB)
test_imshow.py (280 Bytes)
On Mon, Jun 9, 2008 at 10:18 AM,
I attached a patch against the current svn version that adds a 'resample'
argument to imshow. Additionally, this patch supports a 'image.resample'
entry in the rc file. Setting this to false (default), the behaviour is
unchanged.
Hi Gregor -- thanks for sending this. It's something I was aware of
in agg but never got around to exposing. I wonder if the
resample=True|False is the right approach. It might be nice for
imshow to have an 'auto' mode to either resample or interpolate
depending on the image dimensions w/ respect to the destination size.
Ie, if we are displaying the full image into a small destination,
'auto' would default to resample. If we zoom in sufficiently, the
image might be best displayed with interpolation. Is this something
you think would be worthwhile and would you like to work on support
for this?
Also,l I notice the patch exposes span_image_resample_rgba_affine but not
span_image_resample_rgba which takes an interpolator template
argument. I know very little about this area, but was this a
conscious choice or one of expediency? Can you explain the rational?
JDH
John Hunter schrieb:
On Mon, Jun 9, 2008 at 10:18 AM,
I attached a patch against the current svn version that adds a 'resample'
argument to imshow. Additionally, this patch supports a 'image.resample'
entry in the rc file. Setting this to false (default), the behaviour is
unchanged.
Hi Gregor -- thanks for sending this. It's something I was aware of
in agg but never got around to exposing. I wonder if the
resample=True|False is the right approach. It might be nice for
imshow to have an 'auto' mode to either resample or interpolate
depending on the image dimensions w/ respect to the destination size.
Ie, if we are displaying the full image into a small destination,
'auto' would default to resample. If we zoom in sufficiently, the
image might be best displayed with interpolation. Is this something
you think would be worthwhile and would you like to work on support
for this?
If I understood it correctly, Agg (i.e., span_image_resample_*) already exactly behaves like you proposed. It resamples the image if the scaling is less than 1, otherwise it interpolates.
Also,l I notice the patch exposes span_image_resample_rgba_affine but not
span_image_resample_rgba which takes an interpolator template
argument. I know very little about this area, but was this a
conscious choice or one of expediency? Can you explain the rational?
I am not an Agg expert. The documentation of Agg is not very exhaustive. I took it from one of the demos. span_image_resample_rgba_affine seems to be faster than span_image_resample_rgba, but it supports affine but no perspective transformations. If I understood it correctly, matplotlib only uses image translation and scaling, not even rotation or shearing. All this is covered by span_image_resample_rgba_affine.
Gregor
On Mon, Jun 9, 2008 at 11:45 AM, Gregor Thalhammer
If I understood it correctly, Agg (i.e., span_image_resample_*) already
exactly behaves like you proposed. It resamples the image if the scaling is
less than 1, otherwise it interpolates.
I am not an Agg expert. The documentation of Agg is not very exhaustive. I
took it from one of the demos. span_image_resample_rgba_affine seems to be
faster than span_image_resample_rgba, but it supports affine but no
perspective transformations. If I understood it correctly, matplotlib only
uses image translation and scaling, not even rotation or shearing. All this
is covered by span_image_resample_rgba_affine.
OK, all this sounds reasonable. I've committed the changes to the
trunk. Thanks for the explanation and the patch!
JDH