Using OpenCV with Arena SDK on Linux

OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library. OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in the commercial products. The following instructions describe how to configure Lucid Vision GigE cameras with OpenCV on a Linux system.

System Specifications

  • Ubuntu 18.04.1 64-bit, kernel 4.15.0-29-generic
  • Arena SDK for Linux x64, v.0.1.23.0
  • OpenCV 3.4.3

Prerequisites

Install the following dependencies:

$ sudo apt-get install build-essential
$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
$ sudo apt-get install cmake-gui

Building the OpenCV Libraries using CMake and CMake GUI

The following steps illustrate downloading and building OpenCV.

  1. Download the latest version of OpenCV from https://opencv.org/releases.html
$ wget https://github.com/opencv/opencv/archive/3.4.3.zip

Alternately:

$ git clone https://github.com/opencv/opencv.git
$ git clone https://github.com/opencv/opencv_contrib.git
  1. Find your OpenCV download in your desired location. These steps will assume the files are located at /home/ubuntu/opencv.
  2. Launch CMake-GUI (e.x. type cmake-gui from the terminal), choose your source and build directories, and click Configure. In this process, the source directory will be /home/ubuntu/opencv and the build directory will be home/ubuntu/opencv/build

opencv1-CMake-GUI

  1. Use the Unix Makefiles option in the CMakeSetup window and click Finish to begin configuration.

opencv2-CMakeSetup

  1. Choose the OpenCV components to be generated and click Generate. The default options will be used for this process. Additional components that may be desired if not already included are BUILD_EXAMPLES, BUILD_DOCS, and ENABLE_SOLUTION_FOLDERS.

opencv3-CMake

  1. Navigate to /home/ubuntu/opencv/build directory. Build the OpenCV libraries with make:
$ make -j7

The above command allows make to runs 7 jobs in parallel.

  1. Install the OpenCV libraries:
$ sudo make install
$ sudo ldconfig

Example Code

The following C++ code demonstrates using Arena SDK to grab the image from the camera, converting the image to an OpenCV Mat, and displays the image for 10 seconds. This sample assumes the attached camera is imaging in an 8-bit pixel format.

#include "opencv2/opencv.hpp"
#include "opencv2/highgui.hpp"

// [...]

GenApi::INodeMap *pNodeMap = pDevice->GetNodeMap();
gcstring windowName = Arena::GetNodeValue(pNodeMap, "DeviceModelName") + " (" + Arena::GetNodeValue(pNodeMap, "DeviceSerialNumber") + ")";

Arena::IImage *pImage;
cv::Mat img;

int framesPerSecond = (int)Arena::GetNodeValue(pNodeMap, "AcquisitionFrameRate");
int numberOfSeconds = 10;

GenApi::CFloatPtr pExposureTimeNode = pNodeMap->GetNode("ExposureTime");
const int64_t getImageTimeout_ms = static_cast(pExposureTimeNode->GetMax() / 1000 * 2);

std::cout << "Starting stream." << std::endl;
pDevice->StartStream();

for (int i = 0; i < framesPerSecond * numberOfSeconds;  i++)
{
    pImage = pDevice->GetImage(getImageTimeout_ms);
    img = cv::Mat((int)pImage->GetHeight(), (int)pImage->GetWidth(), CV_8UC1, (void *)pImage->GetData());

    cv::imshow(windowName.c_str(), img);
    cv::waitKey(1);
    pDevice->RequeueBuffer(pImage);
}
cv::destroyAllWindows();

// Stop stream
std::cout << "Stopping stream." << std::endl;
pDevice->StopStream();

Remember to include /usr/local/include in your makefile and link the to the OpenCV libraries:

INCLUDE= -I../../../include/Arena \
         -I../../../include/Save \
         -I../../../include/GenTL \
         -I../../../GenICam/library/CPP/include \
         -I/usr/local/include

LDFLAGS = -L../../../lib64 \
          -L../../../GenICam/library/lib/Linux64_x64 \
          -L/usr/local

LIBS= -larena -lsave -lgentl -lopencv_core -lopencv_highgui $

Additional Notes

  • Using the above parameters, the OpenCV include files will be installed to /usr/local/include and the binaries will be installed to /usr/local.
  • If you see the following message when attempting to run OpenCV-related display code:
Failed to load module “canberra-gtk-module”
Install the ganberra-gtk-module:
$ sudo apt-get install libcanberra-gtk-module