Opencv打开basler相机——实现显示视频、保存图片、记录视频(Windows下)
【摘要】
文章目录
一、环境介绍二、环境配置三、完整代码
一、环境介绍
系统:windows10开发环境:VS2015+OpenCV3.1其它库:pylon(安装basler官方SDK自带的库)
开发环境配置,可参考这里:VS2015 + OpenCV3.1 环境配置与项目搭建(C++版)
二、环境配置
1.首先根据自己的VS选择对应的版本 2.配...
一、环境介绍
- 系统:windows10
- 开发环境:VS2015+OpenCV3.1
- 其它库:pylon(安装basler官方SDK自带的库)
开发环境配置,可参考这里:VS2015 + OpenCV3.1 环境配置与项目搭建(C++版)
二、环境配置
1.首先根据自己的VS选择对应的版本
2.配置包含目录
3.配置库目录
4.配置附加包含目录
5.配置附加库目录
6.配置附加依赖项
三、完整代码
//定义是否保存图片
#define saveImages 0
//定义是否记录视频
#define recordVideo 0
// 加载OpenCV API
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/video.hpp>
//加载PYLON API.
#include <pylon/PylonIncludes.h>
#include<iostream>
#ifdef PYLON_WIN_BUILD
#include <pylon/PylonGUI.h>
#endif
//命名空间.
using namespace Pylon;
using namespace cv;
using namespace std;
//定义抓取的图像数
static const uint32_t c_countOfImagesToGrab = 800;
void main()
{
//Pylon自动初始化和终止
Pylon::PylonAutoInitTerm autoInitTerm;
try
{ //创建相机对象(以最先识别的相机)
CInstantCamera camera(CTlFactory::GetInstance().CreateFirstDevice());
// 打印相机的名称
std::cout << "Using device " << camera.GetDeviceInfo().GetModelName() << endl;
//获取相机节点映射以获得相机参数
GenApi::INodeMap& nodemap = camera.GetNodeMap();
//打开相机
camera.Open();
//获取相机成像宽度和高度
GenApi::CIntegerPtr width = nodemap.GetNode("Width");
GenApi::CIntegerPtr height = nodemap.GetNode("Height"); //设置相机最大缓冲区,默认为10
camera.MaxNumBuffer = 5;
// 新建pylon ImageFormatConverter对象.
CImageFormatConverter formatConverter;
//确定输出像素格式
formatConverter.OutputPixelFormat = PixelType_BGR8packed;
// 创建一个Pylonlmage后续将用来创建OpenCV images
CPylonImage pylonImage; //声明一个整形变量用来计数抓取的图像,以及创建文件名索引
int grabbedlmages = 0; // 新建一个OpenCV video creator对象.
VideoWriter cvVideoCreator;
//新建一个OpenCV image对象. Mat openCvImage;
// 视频文件名 std::string videoFileName = "openCvVideo.avi";
// 定义视频帧大小
cv::Size frameSize = Size((int)width->GetValue(), (int)height->GetValue()); //设置视频编码类型和帧率,有三种选择
// 帧率必须小于等于相机成像帧率
cvVideoCreator.open(videoFileName, CV_FOURCC('M', 'J', 'P', 'G'), 24, frameSize, true);
//cvVideoCreator.open(videoFileName, CV_F0URCC('M','P',,4','2’), 20, frameSize, true);
//cvVideoCreator.open(videoFileName, CV_FOURCC('M', '3', 'P', 'G'), 20, frameSize, true); // 开始抓取c_countOfImagesToGrab images.
//相机默认设置连续抓取模式
camera.StartGrabbing(c_countOfImagesToGrab, GrabStrategy_LatestImageOnly); //抓取结果数据指针
CGrabResultPtr ptrGrabResult; // 当c_countOfImagesToGrab images获取恢复成功时,Camera.StopGrabbing()
//被RetrieveResult()方法自动调用停止抓取 while (camera.IsGrabbing()) { // 等待接收和恢复图像,超时时间设置为5000 ms. camera.RetrieveResult(5000, ptrGrabResult, TimeoutHandling_ThrowException); //如果图像抓取成功 if (ptrGrabResult->GrabSucceeded()) { // 获取图像数据 cout << "SizeX: " << ptrGrabResult->GetWidth() << endl; cout << "SizeY: " << ptrGrabResult->GetHeight() << endl; //将抓取的缓冲数据转化成pylon image. formatConverter.Convert(pylonImage, ptrGrabResult); // 将 pylon image转成OpenCV image. openCvImage = cv::Mat(ptrGrabResult->GetHeight(), ptrGrabResult->GetWidth(), CV_8UC3, (uint8_t *)pylonImage.GetBuffer()); //如果需要保存图片 if (saveImages) { std::ostringstream s; // 按索引定义文件名存储图片 s << "save//image_" << grabbedlmages << ".jpg"; std::string imageName(s.str()); //保存OpenCV image. imwrite(imageName, openCvImage); grabbedlmages++; } //如果需要记录视频 if (recordVideo) { cvVideoCreator.write(openCvImage); } //新建OpenCV display window. namedWindow("OpenCV Display Window", CV_WINDOW_NORMAL); // other options: CV_AUTOSIZE, CV_FREERATIO //显示及时影像. imshow("OpenCV Display Window", openCvImage); // Define a timeout for customer's input in // '0' means indefinite, i.e. the next image will be displayed after closing the window. // '1' means live stream waitKey(10); } }
}
catch (GenICam::GenericException &e)
{
// Error handling.
cerr << "An exception occurred." << endl << e.GetDescription() << endl;
} system("pause");
return ;
}
测试结果:
温馨提示:这里我配置的是Release x64,所以在编译运行时,也要选择相应的选项。
文章来源: ai-wx.blog.csdn.net,作者:AI 菌,版权归原作者所有,如需转载,请联系作者。
原文链接:ai-wx.blog.csdn.net/article/details/108299852
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)