Using numpy to efficiently convert 16-bit image data to 8 bit for display, with intensity scaling

前端 未结 5 1987
耶瑟儿~
耶瑟儿~ 2020-12-15 09:55

I frequently convert 16-bit grayscale image data to 8-bit image data for display. It\'s almost always useful to adjust the minimum and maximum display intensity to highlight

5条回答
  •  青春惊慌失措
    2020-12-15 10:05

    I would avoid casting the image to float, you could do something like:

    import numpy as np
    
    def display(image, display_min, display_max):
        # Here I set copy=True in order to ensure the original image is not
        # modified. If you don't mind modifying the original image, you can
        # set copy=False or skip this step.
        image = np.array(image, copy=True)
    
        image.clip(display_min, display_max, out=image)
        image -= display_min
        image //= (display_min - display_max + 1) / 256.
        image = image.astype(np.uint8)
        # Display image
    

    Here an optional copy of the image is made in it's native data type and an 8 bit copy is make on the last line.

提交回复
热议问题