×

Image Transformations in OpenCV

OpenCV provides pre-defined image Transformation methods such as

cv2.COLOR_BGR2GRAY

In a Grayscale image, all the colors red, blue, green are displayed as shades of gray. If we compare any colored image, less information needs to be displayed for each pixel in a gray image whereas we display different information about the color of the pixel in a colored image.

Thus its a better practice to handle less information and have better computation power and use gray images over any other colored image.

We can convert a BGR image into Gray scale image using cv2.COLOR_BGR2GRAY().

import cv2
image = cv2.imread('C:\images\publicplace.jpg')
new_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("new", new_image)
Image 11

cv2.GaussianBlur()

Blurring makes an image less visible and more precise edges are only visible. While training a model in Image processing we apply blur effect to reduce the edges and decrease the processing time of the model.

Thus blurring effect helps to increase the computation capability such that the model mainly focuses on the major operations.

We can use cv2.GaussianBlur() to apply image blur effect and we pass an image, kernel size(increases blur effect), border type(specifies the image boundaries when kernel is applies on image borders.)

import cv2
image = cv2.imread('C:\images\publicplace.jpg')
blur_image = cv2.GaussianBlur(image, (5, 5), cv2.BORDER_DEFAULT)
cv2.imshow("original", image)
cv2.imshow("Blur", blur_image)
Image 12

cv2.Canny()

cv2.Canny() returns all the edges present in the image. This will be more useful in object detection process. we can reduce the edges present by blurring the image and later apply the canny effect.

cv2.Canny() takes an image and the aperture size(min and max values) as parameters.

import cv2
image = cv2.imread('C:\Users\Varun\OneDrive\Desktop\images\publicplace.jpg')
canny = cv2.Canny(image, 150, 190)
cv2.imshow("original", image)
cv2.imshow("Edges", canny)

Output :-

Image 13

cv2.dilate()

The cv2.dilate() applies more thickness to the edges thus it will be more useful when we require to highlight a particular area.

To understand the uses of cv2.dilate(), first we need to blur the image thus removing unnecessary edges and passing the blur image into cv2.Canny we obtain only edges present in blur image

Finally dilating the Canny image using cv2.dilate() gives us thick edges.

we can pass the image , kernel size, and the iterations for increasing the thickness of the dilating.

import cv2
image = cv2.imread('C:\images\publicplace.jpg')
blur_image = cv2.GaussianBlur(image, (5, 5), cv2.BORDER_DEFAULT)
cv2.imshow("original", image)
canny = cv2.Canny(blur_image, 150, 190)
dilate=cv2.dilate(canny, (6,6), iterations=4)
cv2.imshow("dilated", dilate)
Image 14

cv2.erode()

Erode function is exactly the opposite of the dilate method.

As we know dilate method adds more dilation to the image, that is more thickness to the edges in a image, Erode reduces the thickness of the image.

We can pass the same dilated image to observe the changes done by cv2.erode()

import cv2

image = cv2.imread('C:\images\publicplace.jpg')
blur_image = cv2.GaussianBlur(image, (5, 5), cv2.BORDER_DEFAULT)
canny = cv2.Canny(blur_image, 150, 190)
dilated = cv2.dilate(canny, (6,6), iterations = 4)
cv2.imshow("dilated", dilated)
eroded = cv2.erode(dilated, (6,6), iterations = 4)
cv2.imshow("erode", eroded)

Output:-

As we can see Eroding process reduces the thickness in the image and returns it. The left image is the dilated image and right one is the eroded image.

Image 20

Addition operation on Images 

To add two images we can use cv2.add() method and it returns an two images overlapped over one another, this operations combines the pixels of both the images and display them in a single image.

While adding two images both images should have the same depth and type.

  • cv2.add() takes image1 and image2 as parameters
  • checking the depth of the images.
