简体   繁体   中英

OpenCV 2.4.3 VideoCapture is not working

Recently I migrated to OpenCV 2.4.3 from OpenCV 2.4.1 .

My program which worked well with 2.4.1 version now encounters problem with 2.4.3 .

The problem is related to VideoCapture that can not open my video file.

I saw a similar problem while searching the internet, but I couldn't find a proper solution for this. Here is my sample code:

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

It's worth mentioning that capturing video from webcam device works well, but I want to grab stream from file.

I'm using QT Creator 5 and I compiled OpenCV with MinGW . I'm using Windows.

I tried several different video formats and I rebuilt OpenCV with and without ffmpeg , but the problem still persists.

Any idea how to solve the problem?

Try this:

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);
}

In my experience with OpenCV I struggled using IP cams until my mentor discovered how to get them to work, don't forget to plug your IP address in otherwise it won't work!

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM