imgrate/imgrate.py

44 lines
1.2 KiB
Python
Raw Normal View History

2018-01-01 15:33:10 +00:00
#!/usr/bin/env python3
import cv2
import numpy as np
2018-01-01 15:33:10 +00:00
from argparse import ArgumentParser
2018-02-24 19:21:27 +00:00
from functools import wraps
def image_required(fun):
@wraps(fun)
def wrapper(self, *args, **kwargs):
if self.image is None:
raise RuntimeError('Method {}.{} requires an image!'
.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, image=None):
self.image = image
2018-02-24 19:21:27 +00:00
@image_required
2018-02-24 19:04:44 +00:00
def laplacian_variance(self):
return cv2.Laplacian(self.image, cv2.CV_64F).var()
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])
2018-01-01 15:33:10 +00:00
if __name__ == '__main__':
ap = ArgumentParser()
ap.add_argument('images', type=str, nargs='+', help='')
args = ap.parse_args()
image = cv2.imread(args.images[0])
grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
2018-01-01 15:33:10 +00:00
2018-02-24 19:21:27 +00:00
print(imgrate(grayscale).laplacian_variance())