Implement checking for loaded image

This commit is contained in:
Kristóf Tóth 2018-02-24 20:21:27 +01:00
parent 395729aca1
commit 64561d7be4

View File

@ -2,15 +2,28 @@
import cv2 import cv2
import numpy as np import numpy as np
from argparse import ArgumentParser from argparse import ArgumentParser
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
class imgrate: class imgrate:
def __init__(self, image=None): def __init__(self, image=None):
self.image = image self.image = image
@image_required
def laplacian_variance(self): def laplacian_variance(self):
return cv2.Laplacian(self.image, cv2.CV_64F).var() return cv2.Laplacian(self.image, cv2.CV_64F).var()
@image_required
def fdibm(self): def fdibm(self):
F = np.fft.fft2(self.image) F = np.fft.fft2(self.image)
Fcentered = np.fft.fftshift(F) Fcentered = np.fft.fftshift(F)
@ -28,4 +41,4 @@ if __name__ == '__main__':
image = cv2.imread(args.images[0]) image = cv2.imread(args.images[0])
grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
print(imgrate(grayscale).fdibm()) print(imgrate(grayscale).laplacian_variance())