2012-05-11 99 views
5

Làm cách nào để xoay tất cả các khung hình trong luồng video bằng OpenCV? Tôi đã thử sử dụng mã được cung cấp trong một similar question, nhưng nó không có vẻ làm việc với đối tượng hình ảnh Iplimage trả về cv.RetrieveFrame.Cách xoay video bằng OpenCV

Đây là mã Tôi hiện có:

import cv, cv2 
import numpy as np 

def rotateImage(image, angle): 
    if hasattr(image, 'shape'): 
     image_center = tuple(np.array(image.shape)/2) 
     shape = image.shape 
    elif hasattr(image, 'width') and hasattr(image, 'height'): 
     image_center = (image.width/2, image.height/2) 
     shape = np.array((image.width, image.height)) 
    else: 
     raise Exception, 'Unable to acquire dimensions of image for type %s.' % (type(image),) 
    rot_mat = cv2.getRotationMatrix2D(image_center, angle,1.0) 
    result = cv2.warpAffine(image, rot_mat, shape, flags=cv2.INTER_LINEAR) 
    return result 

cap = cv.CaptureFromCAM(cam_index) 
#cap = cv.CaptureFromFile(path) 
fps = 24 
width = int(cv.GetCaptureProperty(cap, cv.CV_CAP_PROP_FRAME_WIDTH)) 
height = int(cv.GetCaptureProperty(cap, cv.CV_CAP_PROP_FRAME_HEIGHT)) 

fourcc = cv.CV_FOURCC('P','I','M','1') #is a MPEG-1 codec 

writer = cv.CreateVideoWriter('out.avi', fourcc, fps, (width, height), 1) 
max_i = 90 
for i in xrange(max_i): 
    print i,max_i 
    cv.GrabFrame(cap) 
    frame = cv.RetrieveFrame(cap) 
    frame = rotateImage(frame, 180) 
    cv.WriteFrame(writer, frame) 

Nhưng điều này chỉ cung cấp cho các lỗi:

File "test.py", line 43, in <module> 
    frame = rotateImage(frame, 180) 
    File "test_record_room.py", line 26, in rotateImage 
    result = cv2.warpAffine(image, rot_mat, shape, flags=cv2.INTER_LINEAR) 
TypeError: <unknown> is not a numpy array 

Có lẽ vì warpAffine mất một CvMat và không phải là một Iplimage. Theo C++ cheatsheet, chuyển đổi giữa hai là tầm thường, nhưng tôi không thể tìm thấy bất kỳ tài liệu nào về việc thực hiện tương đương trong Python. Làm cách nào để chuyển đổi một Iplimage thành Mat bằng Python?

Trả lời

10

Nếu bạn chỉ sau một vòng quay 180 độ, bạn có thể sử dụng Flip trên cả hai trục,

thay thế:

frame = rotateImage(frame, 180) 

với:

cv.Flip(frame, flipMode=-1) 

Đây là ' tại chỗ ', vì vậy nó nhanh chóng, và bạn sẽ không cần chức năng rotateImage nữa :)

Ví dụ:

import cv 
orig = cv.LoadImage("rot.png") 
cv.Flip(orig, flipMode=-1) 
cv.ShowImage('180_rotation', orig) 
cv.WaitKey(0) 

này: enter image description here trở nên, điều này: enter image description here

2

Bạn không cần sử dụng warpAffine(), hãy xem transpose()flip().

This post minh họa cách xoay hình ảnh 90 độ.

2

Thông qua thử và lỗi, cuối cùng tôi đã phát hiện ra giải pháp.

import cv, cv2 
import numpy as np 

def rotateImage(image, angle): 
    image0 = image 
    if hasattr(image, 'shape'): 
     image_center = tuple(np.array(image.shape)/2) 
     shape = tuple(image.shape) 
    elif hasattr(image, 'width') and hasattr(image, 'height'): 
     image_center = tuple(np.array((image.width/2, image.height/2))) 
     shape = (image.width, image.height) 
    else: 
     raise Exception, 'Unable to acquire dimensions of image for type %s.' % (type(image),) 
    rot_mat = cv2.getRotationMatrix2D(image_center, angle,1.0) 
    image = np.asarray(image[:,:]) 

    rotated_image = cv2.warpAffine(image, rot_mat, shape, flags=cv2.INTER_LINEAR) 

    # Copy the rotated data back into the original image object. 
    cv.SetData(image0, rotated_image.tostring()) 

    return image0 
Các vấn đề liên quan