Skip to content
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

Unable to show true whites/blacks in grayscale images due to linear interpolation #6052

Open
user27182 opened this issue May 8, 2024 · 0 comments
Labels
bug Uh-oh! Something isn't working as expected.

Comments

@user27182
Copy link
Contributor

Describe the bug, what's wrong, and what you expected.

If I plot a grayscale image with just three values (0, 127, 255):

import pyvista as pv
import numpy as np
import vtk

im=pv.ImageData(dimensions=(10,10,1))
scalars = np.array(
                [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
                   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
                 127, 127, 127, 127, 127, 127,   0,   0,   0,   0, 127,
                 127, 127, 127, 127, 127,   0,   0,   0,   0, 127, 127,
                 255, 255, 127, 127,   0,   0,   0,   0, 127, 127, 255,
                 255, 127, 127,   0,   0,   0,   0, 127, 127, 127, 127,
                 127, 127,   0,   0,   0,   0, 127, 127, 127, 127, 127,
                 127,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
                   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
                   0], dtype='uint8')
im['gray'] = scalars

plot_kwargs = dict(
    cpos='xy', 
    zoom='tight', 
    show_scalar_bar=True, 
    cmap='gray', 
    clim=[0, 255], 
    interpolate_before_map=False)
im.plot(**plot_kwargs)

I get the following blurred image. I was unable to find a way to disable the linear interpolation with any of the plotting options.
image

The expected behaviour (or perhaps the desired feature) is that white pixels should be shown as white when plotting grayscale images. This can be achieved using a vtkImageActor which has the SetInterpolationTypeToNearest method. This seems to be a common plotting method used in vtk examples with grayscale images (e.g. MedIanComparison):

actor = vtk.vtkImageActor()
actor.GetMapper().SetInputData(im)
actor.GetProperty().SetInterpolationTypeToNearest()
plot = pv.Plotter()
plot.add_actor(actor)
plot.view_xy()
plot.camera.tight()
plot.show()

image

This bug was discovered as part of #6051 . With that PR, it should be possible to pad an image to add a white border around a grayscale image. But, the "white" border will show as a gray border instead when plotting the padded image.

Steps to reproduce the bug.

See above.

System Information

OS : Darwin
            CPU(s) : 8
           Machine : arm64
      Architecture : 64bit
               RAM : 16.0 GiB
       Environment : IPython
       File system : apfs
        GPU Vendor : Apple
      GPU Renderer : Apple M2
       GPU Version : 4.1 Metal - 88
  MathText Support : True
  Python 3.12.2 (v3.12.2:6abddd9f6a, Feb  6 2024, 17:02:06) [Clang 13.0.0
  (clang-1300.0.29.30)]
           pyvista : 0.44.dev0
               vtk : 9.3.0
             numpy : 1.26.4
        matplotlib : 3.8.2
            scooby : 0.9.2
             pooch : 1.8.0
            pillow : 10.2.0
           imageio : 2.34.0
           IPython : 8.21.0
          colorcet : 3.1.0
           cmocean : 3.1.3
        ipywidgets : 8.1.2
             scipy : 1.12.0
              tqdm : 4.66.2
            meshio : 5.3.5
        jupyterlab : 4.1.4
    pytest_pyvista : 0.1.8
             trame : 3.5.3
      trame_client : 2.16.1
      trame_server : 2.17.2
         trame_vtk : 2.8.5
     trame_vuetify : 2.4.3
      nest_asyncio : 1.6.0
--------------------------------------------------------------------------------

Screenshots

No response

@user27182 user27182 added the bug Uh-oh! Something isn't working as expected. label May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Uh-oh! Something isn't working as expected.
Projects
None yet
Development

No branches or pull requests

1 participant