[np.ndarray, np.bool]: [A binary image] """ # START YOUR CODE HERE ### (You can change anything inside this block) binary_im = np.zeros_like(im, dtype=np.bool) ### END YOUR CODE HERE ### return binary_im if __name__ == "__main__": # NO NEED TO EDIT THE CODE BELOW. verbose = True plt.figure(figsize=(4, 12)) plt.tight_layout() images_to_visualize = [] for i, impath in enumerate(impaths): im = utils.read_im(str(impath)) im_binary = create_binary_image(im) assert im_binary.dtype == np.bool, f"Expected the image to be of dtype np.bool, got {im_binary.dtype}" angles, distances = utils.find_angle(im_binary) angle = 0 if len(angles) > 0: angle = angles[0] * 180 / np.pi print(f"Found angle: {angle:.2f}") hough_im = utils.create_hough_line_image(im, angles, distances) rotated = skimage.transform.rotate(im, angle, cval=im.max()) images_to_visualize.extend([im, im_binary, hough_im, rotated]) image = utils.np_make_image_grid(images_to_visualize, nrow=len(impaths)) utils.save_im("task4d.png", image) plt.imshow(image, cmap="gray")
import matplotlib.pyplot as plt import pathlib import numpy as np import time from utils import read_im, save_im, normalize output_dir = pathlib.Path("image_solutions") output_dir.mkdir(exist_ok=True) im = read_im(pathlib.Path("images", "lake.jpg")) plt.imshow(im) def convolve_im(im, kernel): """ A function that convolves im with kernel Args: im ([type]): [np.array of shape [H, W, 3]] kernel ([type]): [np.array of shape [K, K]] Returns: [type]: [np.array of shape [H, W, 3]. should be same as im] """ assert len(im.shape) == 3 # Rotate kernel cKernel = kernel.copy() kLen = len(kernel) for y in range(kLen): for x in range(kLen): cKernel[y][x] = kernel[kLen - y - 1][kLen - x - 1]
import os import numpy as np import utils import matplotlib.pyplot as plt def magnitude(fft_im): real = fft_im.real imag = fft_im.imag return np.sqrt(real**2 + imag**2) if __name__ == "__main__": # DO NOT CHANGE impath = os.path.join("images", "noisy_moon.png") im = utils.read_im(impath) # START YOUR CODE HERE ### (You can change anything inside this block) # Since the spikes are on a simple horizontal line in the center around [265:275], we # can simply craete a kernel which removes theese values. Non-shifted, [265:275] becomes [0:10] # Note that the middle area should be unaffected. kernel = np.ones(im.shape) spike_height = 4 # Lines are barely visible at < 4 image_center_x = im.shape[1] // 2 image_center_y = im.shape[0] // 2 center_width = 28 # Lines are visible for > 28 kernel[image_center_y - spike_height // 2:image_center_y + spike_height // 2] = 0 kernel[image_center_y - spike_height // 2:image_center_y + spike_height //