Lab - Image Filtering

Computer Vision (CSCI 4220U)

Faisal Qureshi

Faculty of Science, UOIT

http://vclab.science.uoit.ca


Consider the following python program that loads an image, filters it, and displays the original and the filtered images.

filter.py

# Python 2.7

import argparse
import cv2
import numpy as np
from matplotlib import pyplot as plt

def filter(img):
    # Complete this method according to the tasks listed in the lab handout. 
    return img

def process_img1(imgfile):
    print 'Opening ', imgfile
    img = cv2.imread(imgfile)

    # You should implement your functionality in filter function
    filtered_img = filter(img)

    cv2.imshow('Input image',img)
    cv2.imshow('Filtered image',filtered_img)

    print 'Press any key to proceed'   
    cv2.waitKey(0)
    cv2.destroyAllWindows()


def process_img2(imgfile):
    print 'Opening ', imgfile
    img = cv2.imread(imgfile)

    # You should implement your functionality in filter function
    filtered_img = filter(img)

    # You should implement your functionality in filter function

    plt.subplot(121)
    plt.imshow(img)
    plt.title('Input image')
    plt.xticks([]), plt.yticks([])

    plt.subplot(122)
    plt.imshow(filtered_img)
    plt.title('Filtered image')
    plt.xticks([]), plt.yticks([])

    plt.show()


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='CSCI 4220U Lab 2.')
    parser.add_argument('--use-plotlib', action='store_true', help='If specified uses matplotlib for displaying images.')
    parser.add_argument('imgfile', help='Image file')
    args = parser.parse_args()

    if args.use_plotlib:
        process_img2(args.imgfile)
    else:
        process_img1(args.imgfile)

Tasks

You are asked to complete the following tasks (Don't forget to convert the input image to grayscale first):

  1. Write a method gaussian2d(mu, cov, n) that generates an \(n \times n\) Gaussian kernel with mean mu and covariance cov. Use scipy convolve method to blur the input image using this Gaussian kernel. Display the original image and the blurred image.
  2. Write a \(3 \times 3\) kernel that computes x-derivative of the image. Display the original image and the x-derivative of the image.
  3. Write a \(3 \times 3\) kernel that computes y-derivative of the image. Display the original image and the y-derivative of the image.
  4. Write a method that computes gradient of an image (only magnitude). Display the original image and the gradient image. Gradient is defined as follows: \(\nabla I = \sqrt { \left(\frac{\partial I}{\partial x}\right)^2 + \left(\frac{\partial I}{\partial y}\right)^2 }\).
  5. Write down a \(3 \times 3\) filter that returns a positive value if the average value of the 4-adjacent neighbors is less than the center and a negative value otherwise. Display the original image and the grayscale response.

You need to augment the commandline argument parsing, so that your method can support the --task parameter. So we can run the your code as follows:

$ python filter.py --task=1 test.png

What to submit

Submit filter.py via Blackboard.