1. 程式人生 > >opencv學習筆記(2)Pylon5 SDK配置vs2015,並搭配opencv採集圖片的示例

opencv學習筆記(2)Pylon5 SDK配置vs2015,並搭配opencv採集圖片的示例

1.首先去官網下載Pylon5  window版軟體,

下載連結: https://www.baslerweb.com/cn/support/downloads/software-downloads/pylon-5-0-10-windows/

2.安裝時需要選擇developer模式,因為安裝這個模式才能在安裝的檔案中找到 Pylon SDK的配置;

3.安裝完成後進行配置,如下所示

(1)新建vs專案, 右鍵Microsoft.Cpp,點選屬性,新增 包含目錄。這個地方只要找到pylon目錄下的include。安裝pylon後就有了。(參考我的D:\Program Files\Basler\pylon 5\Development\include)


(2)配置 庫目錄, 找到pylon 目錄下的lib (我的是 D:\Program Files\Basler\pylon 5\Development\lib ,不要複製貼上,找到自己的安裝目錄)

附Pylon搭配opencv採集圖片的示例程式:

//定義是否儲存圖片
#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;
}