From 36310909ee02860ed966968f60953d67b6156a2c Mon Sep 17 00:00:00 2001 From: oscarpocock Date: Tue, 5 Apr 2022 17:21:54 +0100 Subject: [PATCH] Implemented basic focus detection, closes #5 --- src/focusdetection/__init__.py | 0 src/focusdetection/focusdetection.py | 49 ++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/focusdetection/__init__.py create mode 100644 src/focusdetection/focusdetection.py diff --git a/src/focusdetection/__init__.py b/src/focusdetection/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/focusdetection/focusdetection.py b/src/focusdetection/focusdetection.py new file mode 100644 index 0000000..af3696d --- /dev/null +++ b/src/focusdetection/focusdetection.py @@ -0,0 +1,49 @@ +import argparse +import os +from os.path import abspath +import cv2 +import pandas +import numpy +import matplotlib.pyplot as plt + +#parser = argparse.ArgumentParser() +#parser.add_argument('images', type=os.path.abspath, metavar='image-location', nargs='+', +# help='path(s) to input image(s)') +#args = vars(parser.parse_args()) +# +#imagePaths = args["images"] + +def laplacian_variance(image): + return cv2.Laplacian(image, cv2.CV_64F).var() + +def fast_fourier(image, size=60, thresh=10): + (h, w) = image.shape + (xCenter, yCenter) = (int(w/ 2), int(h/2)) + fft = numpy.fft.fft2(image) + fftShift = numpy.fft.fftshift(fft) + fftShift[yCenter -size:yCenter + size, xCenter - size:xCenter + size] = 0 + fftShift = numpy.fft.ifftshift(fftShift) + recon = numpy.fft.ifft2(fftShift) + magnitude = 20 * numpy.log(numpy.abs(recon)) + mean = numpy.mean(magnitude) + return (mean, mean <= thresh) + +def laplacian_variance_method(): + column_labels = ['Path', 'Laplacian Variance'] + df = pandas.DataFrame(columns = column_labels) + index = 0 + + for imagePath in imagePaths: + index += 1 + if index % 10 == 0: + print("Processing image " + str(index) + "/" + str(len(imagePaths))) + image = cv2.imread(imagePath) + image_grey = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) + var = laplacian_variance(image_grey) + df.loc[len(df.index)] = [imagePath, var] + + mean_lap_var = df["Laplacian Variance"].mean() + df_not_blurred = df[df['Laplacian Variance']>mean_lap_var] + df_blurred = df[df['Laplacian Variance']<=mean_lap_var] + +#def fast_fourier_transform_method():