Difference between revisions of "Rotation of image"

From Lofaro Lab Wiki
Jump to: navigation, search
Line 2: Line 2:
  
  
import cv2
+
import cv2
import numpy as np
+
import numpy as np
image = cv2.imread('hubo.jpg',0)
+
image = cv2.imread('hubo.jpg',0)
cv2.imshow("Original", image)
+
cv2.imshow("Original", image)
# get the dimensions of the image and calculate the center of the image
+
# get the dimensions of the image and calculate the center of the image
(h, w) = image.shape[:2]
+
(h, w) = image.shape[:2]
center = (w / 2, h / 2)
+
center = (w / 2, h / 2)
# Rotate the image by 45 degrees
+
# Rotate the image by 45 degrees
M = cv2.getRotationMatrix2D(center, 45, 1.0)
+
M = cv2.getRotationMatrix2D(center, 45, 1.0)
rotated = cv2.warpAffine(image, M, (w, h))
+
rotated = cv2.warpAffine(image, M, (w, h))
cv2.imshow("Rotated by 45 Degrees", rotated)
+
cv2.imshow("Rotated by 45 Degrees", rotated)
# Rotate our image by -90 degrees
+
# Rotate our image by -90 degrees
M = cv2.getRotationMatrix2D(center, -90, 1.0)
+
M = cv2.getRotationMatrix2D(center, -90, 1.0)
rotated = cv2.warpAffine(image, M, (w, h))
+
rotated = cv2.warpAffine(image, M, (w, h))
cv2.imshow("Rotated by -90 Degrees", rotated)
+
cv2.imshow("Rotated by -90 Degrees", rotated)
cv2.waitKey(0)
+
cv2.waitKey(0)
cv2.destroyAllWindows()
+
cv2.destroyAllWindows()

Revision as of 23:14, 12 November 2014

We were doing translation where we were shifting our image according to the pixel we want. Rotation of image will rotate the image to some angle let’s say x. so x will represent by how many degrees we want to rotate our image. Let’s explain the concept of rotation using some code.


import cv2
import numpy as np
image = cv2.imread('hubo.jpg',0)
cv2.imshow("Original", image)
# get the dimensions of the image and calculate the center of the image
(h, w) = image.shape[:2]
center = (w / 2, h / 2)
# Rotate the image by 45 degrees
M = cv2.getRotationMatrix2D(center, 45, 1.0)
rotated = cv2.warpAffine(image, M, (w, h))
cv2.imshow("Rotated by 45 Degrees", rotated)
# Rotate our image by -90 degrees
M = cv2.getRotationMatrix2D(center, -90, 1.0)
rotated = cv2.warpAffine(image, M, (w, h))
cv2.imshow("Rotated by -90 Degrees", rotated)
cv2.waitKey(0)
cv2.destroyAllWindows()