Pylon5 SDK搭配OpenCV使用入门

时间:2023-07-11 11:01:44

本文假设已经安装了Basler官网提供的Pylon

Pylon5 SDK搭配OpenCV使用入门

目前最新的版本是5.0.5,如果上述链接打不开,请直接所有Basler官网下载,需要注意的是在安装Pylon5时要选择Developer模式,这样才会安装关于pylon5 SDK开发包,安装完可以到安装路径下找到,软件也会自动将一些路径自动添加到系统环境变量。

Pylon5 SDK搭配OpenCV使用入门

Pylon5 SDK搭配OpenCV使用入门

使用Pylon5 SDK开发与使用OpenCV开发一些功能流程一样,无非是引入包目录(include)和库包含目录(lib),本文使用的OpenCV版本为2.4.9.。。关于工程如何配置,我不想说话,只扔出两张属性表。

Pylon.props

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup>
<ClCompile>
<AdditionalIncludeDirectories>$(PYLON_DEV_DIR)\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<AdditionalLibraryDirectories>$(PYLON_DEV_DIR)\lib\win32;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemGroup />
</Project>

OpenCVConsole.props

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<IncludePath>$(OPENCV_ROOT)\include;$(IncludePath)</IncludePath>
<LibraryPath Condition="'$(Platform)'=='Win32'">$(OPENCV_ROOT)\x86\vc12\lib;$(LibraryPath)</LibraryPath>
<LibraryPath Condition="'$(Platform)'=='X64'">$(OPENCV_ROOT)\x64\vc12\lib;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup>
<Link Condition="'$(Configuration)'=='Debug'">
<AdditionalDependencies>opencv_calib3d249d.lib;opencv_contrib249d.lib;opencv_core249d.lib;opencv_features2d249d.lib;opencv_flann249d.lib;opencv_gpu249d.lib;opencv_highgui249d.lib;opencv_imgproc249d.lib;opencv_legacy249d.lib;opencv_ml249d.lib;opencv_nonfree249d.lib;opencv_objdetect249d.lib;opencv_ocl249d.lib;opencv_photo249d.lib;opencv_stitching249d.lib;opencv_superres249d.lib;opencv_ts249d.lib;opencv_video249d.lib;opencv_videostab249d.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<Link Condition="'$(Configuration)'=='Release'">
<AdditionalDependencies>opencv_calib3d249.lib;opencv_contrib249.lib;opencv_core249.lib;opencv_features2d249.lib;opencv_flann249.lib;opencv_gpu249.lib;opencv_highgui249.lib;opencv_imgproc249.lib;opencv_legacy249.lib;opencv_ml249.lib;opencv_nonfree249.lib;opencv_objdetect249.lib;opencv_ocl249.lib;opencv_photo249.lib;opencv_stitching249.lib;opencv_superres249.lib;opencv_ts249.lib;opencv_video249.lib;opencv_videostab249.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<ClCompile>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup />
</Project>

注意根据本机的库安装地址可能要修改上面的属性表文件,只需要到属性管理器窗口添加现有属性表就好了。如果没有属性窗口,请到VS视图菜单中打开。

Pylon5 SDK搭配OpenCV使用入门

使用Pylon5 SDK搭配OpenCV采集图像程序流程如下:

Pylon5 SDK搭配OpenCV使用入门

下面是完整的源代码,该工程使用Basler piA2400-17gc GigE 相机测试成功。

//定义是否保存图片
#define saveImages 1
//定义是否记录视频
#define recordVideo 1 // 加载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 = 10; 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('D', 'I', 'V','X'), 10, 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 << "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(0); } } }
catch (GenICam::GenericException &e)
{
// Error handling.
cerr << "An exception occurred." << endl
<< e.GetDescription() << endl;
}
return;
}

参考文档:

Application_Note_Build_pylon_C++_Apps_with_Free_Visual_Studio_IDE

Getting_Started_with_pylon5_and_OpenCV