Intensity normalization of image using Python+PIL - Speed issues

前端 未结 2 1135
甜味超标
甜味超标 2021-01-04 23:47

I\'m working on a little problem in my sparetime involving analysis of some images obtained through a microscope. It is a wafer with some stuff here and there, and ultimatel

2条回答
  •  离开以前
    2021-01-05 00:09

    import numpy as np
    from PIL import Image
    
    def normalize(arr):
        """
        Linear normalization
        http://en.wikipedia.org/wiki/Normalization_%28image_processing%29
        """
        arr = arr.astype('float')
        # Do not touch the alpha channel
        for i in range(3):
            minval = arr[...,i].min()
            maxval = arr[...,i].max()
            if minval != maxval:
                arr[...,i] -= minval
                arr[...,i] *= (255.0/(maxval-minval))
        return arr
    
    def demo_normalize():
        img = Image.open(FILENAME).convert('RGBA')
        arr = np.array(img)
        new_img = Image.fromarray(normalize(arr).astype('uint8'),'RGBA')
        new_img.save('/tmp/normalized.png')
    

提交回复
热议问题