//定義是否保存圖片
#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相機】 opencv下連續獲取圖片並保存
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.