skip to Main Content

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:WorkProjectscriptstest_imagesblur1.png', mode='L')

input_image2 = ndimage.imread('D:WorkProjectscriptstest_imagesclr1.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:WorkProjectscriptstry_cpbd.py", line 1, in <module>
    import sys, cpbd
  File "D:Program_Files_2Pythonlibsite-packagescpbd__init__.py", line 3, in <module>
    from .compute import compute
  File "D:Program_Files_2Pythonlibsite-packagescpbdcompute.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_2Pythonlibsite-packagesscipymisc__init__.py)

4

Answers


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

    Edit “D:Program_Files_2Pythonlibsite-packagescpbdcompute.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)
    
    Login or Signup to reply.
  2. Since scipy.misc.imread is deprecated since 1.0.0, and removed in 1.2.0, I would use skimage.io.imread instead (which is in most ways a drop-in replacement).

    Edit the code in cpbd/compute.py

    import skimage.io
    
    input_image1 = skimage.io.imread('blur1.png')
    

    cv2 also works (or other options: imageio, PIL, …) but skimage tends to be a bit easier to install/use.

    Login or Signup to reply.
  3. The following steps worked for me:
    Open the compute.py from C:ProgramDataAnaconda3Libsite-packagescpbdcompute.py or wherever you have installed it. You will find the following code:

    from scipy.ndimage import imread
    

    replace it with:

    from skimage.io import imread
    

    If you can’t save the compute.py file, then copy it to desktop, edit it in the above mentioned way and replace the file in C:ProgramDataAnaconda3Libsite-packagescpbdcompute.py with it.

    Login or Signup to reply.
  4. Following the answer from Baj Mile, I did the following and it worked for me.
    opened the cpbdcompute.py file

    commented the line : from scipy.ndimage import imread

    Added the line: import cv2

    Made the following changes to the main section:

    if __name__ == '__main__':  
        #input_image = imread(argv[1], mode='L')  
        input_image=cv2.imread(argv[1])  
        sharpness = compute(input_image)  
        print('CPBD sharpness for %s: %f' % (argv[1], sharpness))
    

    close the compute.py file.

    In the main code:

    import cpbd
    import cv2
    input_image1 = cv2.imread('testimage.jpg')
    input_image1 = cv2.cvtColor(input_image1, cv2.COLOR_BGR2GRAY)
    cpbd.compute(input_image1)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search