-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
ENH: anti-alias down-sampled images #13724
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
timhoffm
merged 1 commit into
matplotlib:master
from
jklymak:enh-antialias-downsampled-images
Jul 23, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Default interpolation for `image` is new "antialiased" option | ||
------------------------------------------------------------- | ||
|
||
Images displayed in Matplotlib previously used nearest-neighbor | ||
interpolation, leading to aliasing effects for downscaling and non-integer | ||
upscaling. | ||
|
||
New default for :rc:`image.interpolation` is the new option "antialiased". | ||
`imshow(A, interpolation='antialiased')` will apply a Hanning filter when | ||
resampling the data in A for display (or saving to file) *if* the upsample | ||
rate is less than a factor of three, and not an integer; downsampled data is | ||
always smoothed at resampling. | ||
|
||
To get the old behavior, set :rc:`interpolation` to the old default "nearest" | ||
(or specify the ``interpolation`` kwarg of `.Axes.imshow`) | ||
|
||
To always get the anti-aliasing behavior, no matter what the up/down sample | ||
rate, set :rc:`interpolation` to "hanning" (or one of the other filters | ||
available. | ||
|
||
Note that the "hanning" filter was chosen because it has only a modest | ||
performance penalty. Anti-aliasing can be improved with other filters. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
""" | ||
================== | ||
Image Antialiasing | ||
================== | ||
|
||
Images are represented by discrete pixels, either on the screen or in an | ||
image file. When data that makes up the image has a different resolution | ||
than its representation on the screen we will see aliasing effects. | ||
|
||
The default image interpolation in Matplotlib is 'antialiased'. This uses a | ||
hanning interpolation for reduced aliasing in most situations. Only when there | ||
is upsampling by a factor of 1, 2 or >=3 is 'nearest' neighbor interpolation | ||
used. | ||
|
||
Other anti-aliasing filters can be specified in `.Axes.imshow` using the | ||
*interpolation* kwarg. | ||
""" | ||
|
||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
############################################################################### | ||
# First we generate an image with varying frequency content: | ||
x = np.arange(500) / 500 - 0.5 | ||
y = np.arange(500) / 500 - 0.5 | ||
|
||
X, Y = np.meshgrid(x, y) | ||
R = np.sqrt(X**2 + Y**2) | ||
f0 = 10 | ||
k = 250 | ||
a = np.sin(np.pi * 2 * (f0 * R + k * R**2 / 2)) | ||
|
||
|
||
############################################################################### | ||
# The following images are subsampled from 1000 data pixels to 604 rendered | ||
# pixels. The Moire patterns in the "nearest" interpolation are caused by the | ||
# high-frequency data being subsampled. The "antialiased" image | ||
# still has some Moire patterns as well, but they are greatly reduced. | ||
fig, axs = plt.subplots(1, 2, figsize=(7, 4), constrained_layout=True) | ||
for n, interp in enumerate(['nearest', 'antialiased']): | ||
im = axs[n].imshow(a, interpolation=interp, cmap='gray') | ||
axs[n].set_title(interp) | ||
plt.show() | ||
|
||
############################################################################### | ||
# Even up-sampling an image will lead to Moire patterns unless the upsample | ||
# is an integer number of pixels. | ||
fig, ax = plt.subplots(1, 1, figsize=(5.3, 5.3)) | ||
ax.set_position([0, 0, 1, 1]) | ||
im = ax.imshow(a, interpolation='nearest', cmap='gray') | ||
plt.show() | ||
|
||
############################################################################### | ||
# The patterns aren't as bad, but still benefit from anti-aliasing | ||
fig, ax = plt.subplots(1, 1, figsize=(5.3, 5.3)) | ||
ax.set_position([0, 0, 1, 1]) | ||
im = ax.imshow(a, interpolation='antialiased', cmap='gray') | ||
plt.show() | ||
|
||
############################################################################### | ||
# If the small Moire patterns in the default "hanning" antialiasing are | ||
# still undesireable, then we can use other filters. | ||
fig, axs = plt.subplots(1, 2, figsize=(7, 4), constrained_layout=True) | ||
for n, interp in enumerate(['hanning', 'lanczos']): | ||
im = axs[n].imshow(a, interpolation=interp, cmap='gray') | ||
axs[n].set_title(interp) | ||
plt.show() | ||
|
||
|
||
############################################################################# | ||
# | ||
# ------------ | ||
# | ||
# References | ||
# """""""""" | ||
# | ||
# The use of the following functions and methods is shown | ||
# in this example: | ||
|
||
import matplotlib | ||
matplotlib.axes.Axes.imshow |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would make sense to add a comment
on every rcParams use that is just there to prevent the creation of new baseline images.
If we have to regenerate the baseline images all at some point anyway, we can easily search for that comment and remove the artificial non-default.