2013-03-16 54 views
1

最近我从OpenCV 2.4.1迁移到OpenCV 2.4.3OpenCV 2.4.3 VideoCapture无法正常工作

我的程序与2.4.1版本配合良好,现在遇到2.4.3的问题。

该问题与无法打开我的视频文件的VideoCapture有关。

我在搜索互联网时看到类似的问题,但找不到合适的解决方案。下面是我的示例代码:

VideoCapture video(argv[1]); 
while(video.grab()) 
{ 
    video.retrieve(imgFrame); 
    imshow("Video",ImgFrame); 
    waitKey(1); 
} 

值得一提的是,从网络摄像头设备捕捉视频的效果很好,但我想从文件中抓取流。

我使用的是QT Creator 5,我编译的OpenCVMinGW。我正在使用Windows。

我尝试了几种不同的视频格式,我重建了OpenCV有和没有ffmpeg,但问题仍然存在。

任何想法如何解决这个问题?

回答

1

试试这个:

VideoCapture video(argv[1]); 
int delay = 1000.0/video.get(CV_CAP_PROP_FPS); 
while(1) 
{ 
    if (!video.read(ImgFrame)) break; 
    imshow("Video",ImgFrame); 
    waitKey(delay); 
} 
+0

感谢您的建议,但这是行不通的! – 2013-03-18 15:19:54

0

在我与OpenCV的经验,我挣扎着用IP摄像头,直到我的导师发现了如何让他们的工作,不要忘了,否则不会堵塞你的IP地址工作!

import cv2 
import numpy as np 
import urllib.request 

# Sets up the webcam and connects to it and initalizes a variable we use for it 
stream=urllib.request.urlopen('http://xx.x.x.xx/mjpg/video.mjpg') 
bytes=b'' 

while True: 
    # Takes frames from the camera that we can use 
    bytes+=stream.read(16384) 
    a = bytes.find(b'\xff\xd8') 
    b = bytes.find(b'\xff\xd9') 
    if a!=-1 and b!=-1: 
     jpg = bytes[a:b+2] 
     bytes= bytes[b+2:] 
     frame = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.IMREAD_COLOR) 
     img = frame[0:400, 0:640] # Camera dimensions [0:WIDTH, 0:HEIGHT] 

     # Displays the final product 
     cv2.imshow('frame',frame) 
     cv2.imshow('img',img) 

    # Hit esc to kill 
     if cv2.waitKey(1) ==27: 
      exit(0)