- Image restoration
- Superpixels & segmentation
- Finding contours
import numpy as np import matplotlib.pyplot as plt import pandas as pd plt.rcParams['figure.figsize'] = (10, 8)
In this exercise, we'll restore an image that has missing parts in it, using the
We'll work on an image with damaged. 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 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.
def show_image(image, title='Image', cmap_type='gray'): plt.imshow(image, cmap=cmap_type) plt.title(title) plt.axis('off') def plot_comparison(img_original, img_filtered, img_title_filtered): fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(10, 8), sharex=True, sharey=True) ax1.imshow(img_original, cmap=plt.cm.gray) ax1.set_title('Original') ax1.axis('off') ax2.imshow(img_filtered, cmap=plt.cm.gray) ax2.set_title(img_title_filtered) ax2.axis('off')
from skimage.restoration import inpaint from skimage.transform import resize from skimage import color defect_image = plt.imread('./dataset/damaged_astronaut.png') defect_image = resize(defect_image, (512, 512)) defect_image = color.rgba2rgb(defect_image) mask = pd.read_csv('./dataset/astronaut_mask.csv').to_numpy() # Apply the restoration function to the image using the mask restored_image = inpaint.inpaint_biharmonic(defect_image, mask, multichannel=True) # Show ther defective image plot_comparison(defect_image, restored_image, 'Restored image')
As we saw in the video, another use of image restoration is removing objects from an scene. In this exercise, we'll remove the Datacamp logo from an 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.
image_with_logo = plt.imread('./dataset/4.2.06_w_logo_2_2.png') # Initialize the mask mask = np.zeros(image_with_logo.shape[:-1]) # Set the pixels where the logo is to 1 mask[210:272, 360:425] = 1 # Apply inpainting to remove the logo image_logo_removed = inpaint.inpaint_biharmonic(image_with_logo, mask, multichannel=True) # Show the original and logo removed images plot_comparison(image_with_logo, image_logo_removed, 'Image with logo removed')
from skimage.util import random_noise fruit_image = plt.imread('./dataset/fruits_square.jpg') # Add noise to the image noisy_image = random_noise(fruit_image) # Show th original and resulting image plot_comparison(fruit_image, noisy_image, 'Noisy image')
from skimage.restoration import denoise_tv_chambolle noisy_image = plt.imread('./dataset/miny.jpeg') # Apply total variation filter denoising denoised_image = denoise_tv_chambolle(noisy_image, multichannel=True) # Show the noisy and denoised image plot_comparison(noisy_image, denoised_image, 'Denoised Image')
from skimage.restoration import denoise_bilateral landscape_image = plt.imread('./dataset/noise-noisy-nature.jpg') # Apply bilateral filter denoising denoised_image = denoise_bilateral(landscape_image, multichannel=True) # Show original and resulting images plot_comparison(landscape_image, denoised_image, 'Denoised Image')