一、下载安装balser SDK
https://www.baslerweb.com/cn/downloads/software-downloads/#type=pylonsoftware 选择5.2.0版本比较稳定
解压pylon-5.2.0.13457-x86_64.tar.gz
得到pylon-5.2.0.13457-x86_64文件夹

cd pylon-5.2.0.13457-x86_64

解压pylonSDK-5.2.0.13457-x86_64.tar.gz
得到pylon5文件夹
拷贝到/opt/下并安装

sudo cp -r pylon /opt/
cd /opt/
source /opt/pylon5/bin/pylon-setup-env.sh pylon5

将系统和相机利用路由器连接在同一个局域网内,打开软件搜索相机IP并改成固定IP,就可以打开相机看实时图像了

sudo /opt/pylon5/bin/PylonViewerApp

编写如下cpp程序尝试抓图

#include <pylon/PylonIncludes.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/video.hpp>
 
using namespace Pylon;
using namespace std;
using namespace cv;

int main(int argc, char* argv[])
{
	Mat src;
	CImageFormatConverter formatConverter;
    formatConverter.OutputPixelFormat = PixelType_BGR8packed;
    int grabbedlmages = 0;
    CPylonImage pylonImage;
    int exitCode = 0;
    PylonInitialize();
    try
    {
        CInstantCamera camera( CTlFactory::GetInstance().CreateFirstDevice());
        cout << "Using device " << camera.GetDeviceInfo().GetModelName() << endl <<"IP: " << camera.GetDeviceInfo().GetAddress() << endl;
        camera.MaxNumBuffer = 5;
        const unsigned int c_countOfImagesToGrab = 100;
        camera.StartGrabbing( c_countOfImagesToGrab);
        CGrabResultPtr ptrGrabResult;
 
        while ( camera.IsGrabbing())
        {
            camera.RetrieveResult( 5000, ptrGrabResult, TimeoutHandling_ThrowException);
            if (ptrGrabResult->GrabSucceeded())
            {
                cout << "SizeX: " << ptrGrabResult->GetWidth() << endl;
                cout << "SizeY: " << ptrGrabResult->GetHeight() << endl;
                formatConverter.Convert(pylonImage, ptrGrabResult);
                src = cv::Mat(ptrGrabResult->GetHeight(), ptrGrabResult->GetWidth(), CV_8UC3, (uint8_t *) pylonImage.GetBuffer());
                if (false)
                {
                   std::ostringstream s;
                   s << grabbedlmages << ".jpg";
                   std::string imageName(s.str());
                   imwrite(imageName, src);
                   grabbedlmages++;
                }
                namedWindow("OpenCV Display Window", CV_WINDOW_NORMAL); // other options: CV_AUTOSIZE, CV_FREERATIO
                imshow("OpenCV Display Window", src);
                waitKey(1);
                const uint8_t *pImageBuffer = (uint8_t *) ptrGrabResult->GetBuffer();
                cout << "Gray value of first pixel: " << (uint32_t) pImageBuffer[0] << endl << endl;
            }
            else
            {
                cout << "Error: " << ptrGrabResult->GetErrorCode() << " " << ptrGrabResult->GetErrorDescription() << endl;
            }
        }
    }
    catch (const GenericException &e)
    {
        cerr << "An exception occurred." << endl
        << e.GetDescription() << endl;
        exitCode = 1;
    }
    cerr << endl << "Press Enter to exit." << endl;
    while( cin.get() != '\n');
    PylonTerminate();
    return exitCode;
}

CMakeLists文件编辑如下

cmake_minimum_required(VERSION 3.18)
project(PylonGrab)

find_package(PCL REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})

set(PYLON_INCLUDE_DIRS
/opt/pylon5/include
/opt/pylon5/include/Base
/opt/pylon5/include/GenApi
/opt/pylon5/include/pylon
/opt/pylon5/include/bconadapter
/opt/pylon5/include/genapic
/opt/pylon5/include/pylonc
/opt/pylon5/include/FirmwareUpdate
)
FILE (GLOB PYLON_LIBS "/opt/pylon5/lib64/lib*.so" )
set(PYLON_LIBS_DIRS /opt/pylon5/lib64)
include_directories(${PYLON_INCLUDE_DIRS})
link_directories(${PYLON_LIBS_DIRS})

add_executable(PylonGrab pylon.cc)
target_link_libraries(PylonGrab 
${OpenCV_LIBS}
${PYLON_LIBS}
)
1. DeviceRemovalHanding
如何检测相机设备的移除(removal),如何重连去除的相机设备
代码:CSampleConfigurationEventHandler

2. Grab
如何用CInstantCamera类抓取和处理图像。camera.StartGrabbing( c_countOfImagesToGrab );抓取c_countOfImagesToGrab数量的图像。

3. Grab_CameraEvents
相机发送event messages (比如exposure end)给计算机,CInstantCamera可以通过注册camera event handler,自动抓取和处理event messages,保存在node map里。
介绍了两个camera events: Exposre End(曝光图像数量,事件时间等), Event Overrun。
代码:CSoftwareTriggerConfiguration, CSampleCameraEventHandler, CSampleImageEventHandler

4. Grab_ChunkImage
利用 data chunks feature, 如何抓取图像,处理附加数据 (frame count, time stamp, CRC checksums)。在chunk mode下,传输数据分成不同数据块。
代码:CBaslerUniversalGrabResultPtr, CSampleImageEventHandler

