Prelab 6 - Image Enhancing

Summary

In this prelab, you get familiarized with two common tasks in image processing: histogram equalization and 2-D convolution.

Downloads

Submission Instruction

Refer to the Submission Instruction page for more information.

Image Overview

Digital images are made up of picture elements, more commonly known as pixels arranged in a rectangular grid. Three frequently encountered image formats are:

Binary image: are 2D arrays which has only two values 1 or 0 where 1 corresponds to white and 0 corresponds to black.

Intensity or grey scale image: are 2D arrays where pixel intensities have a n-bit representation. For example, an 8-bit image has a variation from 0 to 255 where 0 represents black, 255 represents white and intermediate values correspond to gray levels that span the range from black to white.

RGB color image: are 3D arrays that assign three numerical values to each pixel, each value correponds to the red, green, and blue image channel, respectively.

Part 1 - Image Enhancement

One simple image enhancement method is increasing the image brightness. For grayscale images, we could increase the image brightness by adding some constant value to every single pixel of the image. Use the following Python script to load the image cameraman.tif, and implement the imadd() function to increase the brightness of the image.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import numpy as np
from scipy import misc
import matplotlib.pyplot as plt
import copy
%matplotlib inline

# Implement this function
def imadd(pic,brightness=50):
    # Add brightness to each pixel

    return pic

# Read the image
cameraman_origin = misc.imread('cameraman.tif')
# Create a copy of the origina image for us to manipulate
cameraman_bright = copy.deepcopy(cameraman_origin)

# Call imadd to perform enhancement
cameraman_bright = imadd(cameraman_bright,50)

# Show the results
fig_cam_origin = plt.figure(1)
fig_cam_origin.suptitle('Original Image')
plt.imshow(cameraman_origin,cmap='gray',vmin = 0, vmax = 255)
fig_cam_bright = plt.figure(2)
fig_cam_bright.suptitle('Brightened Image')
plt.imshow(cameraman_bright,cmap='gray',vmin = 0, vmax = 255)
plt.show()

Assignment 1

Increase the brightness of 'cameraman.tif' by brightness = 50. Then try increasing brightness by 300. Repeat for image eco.tif.

Question

  • What is the dynamic range (the number of distinct pixel values in an image) of the orginal and the enhanced image?
  • What will happen if we increase brightness by 300.
  • Could you enhance the quality of the image eco.tif by simply increasing its brightness?

Attention

  • cameraman.tif is a grayscale image, with single channel and pixels of type uint8.
  • eco.tif is a grayscale image, with single channel and pixels of type uint16.

Part 2 - Histogram Equalization

Histogram equalization is one of the most commonly used image contrast enhancement technique. The approach is to design a transformation(or colormap) in such a way that the gray values in the output image are uniformly distributed. More detailed explanation can be found here: Histogram Equalization Tutorial

Use the following Python script to load the image 'eco.tif', and apply histogram equalization. You will have the chance to implement this function in lab 6.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import matplotlib.pyplot as plt
import numpy as np
from scipy import misc
from skimage import exposure
import copy
%matplotlib inline


# Read the image
eco_origin = misc.imread('eco.tif')

# Apply Histogram Equalization here!


# Show the results
fig_cam_origin = plt.figure(1)
fig_cam_origin.suptitle('Original Image')
plt.imshow(eco_origin,cmap='gray')
fig_cam_bright = plt.figure(2)
fig_cam_bright.suptitle('HistEq Image')
plt.imshow(eco_histeq,cmap='gray')
plt.show()

Assignment 2

Use the skimage.exposure.equalize_hist() function to process the image 'eco.tif' and show the results. Note that this histeq function will return images with float pixel values between 0 and 1.

Question

Can you improve the result of enhancement by repeating the histogram equalization? Why?

Part 3 - 2-D Convolution

2-D Convolution, or image filtering, is an important method to process the image. The concept is very similar to 1-D convolution that we have been using before. In 1-D convolution, we compute the value of x[t] based on some linear combination of values of range x[t-k:t]; in 2-D convolution, we compute the value of pixel(row,col) based on some linear combination of its neighboring pixels. This linear combination is represented by a kernel.

In image processing, a kernel, convolution matrix, or mask, is a small matrix that we used as filter to process the image. There are all kinds of kernels to serve different purposes, such as gaussian kernel (low-pass filter), sharpening kernel (high-pass filter), etc. More explanation and examples of Kernel

The computation of 2-D convolution is also straightforward, for each pixel, we update its value based on the combination of its neighbouring pixels specified by the kernel. More explanation and examples of 2-D Convolution

Use the following Python script to load the image kitten.png, and use scipy.signal.convolve2d to apply an Gaussian Smoothing Filter to the image. Different from 1-D convolution, we want the filtered image to be the same size as before.

import numpy
from scipy import misc
from scipy import signal
import matplotlib.pyplot as plt

# Gaussian Kernel Following the Descriptiong: 
# http://www.mathworks.com/help/images/ref/fspecial.html
def gengaussian(size=5,sigma=3.0):
    if size%2==0 or size<2:
        print('Size Not Valid')
        return None
    kernel = numpy.zeros((size,size))
    for x in range(size):
        for y in range(size):
            kernel[x][y] = numpy.exp(-((x-(size-1)/2)**2 \
                           +(y-(size-1)/2)**2)/(2*sigma**2))
    kernel = kernel / numpy.sum(kernel)
    return kernel

# Read Image and Display
kitten_origin = misc.imread('kitten.png')
# Create a copy of the origina image for us to manipulate
kitten_blur = copy.deepcopy(kitten_origin)
# Generate Kernel
kernel = gengaussian(5)
# Apply Convolution Here!


# Display Results
fig_kitten_origin = plt.figure(1)
fig_kitten_origin.suptitle('Original Kitten.png', fontsize=14, fontweight='bold')
plt.imshow(kitten_origin,vmin = 0, vmax = 255)
fig_kitten_blur = plt.figure(2)
fig_kitten_blur.suptitle('Blurred Kitten.png', fontsize=14, fontweight='bold')
plt.imshow(kitten_blur,vmin = 0, vmax = 255)
plt.show()

Assignment 3

Use the signal.convolve2d() function to process the image kitten.png and show the results. Change the sigma value of the gaussian kernel and describe its effects.

Attention

kitten.png is a RGB image, with three channels and pixels of type uint8.

Grading

Prelab 6 will be graded as follows:

  • Assignment 1 [0.75 point]

    Plots of brightness enhanced images [0.5 point]

    Short answer question [0.25 point]

  • Assignment 2 [0.75 point]

    Plots of histogram equalized images [0.5 point]

    Short answer question [0.5 point]

  • Assignment 3 [0.5 point]

    Plots of gaussian filtered images with different sigma value [0.5 point]