def get_derivatives(self, I_x, I_y): """ Get the edges using derivative masks. :I_x: x-component of the input image. :I_y: y-component of the input image. """ if (self.dimension == 0): # Define the 1-D derivative mask. G = np.array([-1, 0, 1]) # Compute the derivatives in both components. I_prime_x = convolve1d(I_x, G, axis=0) I_prime_y = convolve1d(I_y, G, axis=1) elif (self.dimension == 1): # Define the 2-D derivative masks. G_x = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]]) G_y = np.array([[-1, -1, -1], [0, 0, 0], [1, 1, 1]]) # Calculate magnitude of the input. I = np.hypot(I_x, I_y).astype(int) # Compute the derivatives in both components. I_prime_x = ndimage.filters.convolve(I, G_x) I_prime_y = ndimage.filters.convolve(I, G_y) else: print("Error: Invalid dimension.") return # Calculate the magnitude. I_prime = np.hypot(I_prime_x, I_prime_y).astype(int) I_prime = I_prime / I_prime.max() * 255 # Calculate the direction. I_theta = np.arctan2(I_prime_y, I_prime_x).astype(int) return (I_prime, I_prime_x, I_prime_y, I_theta)
def gaussian_smoothing(self, I): """ Perform 1-D Gaussian smoothing. :I: Input image. """ # Define the range of the Gaussian distribution. r = np.linspace(-(self.kernel_size // 2), self.kernel_size // 2, self.kernel_size) # Calculate the Gaussian mask. G = [(np.exp((-x**2.0) / (2.0 * (self.sigma**2.0))) * (1 / (np.sqrt(2.0 * np.pi * (self.sigma**2.0))))) for x in r] # Convolve for both axes. I_smooth_x = convolve1d(I, G, axis=0) I_smooth_y = convolve1d(I, G, axis=1) # Combine both components. I_smooth = np.hypot(I_smooth_x, I_smooth_y).astype(int) return (I_smooth, I_smooth_x, I_smooth_y)
def blur_image_kernel1D(image, kernel): blurred = utils.convolve1d(image, kernel, axis=0) blurred = utils.convolve1d(blurred, kernel, axis=1) return blurred
def blur_image(image, radius): k = create_1d_gaussian_kernel(radius) blurred = utils.convolve1d(image, k, axis=0) blurred = utils.convolve1d(blurred, k, axis=1) return blurred