appear more dramatic? In this exercise, you'll increase the contrast of a cup of coffee. Something you could share with your friends on social media. Don't forget to use #ImageProcessingDatacamp as hashtag! Even though this is not our Sunday morning coffee cup, you can still apply the same methods to any of our photos. A function called show_image(), that displays an image using Matplotlib, has already been defined. It has the arguments image and title, with title being 'Original' by default. > Import the module that includes the Contrast Limited Adaptive Histogram Equalization (CLAHE) function. > Obtain the image you'll work on, with a cup of coffee in it, from the module that holds all the images for testing purposes. > From the previously imported module, call the function to apply the adaptive equalization method on the original image and set the clip limit to 0.03. """ import sys from skimage.data import coffee from skimage import exposure sys.path.append('./helpers') from settings import nda_import_image, show_image, plot_comparison img_coffee = coffee() img_coffee_adapt_eq = exposure.equalize_adapthist(img_coffee, clip_limit=0.03) plot_comparison(img_coffee, img_coffee_adapt_eq, '#ImageProcessingDatacamp')
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')
The good news is that the algorithm used by scikit-image works very well for enlarging images up to a certain point. In this exercise you'll enlarge an image three times!! You'll do this by rescaling the image of a rocket, that will be loaded from the data module. > Import the module and function needed to enlarge images, you'll do this by rescaling. > Import the data module. > Load the rocket() image from data. > Enlarge the rocket_image so it is 3 times bigger, with the anti aliasing filter applied. > Make sure to set multichannel to True or you risk your session timing out! """ import sys from skimage.transform import rescale from skimage.data import rocket sys.path.append('./helpers') from settings import nda_import_image, show_image, plot_comparison img_rocket = rocket() img_rocket_enlarged = rescale(img_rocket, 3, anti_aliasing=True, multichannel=True) plot_comparison(img_rocket, img_rocket_enlarged, '3-times enlarged image')
"""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')
"""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')
> 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, multichannel=True) plot_comparison(img_cat, img_cat_rotated_rescaled,
"""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')
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')
""" 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)')
""" 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')
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)')
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')