Python 2.7.3 + OpenCV 2.4 после поворота окна не подходит к изображению

Я пытаюсь повернуть изображение на несколько градусов, а затем показать его в окне. моя идея состоит в том, чтобы повернуть, а затем показать его в новом окне с новой шириной и высотой окна, рассчитанными на основе старой ширины и высоты:

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)

Должно быть, какие-то глупые ошибки в моем коде приводят к этой проблеме, но я не могу понять... и я знаю, что мой код недостаточно питонический :(..извините за это..

Может кто-нибудь мне помочь?

Лучший,

медвежонок

13
задан Albin David 17 September 2019 в 14:01
поделиться