Home > Backend Development > C++ > How Can OpenCV Be Used to Access Frames from Network Cameras?

How Can OpenCV Be Used to Access Frames from Network Cameras?

Barbara Streisand
Release: 2024-11-09 13:11:02
Original
574 people have browsed it

How Can OpenCV Be Used to Access Frames from Network Cameras?

OpenCV with Network Cameras: A Comprehensive Guide

When working with network cameras, accessing frame data for image processing and analysis tasks often becomes crucial. This article explores how to utilize OpenCV, a powerful computer vision library, to grab frames from network cameras effectively.

Addressing the Challenge

A user inquired about acquiring frames from a network camera using OpenCV 1.1pre1. The camera streamed MPEG4 over RTSP or MJPEG over HTTP. Despite extensive research, the user encountered difficulties in using FFMPEG with OpenCV.

The OpenCV Solution

For OpenCV versions 2.0 and above, a C code snippet is provided below to retrieve frames from the network camera:

#include "cv.h"
#include "highgui.h"
#include <iostream>

int main(int, char**) {
    cv::VideoCapture vcap;
    cv::Mat image;

    const std::string videoStreamAddress = "rtsp://cam_address:554/live.sdp"; 

    if(!vcap.open(videoStreamAddress)) {
        std::cout << "Error opening video stream or file" << std::endl;
        return -1;
    }

    cv::namedWindow("Output Window");

    for(;;) {
        if(!vcap.read(image)) {
            std::cout << "No frame" << std::endl;
            cv::waitKey();
        }
        cv::imshow("Output Window", image);
        if(cv::waitKey(1) >= 0) break;
    }   
}
Copy after login

In the code, videoStreamAddress can be an MJPEG stream address like "http://user:pass@cam_address:8081/cgi/mjpg/mjpg.cgi?.mjpg".

To grab frames from H.264 RTSP streams, refer to your camera's API for specific URL command details. For example, an Axis network camera's RTSP address might be:

rtsp://10.10.10.10:554/axis-media/media.amp
Copy after login

This solution leverages the cv::Mat structure, favored over the legacy IplImage structure. The code creates an output window for frame display, avoiding inefficient window creation with each imshow(...) call.

The above is the detailed content of How Can OpenCV Be Used to Access Frames from Network Cameras?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template