C/C++ OpenCV调用海康摄像头

最近需要用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;
}

 

OpenCV+海康威视摄像头的实时读取

https://blog.csdn.net/lonelyrains/article/details/50350052

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值