最近需要用c++实现opencv调用海康ipc。原来python中是这样写的。
source = "rtsp://admin:xxxxxxxx@192.168.100.38:554/h265/ch1/main/av_stream/1" # 摄像头的rtsp地址
cam = cv2.VideoCapture(source)
想在c++中也这样调用,但是网上找到的都是 这样的例子
#include <opencv2\opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
//读取视频或摄像头
VideoCapture capture(0);
while (true)
{
Mat frame;
capture >> frame;
imshow("读取视频", frame);
waitKey(30); //延时30
}
return 0;
}
后来就读取源码发现有这么一段
/** @overload
@brief Open video file or a capturing device or a IP video stream for video capturing with API Preference
@param filename it can be:
- name of video file (eg. `video.avi`)
- or image sequence (eg. `img_%02d.jpg`, which will read samples like `img_00.jpg, img_01.jpg, img_02.jpg, ...`)
- or URL of video stream (eg. `protocol://host:port/script_name?script_params|auth`).
Note that each video stream or IP camera feed has its own URL scheme. Please refer to the
documentation of source stream to know the right URL.
@param apiPreference preferred Capture API backends to use. Can be used to enforce a specific reader
implementation if multiple are available: e.g. cv::CAP_FFMPEG or cv::CAP_IMAGES or cv::CAP_DSHOW.
@sa The list of supported API backends cv::VideoCaptureAPIs
*/
CV_WRAP VideoCapture(const String& filename, int apiPreference);
上边意思是说
@param filename可以是:
-视频文件的名称(例如。“video.avi”)
-或图像序列(例如。“img_%02d.jpg”,它将读取“img_0 .jpg, img_01.jpg, img_02.jpg,…”)
-或视频流的网址(例如:protocol://host:port/script_name?script_params|auth`)。
注意,每个视频流或IP摄像机提要都有自己的URL方案。请参阅
文档的源流,以了解正确的URL。 这里就是我们需要的了:)
@param apiPreference首选捕获要使用的API后端。可以用来强制执行特定的阅读器吗
实现如果多个可用:例如cv::CAP_FFMPEG或cv::CAP_IMAGES或cv::CAP_DSHOW。
int main()
{
//读取视频或摄像头
//VideoCapture capture(0);
string source = "rtsp://admin:xxxxx@192.168.100.38:554/h265/ch1/main/av_stream/1"; //摄像头的rtsp地址
VideoCapture capture(source, CAP_FFMPEG);
while (true)
{
Mat frame;
capture >> frame;
imshow("读取视频", frame);
waitKey(30); //延时30
}
return 0;
}