How to rotate whole canvas?

asked 2014-06-22 16:56:15 -0500

vladan gravatar image

updated 2014-06-22 17:05:32 -0500

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:

  1. Test image:
  2. cv2 defined rotate function:
  3. 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?

edit retag flag offensive close merge delete