【Basler相机】 opencv下连续获取图片并保存

本文介绍了一个利用Pylon和OpenCV库进行图像采集的C++程序实例。该程序能够从相机中捕获指定数量的图像,并可以选择保存这些图像或录制视频。此外,还展示了如何设置相机参数及如何将捕获的图像转换为OpenCV格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//定义是否保存图片
#define saveImages 1
//定义是否记录视频
#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 = 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(1);//这里必须是1。0则需要单击关闭窗口按钮才能采集下一个图像

            }

        }            

    }
    catch (GenICam::GenericException &e)
    {
        // Error handling.
        cerr << "An exception occurred." << endl
            << e.GetDescription() << endl;
    }
    return;
}
### Basler相机C++开发教程 #### 使用Basler相机制作应用程序的关键要素 为了创建一个能够利用Basler相机功能的应用程序,开发者通常会依赖于Pylon SDK所提供的API。该SDK支持多种编程语言,其中包括C++。对于希望集成Basler相机到其项目中的程序员来说,了解如何初始化设备、配置参数以及获取图像数据至关重要[^1]。 ```cpp // 初始化 Pylon 库 PylonInitialize(); // 创建打开第一个可用的相机实例 CBaslerUsbInstantCamera camera(CTlFactory::GetInstance().CreateFirstDevice()); camera.Open(); ``` 上述代码展示了启动过程的一部分,在这里调用了`PylonInitialize()`函数来准备环境,通过`CBaslerUsbInstantCamera`类实例化了一个代表物理摄像机的对象。这一步骤之后可以继续设置诸如曝光时间之类的属性。 #### 配置相机参数 当涉及到具体应用需求时,调整相机的各项参数变得非常重要。例如,如果目标是在低光照条件下获得更好的图片质量,则可能需要增加ISO增益或延长曝光时间。下面的例子说明了怎样修改这些重要的成像特性: ```cpp // 设置自动曝光模式关闭 camera.ExposureAuto.SetValue(Basler_UEye_CameraParams::ExposureAuto_Off); // 手动设定曝光时间为20ms camera.ExposureTime.SetValue(20000); ``` 这段代码片段显示了如何禁用自动曝光控制将固定值应用于手动曝光时间。这样的操作允许更精确地掌控捕获条件下的光线水平。 #### 图像采集与处理 一旦完成了前期准备工作,下一步就是实际抓取帧对其进行必要的转换以便后续分析。OpenCV库在这里发挥了重要作用,它提供了丰富的工具集用于加载、保存和变换视觉信息。以下是结合两者工作的简单示范: ```cpp #include <pylon/PylonIncludes.h> #include <opencv2/opencv.hpp> using namespace cv; using namespace Pylon; int main(int argc, char* argv[]) { try { // ... (省略之前的初始化部分) while(true){ camera.StartGrabbing(GrabStrategy_LatestImageOnly); for(GrabResultPtr pgrabresult; camera.IsGrabbing(); ){ camera.RetrieveResult(5000,pgrabresult,TimeoutHandling_ThrowException); if(pgrabresult->GrabSucceeded()){ Mat img(Size(pgrabresult->GetWidth(),pgrabresult->GetHeight()), CV_8UC3,(void*)pgrabresult->GetBuffer()); imshow("Live View",img); waitKey(1); } break; } camera.StopGrabbing(); } // 清理资源... camera.Close(); PylonTerminate(); return 0; } catch (...) { /* 错误处理 */ } } ``` 此段代码实现了连续捕捉视频流通过窗口实时预览的功能。值得注意的是,这里使用到了OpenCV提供的`imshow()`方法展示每一帧的内容;同时,也体现了如何安全地中止循环在结束前释放所有占用的硬件接口[^2]。
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值