2015-03-03 120 views
0

我想將我的圖像上的代碼轉換爲視頻。我的程序獲取一幅畫面,作品出來的平均RGB各9 * 9窗口,並輸出圖像:沒有視頻輸出OpenCV蟒蛇

輸入圖像: Input Image

輸出圖像: Output Image

這裏是我的代碼具有圖像輸入/輸出:

import numpy as np 
import cv2 

#Read in image 
img = cv2.imread('images/0021.jpg') 

scale = 9 
#Get x and y components of image 
y_len,x_len,_ = img.shape 

mean_values = [] 
for y in range(scale): 
    for x in range(scale): 
     #Crop image 3*3 windows 
     cropped_img=img[(y*y_len)/scale:((y+1)*y_len)/scale, 
          (x*x_len)/scale:((x+1)*x_len)/scale] 

     mean_val=cv2.mean(cropped_img) 
     mean_val=mean_val[:3] 
     cropped_img[:,:,:] = mean_val 

print img.shape  
cv2.imshow('mean_RGB',img) 
cv2.waitKey(0) 

當試圖使用上的視頻I得到的視頻輸出相同的代碼,但它爲空(0字節)。

下面是代碼:

import numpy as np 
import cv2 

cap = cv2.VideoCapture('videos/kondo2.avi') 

fourcc = cv2.cv.CV_FOURCC(*'DIVX') 
out = cv2.VideoWriter('videos/output.avi',fourcc, 15.0, (800,600),True) 

while(cap.isOpened()): 
    ret, frame = cap.read() 
    if ret == True: 
     y_len,x_len,_ = frame.shape 
     scale = 9 
     for y in range(scale): 
      for x in range(scale): 
       cropped_frame=frame[(y*y_len)/scale:((y+1)*y_len)/scale, 
             (x*x_len)/scale:((x+1)*x_len)/scale] 

       mean_val=cv2.mean(cropped_frame) 
       mean_val=mean_val[:3] 
       cropped_frame[:,:,:] = mean_val 
       out.write(frame) 

cap.release() 
out.release() 
cv2.destroyAllWindows() 

謝謝您的閱讀:)

+0

嘗試帶有雙反斜線(\\)路徑名,還以絕對路徑名儘量保證是沒有問題的。 – GPPK 2015-03-03 09:25:43

+0

@GPPK路徑不應該是問題,因爲'output.avi'視頻被保存到正確的位置,它只是空的沒有幀。 – 2015-03-03 09:30:03

+0

夠公平的。我總是發現最好從靜態路徑名開始,以減少錯誤的可能性。這是問題的時間。 – GPPK 2015-03-03 09:33:45

回答

1

我想你的代碼。我必須改變的三件事:

  • 輸出視頻的編解碼器。我改變它爲MP4,它的工作原理。
  • 行out.write(frame)的縮進。
  • 調整輸入框的大小,以確保它具有合適的尺寸。

這裏對我來說是什麼工作:

import numpy as np 
import cv2 

cap = cv2.VideoCapture('videos/kondo2.avi') 
w=800 
h=600 

fourcc = cv2.cv.CV_FOURCC('m', 'p', '4', 'v') 
out = cv2.VideoWriter('videos/output.avi',fourcc, 25, (w,h),True) 
count = 0 
while(cap.isOpened()): 
    count = count + 1 
    print "processing frame ", count 
    ret, frame = cap.read() 
    if ret == True: 
     frame = cv2.resize(frame,(w,h), interpolation = cv2.INTER_CUBIC) 
     y_len,x_len,_ = frame.shape 

     scale = 9 
     for y in range(scale): 
      for x in range(scale): 
       cropped_frame=frame[(y*y_len)/scale:((y+1)*y_len)/scale, 
            (x*x_len)/scale:((x+1)*x_len)/scale] 

       mean_val=cv2.mean(cropped_frame) 
       mean_val=mean_val[:3] 
       cropped_frame[:,:,:] = mean_val 

     out.write(frame) 


cap.release() 
out.release() 
cv2.destroyAllWindows() 
+0

這是有效的,我必須做的其他事情是將ffmpeg第三方二進制文件從opencv文件夾複製到我的python目錄。在處理完最後一幀後,我得到一個回溯錯誤,即文件「C:\ Python27 \ video.py」,第16行,在 frame = cv2.resize(frame,(w,h),interpolation = cv2。 INTER_CUBIC) error:.. \ .. \ .. \ .. \ opencv \ modules \ imgproc \ src \ imgwarp.cpp:1968:錯誤:(-215)ssize.area()> 0在函數cv :: resize '。我想這是因爲它試圖在不存在的框架上操作?我如何爲這種情況制定例外規定? – 2015-03-09 12:53:28

+0

@NeilVicker:如果ret == True,那麼將該行移動到範圍內,那麼你將會很好。看到我編輯的答案。 – 2015-03-09 16:11:15