软硬件平台
VS2015
OpenCV 3.2
IPCamera 沃仕达T7866WIP CGI协议
代码
#include "stdafx.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>using namespace cv;
using namespace std;int main(int argc, char* argv[])
{cv::VideoCapture vcap;cv::Mat image;const std::string videoStreamAddress = "http://192.168.1.142:81/videostream.cgi?user=admin&pwd=888888&.mjpg";/*Address e.g. "http://IP:port/videostream.cgi?user=admin&pwd=******&.mjpg" *///open the video stream and make sure it's openedif (!vcap.open(videoStreamAddress)) {std::cout << "Error opening video stream or file" << std::endl;return -1;}cv::namedWindow("Output Window");while (1) {for (;;) {if (!vcap.read(image)) {std::cout << "No frame" << std::endl;cv::waitKey();}cv::imshow("Output Window", image);if (cv::waitKey(1) >= 0) break;}}cvWaitKey(0);vcap.~VideoCapture();
}
说明
- 如果用浏览器直接访问URL,得到的是当前的画面,也就是一张图片。URL根据IP地址、端口号、不同摄像机命令格式可能稍有不同。例如我的就是
http://IP:port/videostream.cgi?user=admin&pwd=******&.mjpg
- 外面加一个while(1)循环,就变成“视频”了,但是目前存在掉帧的问题,如果解决了再来补充。(掉帧是因为…电脑和路由我连的是wifi…摄像头到路由,电脑到路由都用网线连起来就没事了。)
参考
https://www.codeproject.com/questions/720045/how-to-access-an-ip-camera-using-opencv-cplusplus