Search code examples
pythonimagealgorithmmetrics

any existing code/library for image sharpness or blurriness estimation in python?


I would like to find some existing code/library for sharpness/blurriness estimation on normal images. (prefer in Python) I will need to compare the performance of different algorithms later.

I have 10000+ MRI scan images with different "quality"(sharpness/blurriness). I need to write code to filter images with certain "quality"(sharpness/blurriness) which is up to user. Hence, I am trying to research about image sharpness/blurriness estimation on medical images. My supervisor told me there are lots of existing code for sharpness/blurriness estimation on normal images(maybe it is no-reference sharpness metric) on internet. She asked me to search about them and try them on normal images first. Then try to learn about their algorithms. I have searched about this on internet and found some pages which are relevant. However, lots of them are out of date.

For example: On Image sharpness metric page,

Cumulative probability of blur detection (CPBD) https://ivulab.asu.edu/software/quality/cpbd

seems not working anymore. I guess the reason is that "imread" function is removed from new "scipy" library. (please see later code and error message) I think I can try the old version of "scipy" later. However, I would like to find some more currently available code/library about image sharpness/blurriness estimation. Also, my working environment will be in Windows 10 or CentOS-7.

I have tried the following code with CPBD:

import sys, cpbd

from scipy import ndimage

input_image1 = ndimage.imread('D:\Work\Project\scripts\test_images\blur1.png', mode='L')

input_image2 = ndimage.imread('D:\Work\Project\scripts\test_images\clr1.png', mode='L')

print("blurry image sharpness:")
cpbd.compute(input_image1)

print("clear image sharpness:")
cpbd.compute(input_image2)

Error message from Python 3.7 shell (ran in Window 10):

Traceback (most recent call last):
  File "D:\Work\Project\scripts\try_cpbd.py", line 1, in <module>
    import sys, cpbd
  File "D:\Program_Files_2\Python\lib\site-packages\cpbd\__init__.py", line 3, in <module>
    from .compute import compute
  File "D:\Program_Files_2\Python\lib\site-packages\cpbd\compute.py", line 14, in <module>
    from scipy.misc import imread #Original: from scipy.ndimage import imread
ImportError: cannot import name 'imread' from 'scipy.misc' (D:\Program_Files_2\Python\lib\site-packages\scipy\misc\__init__.py)

Solution

  • Seems that cpbd package has not been updated from some time. It worked for me with the following steps:

    Edit "D:\Program_Files_2\Python\lib\site-packages\cpbd\compute.py":

    Comment the last 4 lines starting with:

    #if __name__ == '__main__':
    

    Use the python code:

    import cpbd
    
    import cv2
    
    input_image1 = cv2.imread('blur1.png')
    
    if input_image1 is None:
         print("error opening image")
         exit()
    
    input_image1 = cv2.cvtColor(input_image1, cv2.COLOR_BGR2GRAY)
    
    print("blurry image sharpness:")
    
    cpbd.compute(input_image1)