I'm trying to rotate a image some degrees then show it in a window. my idea is to rotate and then show it in a new window with new width and height of window calculated from the old width and height:
new_width = x * cos angle + y * sin angle new_height = y * cos angle + x * sin angle
I was expecting the result to look like below:
but it turns out the result looks like this:
and my code is here:
#!/usr/bin/env python -tt #coding:utf-8 import sys import math import cv2 import numpy as np def rotateImage(image, angel):#parameter angel in degrees if len(image.shape) > 2:#check colorspace shape = image.shape[:2] else: shape = image.shape image_center = tuple(np.array(shape)/2)#rotation center radians = math.radians(angel) x, y = im.shape print 'x =',x print 'y =',y new_x = math.ceil(math.cos(radians)*x + math.sin(radians)*y) new_y = math.ceil(math.sin(radians)*x + math.cos(radians)*y) new_x = int(new_x) new_y = int(new_y) rot_mat = cv2.getRotationMatrix2D(image_center,angel,1.0) print 'rot_mat =', rot_mat result = cv2.warpAffine(image, rot_mat, shape, flags=cv2.INTER_LINEAR) return result, new_x, new_y def show_rotate(im, width, height): # width = width/2 # height = height/2 # win = cv2.cv.NamedWindow('ro_win',cv2.cv.CV_WINDOW_NORMAL) # cv2.cv.ResizeWindow('ro_win', width, height) win = cv2.namedWindow('ro_win') cv2.imshow('ro_win', im) if cv2.waitKey() == '\x1b': cv2.destroyWindow('ro_win') if __name__ == '__main__': try: im = cv2.imread(sys.argv,0) except: print '\n', "Can't open image, OpenCV or file missing." sys.exit() rot, width, height = rotateImage(im, 30.0) print width, height show_rotate(rot, width, height)
There must be some stupid mistakes in my code lead to this problem, but I can not figure it out... and I know my code is not pythonic enough :( ..sorry for that..
Can anyone help me?
As BloodyD's answer said,
cv2.warpAffine doesn't auto-center the transformed image. Instead, it simply transforms each pixel using the transformation matrix. (This could move pixels anywhere in Cartesian space, including out of the original image area.) Then, when you specify the destination image size, it grabs an area of that size, beginning at (0,0), i.e. the upper left of the original frame. Any parts of your transformed image that don't lie in that region will be cut off.
Here's Python code to rotate and scale an image, with the result centered:
def rotateAndScale(img, scaleFactor = 0.5, degreesCCW = 30): (oldY,oldX) = img.shape #note: numpy uses (y,x) convention but most OpenCV functions use (x,y) M = cv2.getRotationMatrix2D(center=(oldX/2,oldY/2), angle=degreesCCW, scale=scaleFactor) #rotate about center of image. #choose a new image size. newX,newY = oldX*scaleFactor,oldY*scaleFactor #include this if you want to prevent corners being cut off r = np.deg2rad(degreesCCW) newX,newY = (abs(np.sin(r)*newY) + abs(np.cos(r)*newX),abs(np.sin(r)*newX) + abs(np.cos(r)*newY)) #the warpAffine function call, below, basically works like this: # 1. apply the M transformation on each pixel of the original image # 2. save everything that falls within the upper-left "dsize" portion of the resulting image. #So I will find the translation that moves the result to the center of that region. (tx,ty) = ((newX-oldX)/2,(newY-oldY)/2) M[0,2] += tx #third column of matrix holds translation, which takes effect after rotation. M[1,2] += ty rotatedImg = cv2.warpAffine(img, M, dsize=(int(newX),int(newY))) return rotatedImg
When you get the rotation matrix like this:
rot_mat = cv2.getRotationMatrix2D(image_center,angel,1.0)
Your "scale" parameter is set to 1.0, so if you use it to transform your image matrix to your result matrix of the same size, it will necessarily be clipped.
You can instead get a rotation matrix like this:
rot_mat = cv2.getRotationMatrix2D(image_center,angel,0.5)
that will both rotate and shrink, leaving room around the edges (you can scale it up first so that you will still end up with a big image).
Also, it looks like you are confusing the numpy and OpenCV conventions for image sizes. OpenCV uses (x, y) for image sizes and point coordinates, while numpy uses (y,x). That is probably why you are going from a portrait to landscape aspect ratio.
I tend to be explicit about it like this:
imageHeight = image.shape imageWidth = image.shape pointcenter = (imageHeight/2, imageWidth/2)
Ultimately, this works fine for me:
def rotateImage(image, angel):#parameter angel in degrees height = image.shape width = image.shape height_big = height * 2 width_big = width * 2 image_big = cv2.resize(image, (width_big, height_big)) image_center = (width_big/2, height_big/2)#rotation center rot_mat = cv2.getRotationMatrix2D(image_center,angel, 0.5) result = cv2.warpAffine(image_big, rot_mat, (width_big, height_big), flags=cv2.INTER_LINEAR) return result
Here is the complete script that I executed. Just cv2.imshow("winname", image) and cv2.waitkey() with no arguments to keep it open:
import cv2 def rotateImage(image, angel):#parameter angel in degrees height = image.shape width = image.shape height_big = height * 2 width_big = width * 2 image_big = cv2.resize(image, (width_big, height_big)) image_center = (width_big/2, height_big/2)#rotation center rot_mat = cv2.getRotationMatrix2D(image_center,angel, 0.5) result = cv2.warpAffine(image_big, rot_mat, (width_big, height_big), flags=cv2.INTER_LINEAR) return result imageOriginal = cv2.imread("/Path/To/Image.jpg") # this was an iPhone image that I wanted to resize to something manageable to view # so I knew beforehand that this is an appropriate size imageOriginal = cv2.resize(imageOriginal, (600,800)) imageRotated= rotateImage(imageOriginal, 45) cv2.imshow("Rotated", imageRotated) cv2.waitKey()
Really not a lot there... And you were definitely right to use
if __name__ == '__main__': if it is a real module that you're working on.