美文网首页摄像头
Ubuntu16.04下配置Basler工业相机(使用QtCre

Ubuntu16.04下配置Basler工业相机(使用QtCre

作者: SIENTIST | 来源:发表于2018-09-01 11:17 被阅读0次

    Basler工业相机网上资料少,写的博客更少,当时为了把这个Basler相机用起来,不知道耗费了我多少心血。
    查阅了Basler的官方文档,还有各种语焉不详的博客,终于能够在Linux下调用Basler相机了。
    所以我决定写一篇非常详细的博客,好让后来者少踩些坑。


    我的第一个工业相机,据说这玩意要近万块钱

    其实Linux下配置Basler摄像头时和配置OpenCV时相差不大。
    先去 官网下载对应的安装包
    https://www.baslerweb.com/cn/sales-support/downloads/software-downloads/#type=pylonsoftware;version=all;os=windows
    根据自己的系统选择x86或者x86_64(即x64)版本。

    我的相机型号为acA1920-40gc。

    下载好后,对压缩包进行解压操作,可以选择解压文件到自己选择的目录,此处我们选择默认当前目录:

    $ tar -xzvf  pylon-5.0.***.tar.gz
    

    解压文件后,打开文件,里边还有一个压缩包,此压缩包即为安装文件,解压此文件到/opt目录下:

    $ sudo tar -C /opt -xzvf pylon***armhf.tar.gz
    

    安装完毕后就开始在qtcreator中进行配置,以便在Qtcreator中调用该相机。
    1、首先打开Qtcreator,如下图所示,创建控制台项目或者空项目


    ————————————————————————————
    2、然后打开.pro文件,在其中配置Basler相机:

    先找到INCLUDEPATH的路径:
    (1)点开“计算机”,点开文件夹“opt”



    (2)接着打开pylon5文件夹



    那么INCLUDEPATH 的内容为:
    INCLUDEPATH += /opt/pylon5/include \
    /opt/pylon5/include/pylon
    

    例子:(下图中我是把OpenCV和Basler一起配置)


    ——————————————————————————————————

    3、写好了INCLUDEPATH,再来写LIBS。
    LIBS(我目前知道的)有2种写法:
    第一种是直接写出路径来:

    例子:
    LIBS += /usr/local/lib/libopencv_calib3d.so \
    /usr/local/lib/libopencv_calib3d.so.3.2 \
    /usr/local/lib/libopencv_calib3d.so.3.2.0 \
    /usr/local/lib/libopencv_core.so \
    

    第二种方法是先写一个总的,再写分的:

    例子:
    LIBS +=-L/opt/pylon5/lib64 \
    -lbxapi-5.0.11 \
    -lbxapi \
    -lFirmwareUpdate_gcc_v3_0_Basler_pylon_v5_0 \
    -lGCBase_gcc_v3_0_Basler_pylon_v5_0 \
    -lGenApi_gcc_v3_0_Basler_pylon_v5_0 \
    -lgxapi-5.0.11 \
    

    这2种写法都是一样的,是通用的。

    先找到LIB所在的路径为 opt / pylon5 / lib64



    可以看到里面有很多后缀为.so的文件,把这些文件的路径写到.pro文件中就行了。



    这里我把我总的.pro文件内容贴出来,可供参考:
    (我这里面同时配置了OpenCV和Basler,注意我的Basler的版本为5.0.11)
    TEMPLATE = app
    CONFIG += console c++11
    CONFIG -= app_bundle
    CONFIG -= qt
    
    SOURCES += main.cpp
    
    INCLUDEPATH += /usr/local/include \
    /usr/local/include/opencv \
    /usr/local/include/opencv2 \
    /opt/pylon5/include \
    /opt/pylon5/include/pylon
    
    LIBS += /usr/local/lib/libopencv_calib3d.so \
    /usr/local/lib/libopencv_calib3d.so.3.2 \
    /usr/local/lib/libopencv_calib3d.so.3.2.0 \
    /usr/local/lib/libopencv_core.so \
    /usr/local/lib/libopencv_core.so.3.2 \
    /usr/local/lib/libopencv_core.so.3.2.0 \
    /usr/local/lib/libopencv_features2d.so \
    /usr/local/lib/libopencv_features2d.so.3.2 \
    /usr/local/lib/libopencv_features2d.so.3.2.0 \
    /usr/local/lib/libopencv_flann.so \
    /usr/local/lib/libopencv_flann.so.3.2 \
    /usr/local/lib/libopencv_flann.so.3.2.0 \
    /usr/local/lib/libopencv_highgui.so \
    /usr/local/lib/libopencv_highgui.so.3.2 \
    /usr/local/lib/libopencv_highgui.so.3.2.0 \
    /usr/local/lib/libopencv_imgcodecs.so \
    /usr/local/lib/libopencv_imgcodecs.so.3.2 \
    /usr/local/lib/libopencv_imgcodecs.so.3.2.0 \
    /usr/local/lib/libopencv_imgproc.so \
    /usr/local/lib/libopencv_imgproc.so.3.2 \
    /usr/local/lib/libopencv_imgproc.so.3.2.0 \
    /usr/local/lib/libopencv_ml.so \
    /usr/local/lib/libopencv_ml.so.3.2 \
    /usr/local/lib/libopencv_ml.so.3.2.0 \
    /usr/local/lib/libopencv_objdetect.so \
    /usr/local/lib/libopencv_objdetect.so.3.2 \
    /usr/local/lib/libopencv_objdetect.so.3.2.0 \
    /usr/local/lib/libopencv_photo.so \
    /usr/local/lib/libopencv_photo.so.3.2 \
    /usr/local/lib/libopencv_photo.so.3.2.0 \
    /usr/local/lib/libopencv_shape.so \
    /usr/local/lib/libopencv_shape.so.3.2 \
    /usr/local/lib/libopencv_shape.so.3.2.0 \
    /usr/local/lib/libopencv_stitching.so \
    /usr/local/lib/libopencv_stitching.so.3.2 \
    /usr/local/lib/libopencv_stitching.so.3.2.0 \
    /usr/local/lib/libopencv_superres.so \
    /usr/local/lib/libopencv_superres.so.3.2 \
    /usr/local/lib/libopencv_superres.so.3.2.0 \
    /usr/local/lib/libopencv_video.so \
    /usr/local/lib/libopencv_video.so.3.2 \
    /usr/local/lib/libopencv_video.so.3.2.0 \
    /usr/local/lib/libopencv_videoio.so \
    /usr/local/lib/libopencv_videoio.so.3.2 \
    /usr/local/lib/libopencv_videoio.so.3.2.0 \
    /usr/local/lib/libopencv_videostab.so \
    /usr/local/lib/libopencv_videostab.so.3.2 \
    /usr/local/lib/libopencv_videostab.so.3.2.0 \
    /usr/local/lib/libopencv_viz.so \
    /usr/local/lib/libopencv_viz.so.3.2 \
    /usr/local/lib/libopencv_viz.so.3.2.0 \
    -L/opt/pylon5/lib64 \
    -lbxapi-5.0.11 \
    -lbxapi \
    -lFirmwareUpdate_gcc_v3_0_Basler_pylon_v5_0 \
    -lGCBase_gcc_v3_0_Basler_pylon_v5_0 \
    -lGenApi_gcc_v3_0_Basler_pylon_v5_0 \
    -lgxapi-5.0.11 \
    -lgxapi \
    -llog4cpp_gcc_v3_0_Basler_pylon_v5_0 \
    -lLog_gcc_v3_0_Basler_pylon_v5_0 \
    -lMathParser_gcc_v3_0_Basler_pylon_v5_0 \
    -lNodeMapData_gcc_v3_0_Basler_pylon_v5_0 \
    -lpylonbase-5.0.11 \
    -lpylonbase \
    -lpylonc-5.0.11 \
    -lpylonc \
    -lpylon_TL_bcon-5.0.11 \
    -lpylon_TL_bcon \
    -lpylon_TL_camemu-5.0.11 \
    -lpylon_TL_camemu \
    -lpylon_TL_gige-5.0.11 \
    -lpylon_TL_gige \
    -lpylon_TL_usb-5.0.11 \
    -lpylon_TL_usb \
    -lpylonutility-5.0.11 \
    -lpylonutility \
    -luxapi-5.0.11 \
    -luxapi \
    -lXmlParser_gcc_v3_0_Basler_pylon_v5_0 \
    
    

    4、这样配置好了.pro文件,就能开始写程序了。
    在Linux中调用Basler摄像头,需要一段比较长的代码,代码如下:

    //定义是否保存图片
    #define saveImages 0
    //定义是否记录视频
    #define recordVideo 0
    
    // 加载OpenCV API
    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/video/video.hpp>
    #include<opencv2/opencv.hpp>
    
    //加载PYLON API.
    #include <pylon/PylonIncludes.h>
    
    #include <pylon/gige/BaslerGigEInstantCamera.h> //自动调节
    
    //加载C++ 头文件
    #include<iostream>
    
    
    //命名空间.
    using namespace Pylon;
    using namespace cv;
    using namespace std;
    
    using namespace Basler_GigECameraParams; //自动调节
    
    typedef Pylon::CBaslerGigEInstantCamera Camera_t; //自动调节
    typedef Camera_t::GrabResultPtr_t GrabResultPtr_t; //自动调节
    
    
    static const uint32_t c_countOfImagesToGrab = 2000;
    
    int saveImage_flag=0; //保存一张图片
    
    int main()
    {
                     cout<<"000"<<endl;
        //Pylon自动初始化和终止
        Pylon::PylonAutoInitTerm autoInitTerm;
    
        try
        {
                   cout<<"0"<<endl;
    
            // 原程序对camera的定义
            //CInstantCamera camera(CTlFactory::GetInstance().CreateFirstDevice());
    
           CDeviceInfo info;
           info.SetDeviceClass( Camera_t::DeviceClass());
    
           // Create an instant camera object with the first found camera device that matches the specified device class.
           Camera_t camera( CTlFactory::GetInstance().CreateFirstDevice( info));
    
    
                  cout<<"1"<<endl;
    
            // 打印相机的名称
            std::cout << "Using device " << camera.GetDeviceInfo().GetModelName() << endl;
                    cout<<"2"<<endl;
    
            //获取相机节点映射以获得相机参数
            GenApi::INodeMap& nodemap = camera.GetNodeMap();
                                cout<<"3"<<endl;
    
            //打开相机
            camera.Open();
                    cout<<"4"<<endl;
    
    
            //获取相机成像宽度和高度
            GenApi::CIntegerPtr width = nodemap.GetNode("Width");
            GenApi::CIntegerPtr height = nodemap.GetNode("Height");
                                cout<<"5"<<endl;
    
            //设置相机最大缓冲区,默认为10
            camera.MaxNumBuffer = 5;
            // 新建pylon ImageFormatConverter对象.
            CImageFormatConverter formatConverter;
                            cout<<"6"<<endl;
    
            //确定输出像素格式
            formatConverter.OutputPixelFormat = PixelType_BGR8packed;
            // 创建一个Pylonlmage后续将用来创建OpenCV images
            CPylonImage pylonImage;
                        cout<<"7"<<endl;
    
            //声明一个整形变量用来计数抓取的图像,以及创建文件名索引
            int grabbedlmages = 0;
    
            // 新建一个OpenCV video creator对象.
            VideoWriter cvVideoCreator;
            //新建一个OpenCV image对象.
    
            Mat openCvImage;
            // 视频文件名
                        cout<<"8"<<endl;
    
            std::string videoFileName = "openCvVideo.avi";
            // 定义视频帧大小
            cv::Size frameSize = Size((int)width->GetValue(), (int)height->GetValue());
                            cout<<"9"<<endl;
            cout<<"Width: "<<frameSize.width<<endl;
            cout<<"Height: "<<frameSize.height<<endl;
    
    
            //设置视频编码类型和帧率,有三种选择
            // 帧率必须小于等于相机成像帧率!!!!
            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);
                cout<<"10"<<endl;
    
            // 开始抓取c_countOfImagesToGrab images.
            //相机默认设置连续抓取模式
            camera.StartGrabbing(-1, GrabStrategy_LatestImageOnly); //c_countOfImagesToGrab
    
            //抓取结果数据指针
            CGrabResultPtr ptrGrabResult;
    
            // 当c_countOfImagesToGrab images获取恢复成功时,Camera.StopGrabbing()
            //被RetrieveResult()方法自动调用停止抓取
    
    
            cout << "Initial Gain = " << camera.GainRaw.GetValue() << endl;
    
    
            cout << "Initial exposure time = ";
            cout << camera.ExposureTimeAbs.GetValue() << " us" << endl;
    
    
            cout << "Initial balance ratio: ";
            camera.BalanceRatioSelector.SetValue(BalanceRatioSelector_Red);
                cout << "R = " << camera.BalanceRatioAbs.GetValue() << "   ";
            camera.BalanceRatioSelector.SetValue(BalanceRatioSelector_Green);
                cout << "G = " << camera.BalanceRatioAbs.GetValue() << "   ";
            camera.BalanceRatioSelector.SetValue(BalanceRatioSelector_Blue);
                cout << "B = " << camera.BalanceRatioAbs.GetValue() << endl;
    
    
            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
                    //显示及时影像.
                      if(!openCvImage.data)
                      {
                          cout<<"opencvImage fail"<<endl;
                          continue;
                      }
    
                    imshow("OpenCV Display Window", openCvImage);
    
                    if(saveImage_flag==0) //只保存一张图片
                    {
                        imwrite("/home/fsac/2.jpg",openCvImage);
                        saveImage_flag=1;
                    }
    
    
                    // 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);
    
                }
                else
                {
                    cout<<"图像读取失败,即ptrGrabResult->GrabSucceeded()未成功"<<endl;
                    continue;
                }
            }
    
            if(!camera.IsGrabbing())
                cout<<"camera.IsGrabbing() is failed"<<endl;
    
    
        }
        catch (GenICam::GenericException &e)
        {
            // Error handling.
            cerr << "An exception occurred." << endl
                << e.GetDescription() << endl;
        }
        return 0;
    }
    
    

    相关文章

      网友评论

        本文标题:Ubuntu16.04下配置Basler工业相机(使用QtCre

        本文链接:https://www.haomeiwen.com/subject/inwywftx.html