Home > Backend Development > C++ > How can I capture frames from IP cameras with OpenCV?

How can I capture frames from IP cameras with OpenCV?

Mary-Kate Olsen
Release: 2024-11-09 17:45:02
Original
695 people have browsed it

How can I capture frames from IP cameras with OpenCV?

Capturing Frames from IP Cameras with OpenCV

To capture frames from an IP camera using OpenCV, you can employ the following steps:

  1. Include the Necessary Libraries:

    • For OpenCV version 2.0 or higher:

      • #include "cv.h"
      • #include "highgui.h"
    • For older versions of OpenCV (e.g., 1.1pre1):

      • #include "cv.h"
      • #include "highgui.h"
  2. Open the Video Stream:

    • Use cv::VideoCapture to open the video stream. For example:

      • cv::VideoCapture vcap("rtsp://cam_address:554/live.sdp"); // RTSP stream
      • cv::VideoCapture vcap("http://user:pass@cam_address:8081/cgi/mjpg/mjpg.cgi?.mjpg"); // MJPEG stream
  3. Read Frames:

    • Use vcap.read(image) to read frames into a cv::Mat image.
  4. Display Frames:

    • Create an output window using cv::namedWindow("Output Window").
    • Display frames using cv::imshow("Output Window", image).
  5. Handle User Interaction:

    • Use cv::waitKey() to wait for user input or a specified time interval.

Example Code for OpenCV 2.0 :

#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";  // RTSP stream

    // Open the video stream and check if it's open
    if (!vcap.open(videoStreamAddress)) {
        std::cout << "Error opening video stream or file" << std::endl;
        return -1;
    }

    // Create output window for displaying frames.
    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;
    }

    return 0;
}
Copy after login

Update for H.264 RTSP Streams:

To capture frames from H.264 RTSP streams with OpenCV 2.0 or higher, you can use the following URL format:

  • rtsp://10.10.10.10:554/axis-media/media.amp

If the camera requires authentication, use the following URL format:

  • rtsp://username:[email protected]:554/axis-media/media.amp

The above is the detailed content of How can I capture frames from IP cameras with OpenCV?. 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