2012-05-11 23 views
5

Come si fa a ruotare tutti i fotogrammi in un flusso video usando OpenCV? Ho provato a utilizzare il codice fornito in un similar question, ma non sembra funzionare con l'oggetto immagine Iplimage restituito cv.RetrieveFrame.Come ruotare un video con OpenCV

Questo è il codice ho attualmente:

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) 

Ma questo solo dà l'errore:

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 

presumibilmente perché warpAffine prende un CvMat e non un Iplimage. Secondo lo C++ cheatsheet, la conversione tra i due è banale, ma non riesco a trovare alcuna documentazione sul fare l'equivalente in Python. Come posso convertire un Iplimage in Mat in Python?

risposta

10

Se sono solo dopo una rotazione di 180 gradi, è possibile utilizzare Flip su entrambi gli assi,

sostituire:

frame = rotateImage(frame, 180) 

con:

cv.Flip(frame, flipMode=-1) 

Questo è ' sul posto ", quindi è veloce, e non avrai più bisogno della tua funzione rotateImage :)

Esempio:

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

questo: enter image description here diventa, questo: enter image description here

2

Non è necessario utilizzare warpAffine(), dare un'occhiata a transpose() e flip().

This post mostra come ruotare un'immagine di 90 gradi.

2

Attraverso tentativi ed errori, alla fine ho scoperto la soluzione.

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 
Problemi correlati