From 64561d7be4ba282240f411f32dd27a0511ec024a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Sat, 24 Feb 2018 20:21:27 +0100 Subject: [PATCH] Implement checking for loaded image --- imgrate.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/imgrate.py b/imgrate.py index c7548b6..761d61f 100755 --- a/imgrate.py +++ b/imgrate.py @@ -2,15 +2,28 @@ import cv2 import numpy as np 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: def __init__(self, image=None): self.image = image + @image_required def laplacian_variance(self): return cv2.Laplacian(self.image, cv2.CV_64F).var() + @image_required def fdibm(self): F = np.fft.fft2(self.image) Fcentered = np.fft.fftshift(F) @@ -28,4 +41,4 @@ if __name__ == '__main__': image = cv2.imread(args.images[0]) grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) - print(imgrate(grayscale).fdibm()) \ No newline at end of file + print(imgrate(grayscale).laplacian_variance()) \ No newline at end of file