import numpy as np
import matplotlib.pyplot as plt


## Jump into filtering

• Filters
• Enhancing an image
• Emphasize or remove features
• Smoothing
• Sharpening
• Edge detection

### Edge detection

In this exercise, you'll detect edges in an image by applying the Sobel filter.

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


### 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.

from skimage.filters import gaussian

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


## Contrast enhancement

• Contrast
• difference between the maximum and minimum pixel intensity in the image
• Enhance contrast
• Contrast stretching
• Histogram equalization

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

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


### Aerial image

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.

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