Detect which image is sharper

后端 未结 5 1472
情话喂你
情话喂你 2020-12-07 18:56

I\'m looking for a way to detect which of two (similar) images is sharper.

I\'m thinking this could be using some measure of overall sharpness and generating a score

5条回答
  •  余生分开走
    2020-12-07 19:58

    As e.g. shown in this Matlab Central page, the sharpness can be estimated by the average gradient magnitude.

    I used this in Python as

    from PIL import Image
    import numpy as np
    
    im = Image.open(filename).convert('L') # to grayscale
    array = np.asarray(im, dtype=np.int32)
    
    gy, gx = np.gradient(array)
    gnorm = np.sqrt(gx**2 + gy**2)
    sharpness = np.average(gnorm)
    

    A similar number can be computed with the simpler numpy.diff instead of numpy.gradient. The resulting array sizes need to be adapted there:

    dx = np.diff(array)[1:,:] # remove the first row
    dy = np.diff(array, axis=0)[:,1:] # remove the first column
    dnorm = np.sqrt(dx**2 + dy**2)
    sharpness = np.average(dnorm)
    

提交回复
热议问题