Linux系统调试basler Gige接口工业相机并用C++、OpenCV开发
Linux系統(tǒng)帶有桌面操作、basler相機(jī)(GIge接口)、路由器、Python編程環(huán)境(之前的帖子有安裝步驟)
1.首先利用官網(wǎng)自帶的軟件調(diào)試通相機(jī),軟件下載地址:https://www.baslerweb.com/cn/sales-support/downloads/software-downloads/#type=pylonsoftware;version=all;os=windows;series=baslerace;model=all?根據(jù)自己的系統(tǒng)型號(hào)下載對(duì)應(yīng)的軟件安裝好,這里先說一下怎么安裝,先解壓后里面有一個(gè)有關(guān)SDK的壓縮包,再解壓至opt:sudo tar -C /opt -xzf pylonSDK-5.1.0.12682-x86_64.tar.gz,在修改環(huán)境變量:cd /opt ? ? ? ?source ./pylon5/bin/pylon-setup-env.sh pylon5。
2.將系統(tǒng)和相機(jī)利用路由器連接在同一個(gè)局域網(wǎng)內(nèi),打開軟件先選擇Tools,搜索相機(jī)IP并改成固定IP,就可以打開相機(jī)看實(shí)時(shí)圖像了。
3.利用C++和OpenCV開發(fā),這里是因?yàn)閜ypylon沒有l(wèi)inux版本的,Windows下才支持pypylon(可以看之前的博客)。先講一下opencv的安裝:pip install opencv-python這是安裝最新版的opencv,要根據(jù)自己的Python環(huán)境來安裝(之前帖子有將linux下pychar+anaconda的安裝)在安裝opencv之前要保證numpy和scipy安裝好的。
這說明安裝成功。之后再來安裝eclipse+C++環(huán)境(之前帖子也有)。
下面正式進(jìn)入開發(fā):打開eclipse創(chuàng)建一個(gè)新的c++環(huán)境,下面用圖來說明配置include文件和依賴庫
上面三張圖就是添加文件路徑和依賴庫的步驟。
最后給出代碼和效果圖,這里的代碼參考的是官網(wǎng)C++例程:
/** Grab.cpp** Created on: 2018年11月30日* Author: wenhan*/#define saveImages 1 // Include files to use the PYLON API. #include <pylon/PylonIncludes.h> #ifdef PYLON_WIN_BUILD # include <pylon/PylonGUI.h> #endif #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/video/video.hpp>// Namespace for using pylon objects. using namespace Pylon;// Namespace for using cout. using namespace std; using namespace cv;// Number of images to be grabbed. static const uint32_t c_countOfImagesToGrab = 100; //int main(int argc, char* argv[]) {Mat src;CImageFormatConverter formatConverter;formatConverter.OutputPixelFormat = PixelType_BGR8packed;int grabbedlmages = 0;// 創(chuàng)建一個(gè)Pylonlmage后續(xù)將用來創(chuàng)建OpenCV imagesCPylonImage pylonImage;// The exit code of the sample application.int exitCode = 0;// Before using any pylon methods, the pylon runtime must be initialized.PylonInitialize();try{// Create an instant camera object with the camera device found first.CInstantCamera camera( CTlFactory::GetInstance().CreateFirstDevice());// Print the model name of the camera.cout << "Using device " << camera.GetDeviceInfo().GetModelName() << endl;// The parameter MaxNumBuffer can be used to control the count of buffers// allocated for grabbing. The default value of this parameter is 10.camera.MaxNumBuffer = 5;// Start the grabbing of c_countOfImagesToGrab images.// The camera device is parameterized with a default configuration which// sets up free-running continuous acquisition.camera.StartGrabbing( c_countOfImagesToGrab);// This smart pointer will receive the grab result data.CGrabResultPtr ptrGrabResult;// Camera.StopGrabbing() is called automatically by the RetrieveResult() method// when c_countOfImagesToGrab images have been retrieved.while ( camera.IsGrabbing()){// Wait for an image and then retrieve it. A timeout of 5000 ms is used.camera.RetrieveResult( 5000, ptrGrabResult, TimeoutHandling_ThrowException);// Image grabbed successfully?if (ptrGrabResult->GrabSucceeded()){// Access the image data.cout << "SizeX: " << ptrGrabResult->GetWidth() << endl;cout << "SizeY: " << ptrGrabResult->GetHeight() << endl;formatConverter.Convert(pylonImage, ptrGrabResult);src = cv::Mat(ptrGrabResult->GetHeight(), ptrGrabResult->GetWidth(), CV_8UC3, (uint8_t *) pylonImage.GetBuffer());//如果需要保存圖片if (saveImages){std::ostringstream s;// 按索引定義文件名存儲(chǔ)圖片s << "/home/wenhan/img/image_" << grabbedlmages << ".jpg";std::string imageName(s.str());//保存OpenCV image.imwrite(imageName, src);grabbedlmages++;}//新建OpenCV display window.namedWindow("OpenCV Display Window", CV_WINDOW_NORMAL); // other options: CV_AUTOSIZE, CV_FREERATIO//顯示及時(shí)影像.imshow("OpenCV Display Window", src);waitKey(1);const uint8_t *pImageBuffer = (uint8_t *) ptrGrabResult->GetBuffer();cout << "Gray value of first pixel: " << (uint32_t) pImageBuffer[0] << endl << endl;#ifdef PYLON_WIN_BUILD// Display the grabbed image.Pylon::DisplayImage(1, ptrGrabResult); #endif}else{cout << "Error: " << ptrGrabResult->GetErrorCode() << " " << ptrGrabResult->GetErrorDescription() << endl;}}}catch (const GenericException &e){// Error handling.cerr << "An exception occurred." << endl<< e.GetDescription() << endl;exitCode = 1;}// Comment the following two lines to disable waiting on exit.cerr << endl << "Press Enter to exit." << endl;while( cin.get() != '\n');// Releases all pylon resources.PylonTerminate();return exitCode; }效果圖:
最后可以直接運(yùn)行c++的程序:
cd (c++程序debug下)
./(c++名稱) ?attribute
總結(jié)
以上是生活随笔為你收集整理的Linux系统调试basler Gige接口工业相机并用C++、OpenCV开发的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux安装和入门
- 下一篇: linux 其他常用命令