Image processing in Python (scaling, rotation, shearing and edge detection)

Let`s start simple, i.e. scaling the image.

Scaling the image : —

The scaling operation increases / decreases the size of the image.

import cv2

import numpy as np

  

FILE_NAME = `volleyball.jpg`

try :

# Read image from disk.

img = cv2.imread (FILE_NAME)

 

  # Get the number of pixels horizontally and vertically.

  (height, width) = img.shape [: 2 ]

  

# Specify the image size along with interpolation methods.

# cv2.INTER_AREA is used for compression while cv2.INTER_CUBIC

# used for scaling.

res = cv2.resize (img, ( int (width / 2 ), int (height / 2 )), interpolation = cv2.INTER_CUBIC)

 

# Write the image back to disk.

cv2.imwrite ( ` result.jpg` , res)

 

except IOError:

  print ( `Error while reading files !!!` )

 

Output:

Image rotation: —
Images can be rotated to any degree clockwise or otherwise. We just need to define a rotation matrix by specifying a rotation point, rotation rate, and scale factor.

import cv2

import numpy as np

  

FILE_NAME = ` volleyball.jpg`

try :

# Read image from disk.

img = cv2.imread (FILE_NAME)

 

# Image form living in pixels.

(rows, cols) = img.shape [: 2 ]

 

# getRotationMatrix2D creates the matrix needed for the transformation.

# We need a matrix to rotate 45 about center degrees without scaling.

M = cv2.getRotationMatrix2D ((cols / 2 , rows / 2 ), 45 , 1 )

res = cv2.warpAffine (img, M, (cols, rows))

 

# Write image back to disk.

cv2.imwrite ( `result.jpg` , res)

except IOError:

  print ( `Error while reading files !!!` )

Output:

Translation from Images: —
To translate an image means to shift it within a given frame of reference.

import cv2

import numpy as np

 

FILE_NAME = `volleyball.jpg`

# Create translation matrix.
# If the shift is (x, y), then the matrix will be
# M = [1 0 x]
# [0 1 year]
# Let`s move to (100, 50).

M = np.float32 ([[ 1  , 0 , 100 ], [ 0 , 1 , 50 ]])

 

try :

 

# Read image from disk.

img = cv2.imread (FILE_NAME)

(rows, cols) = img.shape [: 2 ]

  

  # warpAffine makes an appropriate offset given

# translation matrix.

res = cv2.warpAffine (img, M, (cols, rows))

  

  # Write the image back to disk.

cv2.imwrite ( `result.jpg` , res)

 

except IOError:

print ( `Error while reading files !!!` )

Exit :

Edge detection in the image: —
The image detection process involves detecting sharp edges in the image. This edge detection is important in the context of image recognition or object localization / detection . There are several algorithms for edge detection due to its wide applicability. We will use one such algorithm known as Canny Edge Detection .

import cv2

import numpy as np

 

FILE_NAME = `volleyball.jpg`

try :

# Read image from disk.

img = cv2.imread (FILE_NAME)

 

  Detect tricky edges.

edges = cv2.Canny (img, 100 , 200 )

 

# Write the image back to disk.

cv2.imwrite ( ` result.jpg` , edges )

except IOError:

print ( `Error while reading files !! ! ` )

Output:

Please refer to Github for more details.