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