import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

plt.rcParams['figure.figsize'] = (10, 8)


Image restoration

• Image reconstruction
• Fixing damaged images
• Text removing
• Logo removing
• Object removing
• Inpainting
• Reconstructing lost parts of images
• Looking at the non-damaged regions

Let's restore a damaged image

In this exercise, we'll restore an image that has missing parts in it, using the inpaint_biharmonic() function.

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 = resize(defect_image, (512, 512))
defect_image = color.rgba2rgb(defect_image)

# Apply the restoration function to the image using the mask

# Show ther defective image
plot_comparison(defect_image, restored_image, 'Restored image')


Removing logos

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')

# Set the pixels where the logo is to 1

# Apply inpainting to remove the logo
image_logo_removed = inpaint.inpaint_biharmonic(image_with_logo,
multichannel=True)

# Show the original and logo removed images
plot_comparison(image_with_logo, image_logo_removed, 'Image with logo removed')


Noise

• Denoising types
• Total variation (TV)
• Bilateral
• Wavelet denoising
• Non-local means denoising

Let's make some noise!

In this exercise, we'll practice adding noise to a fruit image.

from skimage.util import random_noise

# 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')


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.

from skimage.restoration import denoise_tv_chambolle

# 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')


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.

from skimage.restoration import denoise_bilateral