我正在尝试将图像旋转一定角度然后在窗口中显示它。 我的想法是旋转,然后在新窗口中显示它,并根据旧宽度和高度计算出新的窗口宽度和高度:
new_width = x * cos angle + y * sin angle
new_height = y * cos angle + x * sin angle
我期望结果如下所示:
但结果是这样的:
我的代码在这里:
#!/usr/bin/env python -tt
#coding:utf-8
import sys
import math
import cv2
import numpy as np
def rotateImage(image, angle):#parameter angle 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(angle)
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,angle,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[1],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)
我的代码中一定有一些愚蠢的错误导致了这个问题,但我无法弄清楚...... 而且我知道我的代码不够 pythonic :( ..很抱歉..
谁能帮帮我?
最好的,
熊熊
最佳答案
正如 BloodyD 的回答所说,cv2.warpAffine
不会自动将转换后的图像居中。相反,它只是使用变换矩阵来变换每个像素。 (这可以将像素移动到笛卡尔空间中的任何位置,包括原始图像区域之外。)然后,当您指定目标图像大小时,它会抓取该大小的区域,从 (0,0) 开始,即图像的左上角原始框架。不在该区域中的转换图像的任何部分都将被切断。
这是旋转和缩放图像的 Python 代码,结果居中:
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
关于旋转窗口不适合图像后的 Python 2.7.3 + OpenCV 2.4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11764575/