Skip to content

imshow alpha interpolation looks wrong #11316

Closed

Description

@fargiolas

Hi, not much of an issue, but it seems that imshow is not interpolating RGBA images in the proper way. There was some discussion about a similar issue in #9906 which ended up on a documentation note:

imshow expects RGB images adopting the straight (unassociated) alpha representation.

If that's the case imshow should still switch to associated colors (premultiply alpha) before interpolation to avoid artefacts like the one below, shouldn't it?

import matplotlib.pyplot as plt
import numpy as np

fig, (axl, axr) = plt.subplots(1, 2)

# full green image
img = np.zeros((5, 5, 4))
img[...,1] = np.ones((5, 5))
# transparent above main diagonal
img[...,3] = np.tril(np.ones((5, 5), dtype=np.uint8))

axl.imshow(img, interpolation="none")
axr.imshow(img, interpolation="bilinear")

plt.show()

bug

Activity

  1. fargiolas commented on May 26, 2018

    @fargiolas
    Author

    it seems something Agg specific, it doesn't happen e.g. with the cairo backend

  2. added this to the milestone on May 29, 2018
  3. tacaswell commented on May 29, 2018

    @tacaswell
    Member

    It looks like it is compositing it into black (0, 0, 0) rather than white (1, 1, 1)....

  4. self-assigned this
    on May 29, 2018
  5. fargiolas commented on May 29, 2018

    @fargiolas
    Author

    @tacaswell maybe my description can be a bit misleading. Black fringing is a common artifact when some part of a graphic pipeline uses premultiplied alpha while others don't, but I'm not entirely sure that's what's happening here.

  6. tacaswell commented on May 29, 2018

    @tacaswell
    Member

    else:
    # Always convert to RGBA, even if only RGB input
    if A.shape[2] == 3:
    A = _rgb_to_rgba(A)
    elif A.shape[2] != 4:
    raise ValueError("Invalid dimensions, got %s" % (A.shape,))
    output = np.zeros((out_height, out_width, 4), dtype=A.dtype)
    alpha = self.get_alpha()
    if alpha is None:
    alpha = 1.0
    _image.resample(
    A, output, t, _interpd_[self.get_interpolation()],
    self.get_resample(), alpha,
    self.get_filternorm(), self.get_filterrad())

    That is where we re-sample RBGA images for rendering. From looking at that I suspect that this is an Agg bug, but we should be hitting this code path in cairo as well...

    I suspect the fix here is to change to output starting as (1, 1, 1, 0), which will make it look right more often (as the default patch color is white), but should dig into it a bit more. We may want to re-sample the RGB and A seperatly like we do with the color-mapped data.

  7. added a commit that references this issue on Sep 7, 2018
  8. dabana commented on Sep 7, 2018

    @dabana
    Contributor

    @tacaswell was right about doing the alpha and rgb interpolation separatly.

    a_plot

  9. dabana commented on Sep 7, 2018

    @dabana
    Contributor

    While building a test for this issue, I came across a problem with the interpolation keyword when savefig() is used with the SVG format. See issue #12065.

  10. modified the milestones: v3.1.0, v3.2.0 on Feb 4, 2019
  11. added a commit that references this issue on Feb 26, 2019
  12. modified the milestones: v3.2.0, v3.1.0 on May 17, 2019
  13. tacaswell commented on May 17, 2019

    @tacaswell
    Member

    This was fixed by #12062 / #13559. Thanks @dabana !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions