旋转窗口不适合图像后的 Python 2.7.3 + OpenCV 2.4

标签 python python-2.7 opencv

我正在尝试将图像旋转一定角度然后在窗口中显示它。 我的想法是旋转,然后在新窗口中显示它,并根据旧宽度和高度计算出新的窗口宽度和高度:

new_width = x * cos angle + y * sin angle
new_height = y * cos angle + x * sin angle

我期望结果如下所示:

enter image description here

但结果是这样的:

enter image description here

我的代码在这里:

#!/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

enter image description here

关于旋转窗口不适合图像后的 Python 2.7.3 + OpenCV 2.4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11764575/

相关文章:

由于子进程,Python 应用程序变得无响应

python - 在 C 中加密/取消加密 Python 脚本

Python: self.assertEqual(a, b, msg) --> 我想要 diff AND msg

java - 具有特定边框颜色和特定背景颜色的裁剪矩形 JAVA

opencv - 用cv:Mat填充一个std::vector

python - 如何为 hdf5 正确使用 Pandas 迭代器?

python - 如何在 Pandas 数据框中设置每日频率?

python - 如何从pytorch中的AlexNet中提取fc7特征作为numpy数组?

python - 如何通过Python脚本向Linux添加用户?

c++ - 参数传递