import numpy as np from scipy import ndimage def decimate_to(shape, image): """Decimate an image to reduce its size if it's too big.""" decimation = np.max(np.ceil(np.array(image.shape, dtype=np.float)[:len(shape)]/np.array(shape))) return image[::int(decimation), ::int(decimation), ...] def sharpness_sum_lap2(rgb_image): """Return an image sharpness metric: sum(laplacian(image)**")""" # image_bw=np.mean(decimate_to((1000,1000), rgb_image),2) image_bw = np.mean(rgb_image, 2) image_lap = ndimage.filters.laplace(image_bw) return np.mean(image_lap.astype(np.float)**4) def sharpness_edge(image): """Return a sharpness metric optimised for vertical lines""" gray = np.mean(image.astype(float), 2) n = 20 edge = np.array([[-1]*n + [1]*n]) return np.sum([np.sum(ndimage.filters.convolve(gray, W)**2) for W in [edge, edge.T]])