def robertsFilters(image): """ Apply Roberts filters in X and Y directions Parameters ---------- image: ndarray((h, w, 3)) Returns ------- ndarray((h, w, 3)) """ colourers.info(f'Applying Roberts filter in X and Y directions') Kx = np.array([[1, 0], [0, -1]], np.float32) Ky = np.array([[0, 1], [-1, 0]], np.float32) Ix = optimizedConv2D(image, Kx) Iy = optimizedConv2D(image, Ky) G = np.hypot(Ix, Iy) G = G / G.max() * 255 theta = np.arctan2(Iy, Ix) return (G, theta)
def kirschFilters(image): """ Apply Kirsch filters in X and Y directions Parameters ---------- image: ndarray((h, w, 3)) Returns ------- ndarray((h, w, 3)) """ colourers.info(f'Applying Kirsch filter in X and Y directions') Kx = np.array([[-3, -3, 5], [-3, 0, 5], [-3, -3, 5]], np.float32) Ky = np.array([[-3, -3, -3], [-3, 0, -3], [5, 5, 5]], np.float32) Ix = optimizedConv2D(image, Kx) Iy = optimizedConv2D(image, Ky) G = np.hypot(Ix, Iy) G = G / G.max() * 255 theta = np.arctan2(Iy, Ix) return (G, theta)
def gaborFilter(image, kernel): """ Performing a gabor filter Parameters ---------- image: ndarray((h, w, 3)) Returns ------- ndarray((h, w, 3)) """ return optimizedConv2D(image, kernel).astype(np.uint8)
def averageBlur(image): """ Apply an average blur on the image Parameters ---------- image: ndarray((h, w, 3)) Returns ------- ndarray((h, w, 3)) """ averageBlurKernel = np.ones((5, 5)) * 1.0 / 25 return optimizedConv2D(image, averageBlurKernel)
def simpleBlur(image): """ Apply a simple blur on the image Parameters ---------- image: ndarray((h, w, 3)) Returns ------- ndarray((h, w, 3)) """ simpleBlurKernel = np.ones((3, 3)) * 1.0 / 9 return optimizedConv2D(image, kernel=simpleBlurKernel)
def increasedEdgeEnhancement(image): """ Performing an increased edge enhancement filter Parameters ---------- image: ndarray((h, w, 3)) Returns ------- ndarray((h, w, 3)) """ increasedEdgeEnhanceKernel = np.array( [[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]], np.float32) return optimizedConv2D(image, increasedEdgeEnhanceKernel).clip(0, 255)
def sharpen(image): """ Performing a sharpening filter Parameters ---------- image: ndarray((h, w, 3)) Returns ------- ndarray((h, w, 3)) """ increasedEdgeEnhanceKernel = np.array( [[0, -1, 0], [-1, 5, -1], [0, -1, 0]], np.float32) return optimizedConv2D(image, increasedEdgeEnhanceKernel).clip(0, 255)
def unsharp(image): """ Performing an unsharpening filter Parameters ---------- image: ndarray((h, w, 3)) Returns ------- ndarray((h, w, 3)) """ increasedEdgeEnhanceKernel = np.array( [[1, 4, 6, 4, 1], [4, 16, 24, 16, 4], [6, 24, -476, 24, 6], [4, 16, 24, 16, 4], [1, 4, 6, 4, 1]], np.float32) * -1.0 / 256 return optimizedConv2D(image, increasedEdgeEnhanceKernel).clip(0, 255)
def emboss(image): """ Performing an emboss filter Parameters ---------- image: ndarray((h, w, 3)) Returns ------- ndarray((h, w, 3)) """ embossKernel = np.array([ [-2, -1, 0], [-1, 1, 1], [ 0, 1, 2] ]) return optimizedConv2D(image, embossKernel).clip(0, 255)
def kirschEdgeDetection(image, sigma=1, kernelSize=5): """ Apply the Kirsch filter for an edge detection Parameters ---------- image: ndarray((h, w, 3)) sigma: float kernelSize: int Returns ------- ndarray((h, w, 3)) """ smoothedImage = optimizedConv2D(image, gaussianKernel(kernelSize, sigma)) gradientMatrix, thetaMatrix = kirschFilters(smoothedImage) return gradientMatrix
def motionBlur(image): """ Apply a motion blur on the image Parameters ---------- image: ndarray((h, w, 3)) Returns ------- ndarray((h, w, 3)) """ motionBlurKernel = np.array([ [1, 0, 0, 0, 1], [0, 1, 0, 1, 0], [0, 0, 1, 0, 0], [0, 1, 0, 1, 0], [1, 0, 0, 0, 1] ]) * 1.0 / 9 return optimizedConv2D(image, motionBlurKernel)
def gaussianBlur(image): """ Apply a gaussian blur on the image Parameters ---------- image: ndarray((h, w, 3)) Returns ------- ndarray((h, w, 3)) """ gaussianBlurKernel = np.array([ [2, 4, 5, 4, 2], [4, 9, 12, 9, 4], [5, 12, 15, 12, 5], [4, 9, 12, 9, 4], [2, 4, 5, 4, 2] ]) * 1.0 / 159 return optimizedConv2D(image, gaussianBlurKernel)
def cannyEdgeDetection(image, sigma=1, kernelSize=5, weakPix=75, strongPix=255, lowThreshold=0.05, highThreshold=0.15): """ Apply the Canny filter for an edge detection Parameters ---------- image: ndarray((h, w, 3)) sigma: float kernelSize: int weakPix: int strongPix: int lowThreshold: int highThreshold: int Returns ------- ndarray((h, w, 3)) """ smoothedImage = optimizedConv2D(image, gaussianKernel(kernelSize, sigma)) gradientMatrix, thetaMatrix = sobelFilters(smoothedImage) nonMaxImage = nonMaxSuppression(gradientMatrix, thetaMatrix) thresholdImage = threshold(nonMaxImage, lowThresholdRatio=lowThreshold, highThresholdRatio=highThreshold, weakPix=weakPix, strongPix=strongPix) return hysteresis(thresholdImage, weakPixel=weakPix, strongPixel=strongPix)