Opencv打开basler相机——实现显示视频、保存图片、记录视频(Windows下)
生活随笔
收集整理的這篇文章主要介紹了
Opencv打开basler相机——实现显示视频、保存图片、记录视频(Windows下)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 一、環境介紹
- 二、環境配置
- 三、完整代碼
一、環境介紹
- 系統:windows10
- 開發環境:VS2015+OpenCV3.1
- 其它庫:pylon(安裝basler官方SDK自帶的庫)
開發環境配置,可參考這里:VS2015 + OpenCV3.1 環境配置與項目搭建(C++版)
二、環境配置
1.首先根據自己的VS選擇對應的版本
2.配置包含目錄
3.配置庫目錄
4.配置附加包含目錄
5.配置附加庫目錄
6.配置附加依賴項
三、完整代碼
//定義是否保存圖片 #define saveImages 0 //定義是否記錄視頻 #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 = 800;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");//設置相機最大緩沖區,默認為10camera.MaxNumBuffer = 5;// 新建pylon ImageFormatConverter對象.CImageFormatConverter formatConverter;//確定輸出像素格式formatConverter.OutputPixelFormat = PixelType_BGR8packed;// 創建一個Pylonlmage后續將用來創建OpenCV imagesCPylonImage 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('M', 'J', 'P', 'G'), 24, 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 << "save//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 streamwaitKey(10);}}}catch (GenICam::GenericException &e){// Error handling.cerr << "An exception occurred." << endl<< e.GetDescription() << endl;}system("pause");return ; }測試結果:
溫馨提示:這里我配置的是Release x64,所以在編譯運行時,也要選擇相應的選項。
總結
以上是生活随笔為你收集整理的Opencv打开basler相机——实现显示视频、保存图片、记录视频(Windows下)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2022美国科学院院士名单公布:图灵奖得
- 下一篇: 【省钱】购物返利