5. Grab_MultiCast
multi cast即两个电脑上的运行相同程序,接收同一个相机的数据。

6. Grab_MultipleCameras
利用CInstantCameraArray类抓取和处理来自多个相机的图像。

7. Grab_Strategies
使用CInstantCamera的抓取策略:
GrabStrategy_OneByOne: 根据图像获取顺序依次处理。当需要处理所有抓取的图像时,该策略可用。比如在生产和质量监控中。
GrabStrategy_LatestImageOnly 和GrabStrategy_LatestImages:用于当获取的图像只在屏幕上显示。
GrabStrategy_UpcomingImage : 确保调用RetrieveResult()后,获得已被抓取的图像。

8 Grab_UsingActionCommand
只适用 Basler GigE Vision相机。向多个相机发出GigE Vision ACTION_CMD。通过使用action command,可以同时trigger多个相机。而利用software triggering,每个相机需要单独触发。
代码:
IGigETransportLayer: issue action command。
CActionTriggerConfiguration: set up the basic action command features

9. Grab_UsingBufferFactory
使用用户提供的Buffer factory。可选,用于高级用例。只有打算抓取数据到外部提供缓存时,使用buffer factory。
代码:
MyBufferFactory: 用户提供的buffer factory。
SetBufferFactory

10 Grab_UsingExposureEndEvent
使用Exposure End 事件,加速图像获取。比如,当传感器曝光结束,相机发送Exposure End事件给计算机。计算机可以在图像传输完成前,接收到事件。
这可以避免不必要的延迟:在图像数据未完成传输时,移动图像对象。
代码:
MyEvents
CEventHandle

11. Grab_UsingGrabLoopThread
利用CInstantCamera提供的grab loop thread抓取和处理图像。

12. Grab_UsingSequencer
使用Sequencer feature来抓取图像,使用三个sequence set获取图像,每个sequence set使用不同的图像高度。

13 GUI_ImageWindow
使用CPylonImageWindow类显示图像。

14 GUI_Sample
使用MFC GUI和pylon C++ API来枚举相机,配置相机,开始/停止抓取,显示/存储抓取图像。
利用GUI控制来显示和修改相机参数。

15 GUI_SampleMultiCam
使用MFC GUI 和pylon C++ API, 操作多个相机。

16 ParametrizeCamera_AutoFunctions
使用Basler相机的auto functions, 比如, Gain Auto, Exposure Auto, Balance White Auto。自动调整相机参数。

17. ParametrizeCamera_Configurations
使用现有configuration event, 注册自己的configuration event handler。
configuration event handler继承自CConfigurationEventHandler,并重写虚函数。当相机状态改变,event handler的函数被调用。

18. ParametrizeCamera_GenericParameterAccess
对于相机配置和参数访问,pylon API 使用GenlCam标准定义的技术。代码中,利用GenApi::INodeMap访问各种参数。

19. ParametrizeCamera_LoadAndSave
利用CFeaturePersistence, 保存(加载)相机特征(node map)到(从)文件。

20. ParametrizeCamera_LookupTable
使用独立于相机接口的亮度查找表(Luminance Lookup Table, LUT)特征。查找表的用处:replace the pixel values in your images by values defined by you.

21. ParametrizeCamera_NativeParameterAccess
使用device-specific的instant Camera class,配置相机

22. ParametrizeCamera_SerialCommunication
使用ace 2 Pro 相机支持的 串行通信(Serial Communication) 特征。

23. ParametrizeCamera_Shading
计算和上传gain shading sets给相机
CreateShadingData()假设曝光条件(光照,曝光时间)已经设置好,能获取强度均匀的图像(images of uniform intensity),但是获取的图像并不均匀。因此计算gain shading 数据,补偿观测到的不均匀(non-uniformity)。gain shading 数据保存在本地文件中。
UploadFile(): 将计算的gain shading data从本地文件发送到相机。

24. ParametrizeCamera_UserSets
使用用户配置集合(user configuration sets, User Set), 配置相机使其利用用户定义的设置(user set 1)开启。
也可以利用pylon Viewer配置相机,然后保存自定义设置到一个user set。

25 Utility_GrabAvi
在windows系统下,生成 Audio Video InterLeave (AVI)格式的视频。

26 Utility_GrabVideo
生成MP4格式的视频。假定pylon Supplementary Package for MPEG-4已经安装。

27 Utility_Image
使用pylon图像类CPylonImage 和CPylonBitmapImage。
CPylonImage支持处理不同像素类型的图像缓存
CPylonBitmapImage: 可以用来创建窗口位图来显示图像。另外,pylon有两个图像类相关接口(IImage和IReusableImage)
IImage可以用来访问图像属性,图像缓存。
IReusableImage接口拓展了IImage接口,能够重新使用图像资源,来表示一个不同的图像。

28. Utility_ImageDecompressor
使用Basler Compression Beyond特征。压缩图像。

29. Utility_ImageFormatConverter
使用CImageFormatConverter类。

30. Utility_ImageLoadAndSave
使用CImagePersistence类,加载和保存图像。使用了图像类相关的pylon接口IImage和IReusableImage。
IImage可以用来访问图像属性和图像缓存。因此,保存图像可以使用。
IReusableImage在记载图像时使用。

31. Utility_InstantInterface
使用CInstantInterface类访问接口参数。该例子展示了如何访问Power-over-CoaXPress 设置,检测power usage。

32. Utility_IpConfig
配置GigE Vision相机的IP地址。