Introduction to Computer Vision¶
This notebook introduces some of the image processing algorithms/functions using the OpenCV library
| Function | Description |
|---|---|
| Read Image | Read an image using cv.imread |
| plot_images | Plots two images side by side |
| Convert RGB to grayscale | Converts an RGB image to a grascale image |
| Convert RGB to HSV | Converts an RGB image to an HSV image |
| Corner Detector | Uses an algorithm called Harris corner detector to detect edges and corner |
| Normalize | Normalize the values of the image |
| central_crop | Crops the image with the given image dimensions around the center |
| BGR2RGB | Converts a BGR image to an RGB image |
| resize_shortest_edge | While maintaining the aspect ratio of the image, resizes the shortest edge |
Aren't we doing computer vision throughout this bootcamp? Then why have a dedicated session for computer vision?¶
Computer Vision: Traditional method to teach computer understand images
CNNs: human inspired computing paradigm for computers to understand images
OpenCV ¶
OpenCV is an open source cross platform computer vision library. You will be using OpenCV to get familiar with some of the computer vision functions.
Import numpy, OpenCV and matplotlib to visualize images. We will use a mosaic image to demonstrate the effect of some vision operation to such image
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
How to read an image?¶
img = cv.imread('img/mosaic.jpg')
plt.figure(figsize=(10, 10)), plt.axis("off"), plt.imshow(img);
print(img.shape)
How to plot images next to each other?¶
def plot_images(original_image, processed_image):
plt.figure(figsize=(15, 15))
plt.subplot(121),plt.imshow(original_image),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(processed_image),plt.title('Processed')
plt.xticks([]), plt.yticks([])
plt.show()
How to convert RGB image to Grayscale image?¶
A color image is represented on the RGB color space, however there are many different color spaces. Each of them have a particular purpose. We will explore some of them
A grayscale image has only one channel which represents the amount of light that each pixel contains. One of the main purposes of grayscale images on vision applications is to detect edges on images.
gray = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
# plt.figure(figsize=(10, 10)), plt.axis("off"), plt.imshow(gray, cmap='gray');
plot_images(img, gray)
How to convert RGB image to HSV format?¶
HSV color space represents an image using the channels hue, saturation and value. This color space aligns a bit better to the way human perceives color-making attributes, and in vision application this color space is useful to detect color more accurately.
hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
plot_images(img, hsv)
gray = np.float32(cv.cvtColor(img,cv.COLOR_BGR2GRAY))
harris = cv.cornerHarris(gray,2,3,0.1)
plt.figure(figsize=(10, 10)), plt.axis("off"), plt.imshow(harris, cmap='gray');
How to normalize image?¶
Normalize the image by dividing by 256, then subtracting the mean i.e. 0.5, and then amplifying the values by multiplying by 2.
def normalize(image):
image_norm=image/256.0
image_norm=image_norm-0.5
image_norm=image_norm*2
plot_images(image, image_norm)
return image_norm
norm_image = normalize(img)
How to central crop image?¶
Reduce the size of the image around the center.
def central_crop(image, crop_height, crop_width):
image_height = image.shape[0]
image_width = image.shape[1]
offset_height = (image_height - crop_height) // 2
offset_width = (image_width - crop_width) // 2
image_crop = image[offset_height:offset_height + crop_height, offset_width:
offset_width + crop_width, :]
plot_images(image, image_crop)
return image_crop
cropped_image = central_crop(img, 213, 320)
print("Size of original image: ", img.shape)
print("Size of cropped image: ", cropped_image.shape)
How to convert BGR image to RGB image?¶
def BGR2RGB(image):
B, G, R = cv.split(image)
processed_image = cv.merge([R, G, B])
plot_images(image, processed_image)
return processed_image
rgb_image = BGR2RGB(img)
How to resize the shortest edge of an image?¶
def resize_shortest_edge(image, size):
H, W = image.shape[:2]
if H >= W:
nW = size
nH = int(float(H)/W * size)
else:
nH = size
nW = int(float(W)/H * size)
processed_image = cv.resize(image,(nW,nH))
plot_images(image, processed_image)
return processed_image
processed_image = resize_shortest_edge(img, 200)
print("Size of original image: ", img.shape)
print("Size of processed image: ", processed_image.shape)