imgrate/imgrate.py

46 lines
1.1 KiB
Python
Executable File

from functools import wraps
import cv2
import numpy as np
def image_required(fun):
@wraps(fun)
def wrapper(self, *args, **kwargs):
if self.image is None:
raise RuntimeError('Property {}.{} requires an image!'
.format(self.__class__.__name__, fun.__name__))
return fun(self, *args, **kwargs)
return wrapper
class imgrate:
def __init__(self, imgfile=None):
self.image = None
if imgfile:
self.load_image(imgfile)
def load_image(self, imgfile):
image = cv2.imread(imgfile)
self.image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
@property
@image_required
def quality(self):
return self.laplacian * self.fdibm
@property
@image_required
def laplacian(self):
return cv2.Laplacian(self.image, cv2.CV_64F).var()
@property
@image_required
def fdibm(self):
F = np.fft.fft2(self.image)
Fcentered = np.fft.fftshift(F)
Fabs = np.abs(Fcentered)
Fmax = Fabs.max()
Th = np.count_nonzero(Fabs > Fmax/1000)
return Th / (F.shape[0] * F.shape[1])