Change language

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.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method