"""Blurring to reduce noise In this exercise you will reduce the sharpness of an image of a building taken during a London trip, through filtering. > Import the Gaussian filter. > Apply the filter to the building_image, set the multichannel parameter to the correct value. > Show the original building_image and resulting gaussian_image. """ import sys from skimage.filters import gaussian sys.path.append('./helpers') from settings import nda_import_image, show_image, plot_comparison str_building_img_path = './dataset/chapter 2/building_image.jpg' img_building = nda_import_image(str_building_img_path) img_building_gaussian = gaussian(img_building, multichannel=True) plot_comparison(img_building, img_building_gaussian, 'Reduced Sharpness Gaussian')
"""Let's make some noise! In this exercise, we'll practice adding noise to a fruit image. >Import the util module and the random noise function. >Add noise to the image. >Show the original and resulting image. """ import sys from skimage.util import random_noise sys.path.append('./helpers') from settings import nda_import_image, show_image, plot_comparison str_fruits_img_path = './dataset/chapter 3/fruits_square.jpg' img_fruits = nda_import_image(str_fruits_img_path) img_fruits_noisy = random_noise(img_fruits) plot_comparison(img_fruits, img_fruits_noisy, 'Noise')
"""Aerial image (Exposure Equalisation) In this exercise, we will improve the quality of an aerial image of a city. The image has low contrast and therefore we can not distinguish all the elements in it. For this we will use the normal or standard technique of Histogram Equalization. > Import the required module from scikit-image. > Use the histogram equalization function from the module previously imported. > Show the resulting image. """ import sys from skimage import exposure sys.path.append('./helpers') from settings import nda_import_image, show_image, plot_comparison str_aerial_img_path = './dataset/chapter 2/image_aerial.tiff' img_aerial = nda_import_image(str_aerial_img_path) img_aerial_eq = exposure.equalize_hist(img_aerial) plot_comparison(img_aerial, img_aerial_eq, 'Resulting Image')
rescaling and a second time without it, so you can compare them. > Import the module and the rotate and rescale functions > Rotate the image 90 degrees clockwise > Rescale with anti aliasing > Rescale without anti aliasing > Show the resulting images """ import sys from skimage.transform import rescale, rotate sys.path.append('./helpers') from settings import nda_import_image, show_image, plot_comparison str_cat_image_path = './dataset/chapter 2/image_cat.jpg' img_cat = nda_import_image(str_cat_image_path) img_cat_rotated = rotate(img_cat, -90) plot_comparison(img_cat, img_cat_rotated, 'Rotated 90-degrees clockwise') img_cat_rotated_rescaled = rescale(img_cat_rotated, 1 / 4, anti_aliasing=True, multichannel=True) plot_comparison(img_cat, img_cat_rotated_rescaled, 'Rotated 90-degrees clockwise and Rescaled') img_cat_rotated_rescaled_no_aa = rescale(img_cat_rotated, 1 / 4, anti_aliasing=False,
"""Trying other methods (Thresholding) As we saw in the video, not being sure about what thresholding method to use isn't a problem. In fact, scikit-image provides us with a function to check multiple methods and see for ourselves what the best option is. It returns a figure comparing the outputs of different global thresholding methods. You will apply this function to this image, matplotlib.pyplot has been loaded as plt. Remember that you can use try_all_threshold() to try multiple global algorithms. > Import the try all function. > Import the rgb to gray convertor function. > Turn the fruits image to grayscale. > Use the try all method on the grayscale image. """ import sys from skimage.filters import try_all_threshold from skimage.color import rgb2gray from matplotlib import pyplot as plt sys.path.append('./helpers') from settings import nda_import_image, show_image str_fruit_image_path = './dataset/chapter 1/fruits-2.jpg' img_fruit = nda_import_image(str_fruit_image_path) img_fruit_grayscale = rgb2gray(img_fruit) fig, ax = try_all_threshold(img_fruit_grayscale, verbose=False) plt.show()
It's important that you do this proportionally, meaning that these are not distorted. First, you'll try it out for one image so you know what code to test later in the rest of the pictures. Remember that by looking at the shape of the image, you can know its width and height. > Import the module and function to resize. > Set the proportional height and width so it is half the image's height size. > Resize using the calculated proportional height and width. """ import sys from skimage.transform import resize sys.path.append('./helpers') from settings import nda_import_image, show_image, plot_comparison str_dogs_banner_img_path = './dataset/chapter 2/dogs_banner.jpg' img_dogs_banner = nda_import_image(str_dogs_banner_img_path) height = int(img_dogs_banner.shape[0] / 2) width = int(img_dogs_banner.shape[1] / 2) img_dogs_banner_resized = resize(img_dogs_banner, (height, width), anti_aliasing=True) plot_comparison(img_dogs_banner, img_dogs_banner_resized, 'Resized image (half)')
> Import the local threshold function > Set the block size to 35 > Obtain the optimal local thresholding > Obtain the binary image by applying local thresholding > Show the binary image """ import sys sys.path.append('./helpers') from settings import show_image, nda_import_image from skimage.filters import threshold_otsu, threshold_local from skimage import color img_page = nda_import_image('./dataset/chapter 1/page_image.png') def img_thresholding(img, type): show_image(img) img_grayscale = color.rgb2gray(img) if type == 'global': thresh = threshold_otsu(img_grayscale) else: thresh = threshold_local(img_grayscale, block_size=35, offset=10) img_binary = img_grayscale > thresh img_binary2 = img_grayscale < thresh show_image(img_binary) show_image(img_binary2) img_thresholding(img_page, type='global')
""" Reducing noise We have a noisy image that we want to improve by removing the noise in it. Use total variation filter denoising to accomplish this. > Import the denoise_tv_chambolle function from its module. > Apply total variation filter denoising. > Show the original noisy and the resulting denoised image. """ import sys from skimage.restoration import denoise_tv_chambolle from skimage.util import random_noise sys.path.append('./helpers') from settings import nda_import_image, show_image, plot_comparison str_cute_dog_img_noisy_path = './dataset/chapter 3/miny.jpeg' img_cute_dog_noisy = random_noise( nda_import_image(str_cute_dog_img_noisy_path)) img_cute_dog = denoise_tv_chambolle(img_cute_dog_noisy, multichannel=True) plot_comparison(img_cute_dog_noisy, img_cute_dog, 'Denoised (TV Champbolle Method)')
import sys from matplotlib import pyplot as plt sys.path.append('./helpers') from settings import show_image, nda_import_image nda_90s_girl = nda_import_image('./dataset/chapter 1/4.1.01.tiff') nda_red_channel = nda_90s_girl[:, :, 0] plt.hist(nda_red_channel.ravel(), bins=256) plt.title('Red Histogram of 90\'s Girl') plt.show()
In this exercise, you'll detect edges in an image by applying the Sobel filter. Let's see if it spots all the figures in the image. > Import the color module so you can convert the image to grayscale. > Import the sobel() function from filters module. > Make soaps_image grayscale using the appropriate method from the color module. > Apply the sobel edge detection filter on the obtained grayscale image soaps_image_gray. """ import sys from skimage.color import rgb2gray from skimage.filters import sobel sys.path.append('./helpers') from settings import nda_import_image, show_image, plot_comparison str_soap_img_path = './dataset/chapter 2/soap_image.jpg' img_soaps = nda_import_image(str_soap_img_path) # show_image(img_soaps) img_soaps_gs = rgb2gray(img_soaps) # show_image(img_soaps_gs) img_soaps_edge_sobel = sobel(img_soaps_gs) print(type(img_soaps_edge_sobel)) # show_image(img_soaps_edge_sobel) plot_comparison(img_soaps, img_soaps_edge_sobel, 'Edges with Sobel')
colored images to grayscale. For that we will use the rgb2gray() function learned in previous video. Which has already been imported for you. Instructions: > Import the otsu threshold function > Make the image grayscale using rgb2gray > Obtain the optimal threshold value with otsu > Apply thresholding to the image > Show the image """ import sys from skimage.filters import threshold_otsu from skimage import color sys.path.append('./helpers') from settings import show_image, nda_import_image img_chess_pieces = nda_import_image('./dataset/chapter 1/bw.jpg') show_image(img_chess_pieces) img_chess_pieces_gray = color.rgb2gray(img_chess_pieces) thresh = threshold_otsu(img_chess_pieces_gray) img_binary = img_chess_pieces_gray > thresh img_binary2 = img_chess_pieces_gray < thresh show_image(img_binary) show_image(img_binary2)
""" Reducing noise while preserving edges In this exercise, you will reduce the noise in this landscape picture. Since we prefer to preserve the edges in the image, we'll use the bilateral denoising filter. > Import the denoise_bilateral function from its module. > Apply bilateral filter denoising. > Show the original noisy and the resulting denoised image. """ import sys from skimage.restoration import denoise_bilateral sys.path.append('./helpers') from settings import nda_import_image, show_image, plot_comparison str_nature_img_noisy = './dataset/chapter 3/noise-noisy-nature.jpg' img_nature_noisy = nda_import_image(str_nature_img_noisy) img_nature = denoise_bilateral(img_nature_noisy, multichannel=True) plot_comparison(img_nature_noisy, img_nature, 'Denoised (Bilateral, Preserve Edges)')
We'll work on an image from the data module, obtained by data.astronaut(). Some of the pixels have been replaced by 1s using a binary mask, on purpose, to simulate a damaged image. Replacing pixels with 1s turns them totally black. The defective image is saved as an array called defect_image. The mask is a black and white image with patches that have the position of the image bits that have been corrupted. We can apply the restoration function on these areas. Remember that inpainting is the process of reconstructing lost or deteriorated parts of images and videos. > Import the module from restoration > Show the defective image > Apply the restoration function to the image using the mask """ import sys from skimage.restoration import inpaint sys.path.append('./helpers') from settings import nda_import_image, show_image, plot_comparison, get_mask str_damaged_astronaut_path = './dataset/chapter 3/damaged_astronaut.png' img_damaged_astronaut = nda_import_image(str_damaged_astronaut_path) img_restored_astronaut = inpaint.inpaint_biharmonic(img_damaged_astronaut, mask, multichannel=True) plot_comparison(img_damaged_astronaut, img_restored_astronaut, 'Restored Image') # need to elaborate on this one further
"""Improving thresholded image (Dilation) In this exercise, we'll try to reduce the noise of a thresholded image using the dilation morphological operation. This operation, in a way, expands the objects in the image. > Import the module. > Obtain the binarized and dilated image, from the original image world_image. """ import sys from skimage import morphology from skimage.color import rgb2gray from skimage.filters import threshold_otsu sys.path.append('./helpers') from settings import nda_import_image, show_image, plot_comparison str_world_image_binary_path = './dataset/chapter 2/world_image_binary.jpg' img_world = nda_import_image(str_world_image_binary_path) thresh = threshold_otsu(rgb2gray(img_world)) img_world_binary = img_world > thresh img_world_dilated = morphology.binary_dilation(img_world_binary) plot_comparison(img_world, img_world_dilated, 'Dilated Image')
You will create and set the mask to be able to erase the logo by inpainting this area. Remember that when you want to remove an object from an image you can either manually delineate that object or run some image analysis algorithm to find it. > Initialize a mask with the same shape as the image, using np.zeros(). > In the mask, set the region that will be inpainted to 1 . > Apply inpainting to image_with_logo using the mask. """ import sys import numpy as np from skimage.restoration import inpaint sys.path.append('./helpers') from settings import nda_import_image, show_image, plot_comparison str_lake_img_with_logo = './dataset/chapter 3/4.2.06_w_logo_2_2.png' img_lake_with_logo = nda_import_image(str_lake_img_with_logo) mask = np.zeros(img_lake_with_logo.shape[:-1]) print(img_lake_with_logo.shape[:-1]) mask[210:272, 360:425] = 1 img_logo_removed = inpaint.inpaint_biharmonic(img_lake_with_logo, mask, multichannel=True) plot_comparison(img_lake_with_logo, img_logo_removed, 'Removed Logo')
> Import the appropriate thresholding and rgb2gray() functions. > Turn the image to grayscale. > Obtain the optimal thresh. > Obtain the binary image by applying thresholding. """ import sys from skimage.filters import try_all_threshold, threshold_li from skimage.color import rgb2gray from matplotlib import pyplot as plt sys.path.append('./helpers') from settings import nda_import_image, show_image str_tools_image_path = './dataset/chapter 1/shapes52.jpg' img_tools = nda_import_image(str_tools_image_path) def apply_thresholding_test(img): img_grayscale = rgb2gray(img) fig, ax = try_all_threshold(img_grayscale, verbose=False) plt.show() apply_thresholding_test(img_tools) img_tools_grayscale = rgb2gray(img_tools) li_thresh = threshold_li(img_tools_grayscale) img_tools_binary = img_tools_grayscale > li_thresh show_image(img_tools_binary)
import sys import numpy as np sys.path.append('./helpers') from settings import show_image, nda_import_image nda_flipped_horiz_vert_seville = nda_import_image( './dataset/chapter 1/sevilleup(2).jpg') nda_flipped_horiz_seville = np.flipud(nda_flipped_horiz_vert_seville) show_image(nda_flipped_horiz_seville) nda_seville = np.fliplr(nda_flipped_horiz_seville) show_image(nda_seville)
Let's try to improve the definition of this handwritten letter so that it's easier to classify. As we can see it's the letter R, already binary, with with some noise in it. It's already loaded as upper_r_image. Apply the morphological operation that will discard the pixels near the letter boundaries. > Import the module from scikit-image. > Apply the morphological operation for eroding away the boundaries of regions of foreground pixels. """ import sys from skimage import morphology from skimage.color import rgb2gray from skimage.filters import threshold_otsu sys.path.append('./helpers') from settings import nda_import_image, show_image, plot_comparison str_written_r_path = './dataset/chapter 2/r5.png' img_written_r = nda_import_image(str_written_r_path) # Had to convert image to binary before binary_erosion() thresh = threshold_otsu(rgb2gray(img_written_r)) img_written_r_binary = rgb2gray(img_written_r) > thresh img_written_r_eroded = morphology.binary_erosion(img_written_r_binary) plot_comparison(img_written_r, img_written_r_eroded, 'Eroded Image')