Filters, Contrast, Transformation and Morphology
You will learn to detect object shapes using edge detection filters, improve medical images with contrast enhancement and even enlarge pictures to five times its original size! You will also apply morphology to make thresholding more accurate when segmenting images and go to the next level of processing images with Python. This is the Summary of lecture "Image Processing in Python", via datacamp.
import numpy as np
import matplotlib.pyplot as plt
def show_image(image, title='Image', cmap_type='gray'):
plt.imshow(image, cmap=cmap_type)
plt.title(title)
plt.axis('off')
soaps_image = plt.imread('./dataset/soap_image.jpg')
from skimage import color
from skimage.filters import sobel
# Make the image grayscale
soaps_image_gray = color.rgb2gray(soaps_image)
# apply edge detection filters
edge_sobel = sobel(soaps_image_gray)
# Show original image
show_image(soaps_image, 'Original')
show_image(edge_sobel, 'Edges with Sobel')
from skimage.filters import gaussian
building_image = plt.imread('./dataset/building_image.jpg')
# Apply filter
gaussian_image = gaussian(building_image, multichannel=True)
# Show the original image
show_image(building_image, 'Original')
show_image(gaussian_image, 'Reduced sharpness Gaussian')
Medical images
You are trying to improve the tools of a hospital by pre-processing the X-ray images so that doctors have a higher chance of spotting relevant details. You'll test our code on a chest X-ray image from the National Institutes of Health Chest X-Ray Dataset
First, you'll check the histogram of the image and then apply standard histogram equalization to improve the contrast.
from skimage import exposure
chest_xray_image = plt.imread('./dataset/chest_xray_image.png')
# Show original x-ray image and its histogram
show_image(chest_xray_image, 'Original x-ray')
plt.title('Histogram of image')
plt.hist(chest_xray_image.ravel(), bins=256);
xray_image_eq = exposure.equalize_hist(chest_xray_image)
# Show the resulting image
show_image(xray_image_eq, 'Resulting image')
image_aerial = plt.imread('./dataset/image_aerial.png')
# Use histogram equalization to improve the contrast
image_eq = exposure.equalize_hist(image_aerial)
# Show the original image
show_image(image_aerial, 'Original')
show_image(image_eq, 'Resulting image')
Let's add some impact and contrast
Have you ever wanted to enhance the contrast of your photos so that they appear more dramatic?
In this exercise, you'll increase the contrast of a cup of coffee.
Even though this is not our Sunday morning coffee cup, you can still apply the same methods to any of our photos.
from skimage import data
# Load the image
original_image = data.coffee()
# Apply the adaptive equalization on the original image
adapthist_eq_image = exposure.equalize_adapthist(original_image, clip_limit=0.03)
# Compare the original image
show_image(original_image)