0

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)

4 Answers4

0

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)
Baj Mile
  • 651
  • 1
  • 7
  • 15
  • Got error:Traceback (most recent call last): File "D:\Work\Project\scripts\try_cpbd_with_opencv.py", line 1, in import cpbd File "D:\Program_Files_2\Python\lib\site-packages\cpbd\__init__.py", line 3, in from .compute import compute File "D:\Program_Files_2\Python\lib\site-packages\cpbd\compute.py", line 14, in from scipy.ndimage import imread #Original: from scipy.ndimage import imread ImportError: cannot import name 'imread' from 'scipy.ndimage' (D:\Program_Files_2\Python\lib\site-packages\scipy\ndimage\__init__.py) >>> – Killed_by_Bugs Oct 20 '19 at 20:43
  • later, I commented 14th line of cpbd. Then, I got following error: Traceback (most recent call last): File "D:\Work\Project\scripts\try_cpbd_with_opencv.py", line 5, in input_image1 = cv2.cvtColor(input_image1, cv2.COLOR_BGR2GRAY) cv2.error: OpenCV(4.1.1) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor' – Killed_by_Bugs Oct 21 '19 at 00:48
  • this error shows that the image file was not opened, try executing the script above in the same folder as the image, it worked perfectly for me. – Baj Mile Oct 21 '19 at 09:11
  • thx, I have edited "D:\Program_Files_2\Python\lib\site-packages\cpbd\compute.py" to "D:\\Program_Files_2\\Python\\lib\\site-packages\\cpbd\\compute.py" Then, it works. However, this code doesn't work well. Sometimes, the clearer image has lower sharpness outcome. Maybe, I need to try other algorithm. – Killed_by_Bugs Oct 21 '19 at 11:56
  • Hmm, I have not tested it extensively but with the few images I tested it worked well, make sure that you covert the images to grayscale not to black-white. With black-white it will not work well. – Baj Mile Oct 21 '19 at 16:46
0

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.

Alex I
  • 18,105
  • 7
  • 72
  • 135
0

The following steps worked for me: Open the compute.py from C:\ProgramData\Anaconda3\Lib\site-packages\cpbd\compute.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:\ProgramData\Anaconda3\Lib\site-packages\cpbd\compute.py with it.

Panagiotis Simakis
  • 999
  • 16
  • 33
0

Following the answer from Baj Mile, I did the following and it worked for me. opened the cpbd\compute.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)