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 :
- cv2.imread()
- cv2.imshow()
- cv2.imwrite()
- cv2.vconcat()
- cv2.putText()
Image Manipulations :
- Image Resizing.
- Image croping.
- Image translation.
- 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.