Learn data science and machine learning by building real-world projects on Jovian

Image Processing techniques for Computer Vision

Installing Open CV

In [1]:
!pip install opencv-python
Requirement already satisfied: opencv-python in /Users/aakanksha/miniconda3/lib/python3.7/site-packages (4.1.2.30) Requirement already satisfied: numpy>=1.14.5 in /Users/aakanksha/miniconda3/lib/python3.7/site-packages (from opencv-python) (1.17.2)

Library Imports

In [2]:
%reload_ext autoreload
%autoreload 2
%matplotlib inline

import cv2
import matplotlib.pyplot as plt
import numpy as np
import random
import matplotlib.patches as patches
from pathlib import Path

1. Reading an Image

Open-CV reads image in the BGR (Blue, Green, Red) order, so we change the order of channels to RGB

In [3]:
def read_image(path):
    im = cv2.imread(str(path))
    return cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
In [4]:
im = read_image('./pikachu_raichu.jpeg')
plt.imshow(im)
Out[4]:
<matplotlib.image.AxesImage at 0x1191fa320>
Notebook Image
In [5]:
im.shape
Out[5]:
(177, 284, 3)

2. Cropping an image

Center Crop
In [6]:
def center_crop(im, crop_pixels = 50):
    return im[crop_pixels:im.shape[0] - crop_pixels, crop_pixels:im.shape[1] - crop_pixels]
In [7]:
cropped = center_crop(im)
plt.imshow(cropped)
Out[7]:
<matplotlib.image.AxesImage at 0x1193517f0>
Notebook Image
Random Crop
In [8]:
def random_crop(im, row_crop_pix = 20):
    rows, cols, _ = im.shape
    cols_crop_pix = round(row_crop_pix*cols/rows)
    row_crop_pix = int(np.floor(2*random.uniform(0, 1)*row_crop_pix))
    cols_crop_pix = int(np.floor(2*random.uniform(0, 1)*cols_crop_pix).astype(int))
    return im[row_crop_pix:rows - row_crop_pix, cols_crop_pix:cols - cols_crop_pix]
In [9]:
cropped = random_crop(im)
plt.imshow(cropped)
Out[9]:
<matplotlib.image.AxesImage at 0x1194c8eb8>
Notebook Image
In [10]:
cropped = random_crop(im)
plt.imshow(cropped)
Out[10]:
<matplotlib.image.AxesImage at 0x119577f28>
Notebook Image

3. Resizing

In [11]:
resized = cv2.resize(im, (120,90))
plt.imshow(resized)
Out[11]:
<matplotlib.image.AxesImage at 0x11962f198>
Notebook Image

4. Flip image

Vertical flip
In [12]:
flip_v = np.flip(im,0)
plt.imshow(flip_v)
Out[12]:
<matplotlib.image.AxesImage at 0x11972a278>
Notebook Image
Horizontal flip
In [13]:
flip_h = np.flip(im,1)
plt.imshow(flip_h)
Out[13]:
<matplotlib.image.AxesImage at 0x119838400>
Notebook Image

5. Rotate image

In [14]:
from scipy import ndimage
rotated = ndimage.rotate(im, 85)
plt.imshow(rotated)
Out[14]:
<matplotlib.image.AxesImage at 0x1199a2ba8>
Notebook Image
Affine transformation with border filling
In [15]:
def affine_img(img, angle):
    rows, columns, _ = img.shape
    M = cv2.getRotationMatrix2D((rows/2,columns/2),angle,1) #Calculates an affine matrix of 2D rotation.
    return cv2.warpAffine(img,M,(columns,rows), borderMode=cv2.BORDER_REFLECT, 
                          flags=cv2.INTER_NEAREST)
In [16]:
rotated = affine_img(im, 30)
plt.imshow(rotated)
Out[16]:
<matplotlib.image.AxesImage at 0x119b86c50>