print(image1.shape, image2.shape
Output :- (480, 640, 3) (640, 640, 3)
  • Increasing the dimensions of the spects.jpg using cv2.resize()
dimensions = (int(image1.shape[0])+160,int(image1.shape[1]))
resize_image = cv2.resize(image1, dimensions, interpolation=cv2.INTER_LINEAR)
import cv2
image1 = cv2.imread('C:\images\spects.jpg')
miage2 = cv2.imread('C:\images\chairs.jpg')
dimensions = (int(image1.shape[0])+ 160,int(image.shape[1]))
resize_image = cv2.resize(image1, dimensions, interpolation=cv2.INTER_LINEAR)
pic = cv2.add(resized_image, image2)
cv2.imshow('frame', pic)

Output:- 

Blending operation on Images

Blending is also referred to as adding two images but with customized features. We can set the transparency of the image accordingly.

Blend two images, by adding some weight to their transparency and combining them. Make sure the images are of the same depth and type, if not apply cv2.resize() to match the dimensions.

Formula for blending is :-

dst = α⋅img1 + β⋅img2 + γ

Here α, β are the weights we add for transparency to the image, and γ is set to ‘0’.

import cv2
image1 = cv2.imread('C:\images\spects.jpg')
miage2 = cv2.imread('C:\images\chairs.jpg')
pic = cv2.addWeighted(image1, 0.5, image2, 0.5, 0)
cv2.imshow('frame', pic)

Output:-

Image Translation

Image translation refers to shifting an image along the x-axis and y-axis. 

Affine transformation is a  geometrical transformation without disturbing the properties of the subject.

cv2.warpaffine() applies an affine transformation to images thus lines and parallelism are preserved even after image translation. The arguments we pass are image, transition matrix, image dimensions. Thus using cv2.warpAffine we can apply several operations on images such as image translation, Rotation.

The process is simple and takes 2 – 3 lines of code. It’s as simple as that.

We define translation() function for this purpose.

import cv2
import numpy as np
image = cv2.imread('C:\images\publicplace.jpg')
def translation(img, x, y):
    trans_m = np.float32([[1, 0, x], [0, 1, y]])
    dimensions = (img.shape[1], img.shape[0])
    return cv2.warpAffine(img, trans_m, dimensions)
trans_image = translation(image, 20, 30)
cv2.imshow('translate', trans_image)
cv2.imshow(‘original’, image)

Output:- 

Image Rotation

For image rotation, we use cv2.getRotationMatrix2D() to return the transition matrix(rotated image matrix) and then pass it to cv2.warAffine() for applying the rotation.

cv2.getRotationMatrix2D((centre), Angle, scaling_factor) takes the parameters such as 

  • Centre of the image used for rotating the image.
  • The angle of Rotation(Image is rotated by this angle)
    • “+”ve rotates the image in the anti-clockwise direction.
    • “-”ve rotates the image in a clockwise direction
  • Scaling factor (we set it for 1 since don’t need to change the size of the image)
import cv2
image = cv2.imread('C:\images\publicplace.jpg')
rows,columns = image.shape[:2]
matrix = cv2.getRotationMatrix2D((rows/2, columns/2), 90, 1.0)
rotated = cv2.warpAffine(image, matrix, (rows, columns))
cv2.imshow('original',image)
cv2.imshow('rotate',rotated)
Image 16

Flip Image

We can Flip an image using cv2.flip(img, flip_code).

  • Flip_code set to 0, flips image by x-axis returns an inverted image.
  • Flip_code set to 1, flips image by y-axis returns a mirror image.
  • Flip_code set to -1, flips image by x-axis & y-axis returns an inverted mirror image.
import cv2
image = cv2.imread('C:\images\publicplace.jpg')
flip = cv2.flip(image,0)
cv2.imshow("flipped", flip)

Output:- 

Crop an Image

Cropping an image refers to slicing an area of image without affecting the original image.

For cropping an image we can pass the coordinates of our area of interest and directly slice it.

Since the image is stored in the form of a NumPy array we need to pass coordinates accordingly.

We can slice the image using image[start Y: end Y, start X: end X]

import cv2
image = cv2.imread('C:\images\publicplace.jpg')
crop = image[10:300, 20:320]
cv2.imshow('crop', crop)