OpenCV

2012-05-11 16 views
5

के साथ एक वीडियो को घुमाने के लिए कैसे करें OpenCV का उपयोग करके आप वीडियो स्ट्रीम में सभी फ़्रेम कैसे घुमाते हैं? मैंने similar question में दिए गए कोड का उपयोग करने का प्रयास किया, लेकिन ऐसा लगता है कि Iplimage छवि ऑब्जेक्ट cv.RetrieveFrame लौटाया गया है।OpenCV

इस कोड मैं वर्तमान में है:

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) 

लेकिन यह सिर्फ त्रुटि देता है:

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 

मुमकिन है क्योंकि warpAffine एक CvMat और नहीं एक Iplimage लेता है। C++ cheatsheet के अनुसार, दोनों के बीच कनवर्ट करना मामूली है, लेकिन मुझे पायथन में बराबर करने पर कोई दस्तावेज नहीं मिल रहा है। मैं पाइथन में एक Iplimage से Mat में कैसे परिवर्तित करूं?

उत्तर

10

तुम सिर्फ एक 180 डिग्री रोटेशन के बाद कर रहे हैं, तो आप दोनों छोरों पर Flip उपयोग कर सकते हैं,

बदल देते हैं:

frame = rotateImage(frame, 180) 

साथ:

cv.Flip(frame, flipMode=-1) 

यह वह जगह है ' जगह में ', तो यह तेज़ है, और आपको अपने rotateImage फ़ंक्शन की आवश्यकता नहीं होगी :)

उदाहरण:

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

इस: enter image description here, हो जाता है इस: enter image description here

2

आपको warpAffine() का उपयोग करने की आवश्यकता नहीं है, transpose() और flip() पर एक नज़र डालें।

This post दर्शाता है कि छवि 90 डिग्री कैसे घुमाएं।

2

परीक्षण-और-त्रुटि के माध्यम से मैंने अंततः समाधान की खोज की।

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 

 संबंधित मुद्दे