Project

Week1 Image Manipulation rotation operation (easy)

Objective : Perform a One of the image manipulation of the image (Rotation)

Library Used :

  • Opencv
  • numpy

Image manipulation are useful for algorithms , manipulating the image as required for the algorithms.

Capturing the required part of the image.

About in-built Functions :

  • cv2.getRotationMatrix2D((col_cen,row_cen), angle, scale)

1. col_cen, row_cen -> co-ordinates of the pixel , rotate the image with respect to the center point as provided.

2. angle -> Amount of an angle to rotate the image

(+ve) -> Anticlockwise Direction.

(-ve) -> Clockwise Direction.

3. Scale -> Factor by which the image to get scaled up or scaled down.

Use this function we get the matrix , contain the rotation behaviour.

  • cv2.warpAffine(image, M, dsize[,flags])

1. image -> on which image we want to perform the Transformation.

2. M-> Transformation Matrix.

3. dsize -> Size of the output image.

4. flags -> Interpolation Method

Applies the transformation Matrix on the image

Some other functions used :

  1. cv2.imread()
  2. cv2.imshow()
  3. cv2.imwrite()
  4. cv2.vconcat()
  5. cv2.putText()

Image Manipulations :

  1. Image Resizing.
  2. Image croping.
  3. Image translation.
  4. Image Rotating.

Code Snippet :

def image_rotation(image, dimensions=None, angle=None, scale=None, center=None):
   rows,cols = image.shape
   if center == None:
      col_cen = cols/2
      row_cen = rows/2
   else:
      col_cen = center[1]
      row_cen = center[0]
   rotation_matrix = cv2.getRotationMatrix2D((col_cen, row_cen), angle, scale)
   rotated_image = cv2.warpAffine(image, rotation_matrix, (image.shape[1], 
                                  image.shape[0]))
   cv2.putText(rotated_image, text='Roatated Image using opencv',
               org=(100,200),
               fontFace= cv2.FONT_HERSHEY_SIMPLEX,
               fontScale=2, color=(255,0,0),
               thickness=5, lineType=cv2.LINE_AA)
   return rotated_image

Function calling :

# center = (row, column)
# angle = +ve(ACW), -ve(CW)
# scale = Factor
angle = 30
scale = 0.5
rotated_image = image_rotation(image, angle = angle, scale = scale)
display_image('rotated_image', rotated_image)
df = cv2.vconcat([image,rotated_image])
save_image('rotated_image1.JPG',df)

Explanation :

1. Prepare the rotation matrix using the getRotationMatrix2D().

2. Then perform the affine (or) Matrix Transformation on the image.

Manipulated Image :

Here considered the parameters as ACW rotation, 0.5 factor shrinking, image size as the original image

.

Conclusion:

Performed the Rotation operation on the image in both anticlockwise and the clockwise direction.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.