imgrate/imgrate.py

46 lines
1.1 KiB
Python
Raw Normal View History

2018-02-24 19:21:27 +00:00
from functools import wraps
2018-04-14 16:26:56 +00:00
import cv2
import numpy as np
2018-02-24 19:21:27 +00:00
2018-02-24 19:30:02 +00:00
2018-02-24 19:21:27 +00:00
def image_required(fun):
@wraps(fun)
def wrapper(self, *args, **kwargs):
if self.image is None:
2018-02-24 19:30:02 +00:00
raise RuntimeError('Property {}.{} requires an image!'
2018-02-24 19:21:27 +00:00
.format(self.__class__.__name__, fun.__name__))
return fun(self, *args, **kwargs)
return wrapper
2018-01-01 15:33:10 +00:00
2018-02-24 19:04:44 +00:00
class imgrate:
def __init__(self, imgfile=None):
self.image = None
2018-04-14 16:26:56 +00:00
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
2018-02-24 19:30:02 +00:00
@property
2018-02-24 19:21:27 +00:00
@image_required
2018-02-24 19:30:02 +00:00
def laplacian(self):
2018-02-24 19:04:44 +00:00
return cv2.Laplacian(self.image, cv2.CV_64F).var()
2018-02-24 19:30:02 +00:00
@property
2018-02-24 19:21:27 +00:00
@image_required
2018-02-24 19:04:44 +00:00
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])