How to rotate whole canvas?
I want to show you output from ndimage
rotation function and cv2
defined to make it easy to explain the problem. So I generate white rectangle on black canvas and rotate it with cv
and then with scipy
:
import cv2
import numpy as np
from scipy import ndimage
def rotate_image(im, angle):
h, w = im.shape[:2]
rot_mat = cv2.getRotationMatrix2D((w/2, h/2), angle, 1.0)
return cv2.warpAffine(im, rot_mat, (w, h), flags=2)
img = np.zeros((300, 200))
img[10:290, 10:190] = 255
cv2.imwrite('img.png', img)
cv2.imwrite('cv2_rot.png', rotate_image(img, -30))
cv2.imwrite('scipy_rot.png', ndimage.rotate(img, -30))
These are the images generated:
- Test image:
cv2
defined rotate function:
ndimage
rotate:
Hopfully it's obvious that I want to make cv2
rotate the image the way ndimage
does - rotate the canvas without loosing original image content.
Does anyone know easy way to do that?
You should see the answer here http://stackoverflow.com/questions/22041699/rotate-an-image-without-cropping-in-opencv-in-c/22042434#22042434