Linux系统调试basler Gige接口工业相机并用C++、OpenCV开发

Linux系统带有桌面操作、basler相机(GIge接口)、路由器、Python编程环境(之前的帖子有安装步骤)

1.首先利用官网自带的软件调试通相机,软件下载地址:https://www.baslerweb.com/cn/sales-support/downloads/software-downloads/#type=pylonsoftware;version=all;os=windows;series=baslerace;model=all 根据自己的系统型号下载对应的软件安装好,这里先说一下怎么安装,先解压后里面有一个有关SDK的压缩包,再解压至opt:sudo tar -C /opt -xzf pylonSDK-5.1.0.12682-x86_64.tar.gz,在修改环境变量:cd /opt        source ./pylon5/bin/pylon-setup-env.sh pylon5。

2.将系统和相机利用路由器连接在同一个局域网内,打开软件先选择Tools,搜索相机IP并改成固定IP,就可以打开相机看实时图像了。
Linux系统调试basler Gige接口工业相机并用C++、OpenCV开发_第1张图片

Linux系统调试basler Gige接口工业相机并用C++、OpenCV开发_第2张图片

3.利用C++和OpenCV开发,这里是因为pypylon没有linux版本的,Windows下才支持pypylon(可以看之前的博客)。先讲一下opencv的安装:pip install opencv-python这是安装最新版的opencv,要根据自己的Python环境来安装(之前帖子有将linux下pychar+anaconda的安装)在安装opencv之前要保证numpy和scipy安装好的。

Linux系统调试basler Gige接口工业相机并用C++、OpenCV开发_第3张图片

这说明安装成功。之后再来安装eclipse+C++环境(之前帖子也有)。

下面正式进入开发:打开eclipse创建一个新的c++环境,下面用图来说明配置include文件和依赖库

Linux系统调试basler Gige接口工业相机并用C++、OpenCV开发_第4张图片

Linux系统调试basler Gige接口工业相机并用C++、OpenCV开发_第5张图片

Linux系统调试basler Gige接口工业相机并用C++、OpenCV开发_第6张图片

上面三张图就是添加文件路径和依赖库的步骤。

最后给出代码和效果图,这里的代码参考的是官网C++例程:

/*
 * Grab.cpp
 *
 *  Created on: 2018年11月30日
 *      Author: wenhan
 */

#define saveImages 1
// Include files to use the PYLON API.
#include 
#ifdef PYLON_WIN_BUILD
#    include 
#endif
#include 
#include 
#include 

// 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;
    // 创建一个Pylonlmage后续将用来创建OpenCV images
    CPylonImage 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;
                    // 按索引定义文件名存储图片
                   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
                //显示及时影像.
                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;
}

效果图:

Linux系统调试basler Gige接口工业相机并用C++、OpenCV开发_第7张图片

最后可以直接运行c++的程序:

cd (c++程序debug下)

./(c++名称)  attribute

你可能感兴趣的:(电子通信硬件,计算机视觉与OpenCV)