31

I need to use OpenCV with a GigE Vision Ethernet Camera, but I couldn't find much useful information on how to do this, any pointers, documents and example code?

I need to read frames from the camera.

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Dr. Snoopy
  • 55,122
  • 7
  • 121
  • 140

4 Answers4

20

Gig-E is a communication standard for a wide range of cameras. OpenCV now contains a wrapper for The Prosilica Gig-E based cameras (see CV_CAP_PVAPI)

But in general it's better to use the camera's native API to get the data and then use openCV to convert the returned data into an image, openCv contains a number of Bayer pattern ->RGB routines.

The CvCapture module is convenient for testing, because it can seemlessly read from a camera or a file - but it's not really suitable for high-speed real-time vision

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • 1
    specifically for GigE vision, it would be better to use Pleora's SDK, though not free. In general though you are correct to recommend the native SDK. You can still use OpenCV for other bits other than the capturing. – Jason Newton Apr 29 '15 at 07:36
  • It seems you both suggest using the native SDK for whatever camera is being used. (@JasonNewton, @MartinBeckett). Why is that? I can imagine the performance and the power of the API being better. But what if flexibility is the goal, and I don't need high power features? – kdbanman Sep 16 '15 at 22:37
  • 1
    @kdbanman You have to define flexibility - like settings are best made when using the native SDK - many things don't have a way to be set via cross camera supporting abstractions. The other is if it runs correctly / robustly... often high level drivers cant deal with the quirks of a vendor specific api. However, if you are using GigE another option exists - the Pleora SDK which is better than any other proprietary implementation. If you want all the other categories, that's the library to use, not OpenCV. It's kind of odd they didn't add support for it instead of Allied Visions' IMO. – Jason Newton Oct 08 '15 at 00:35
  • @JasonNewton, thanks for the detailed response. I'll look into Pleora - I've seen that name around in a few places. In fact, I remember seeing a "pleora" option with EmguCV - the .NET wrapper for OpenCV. – kdbanman Oct 08 '15 at 02:35
  • Using the native SDK is a good practice because they are well documented and you can get assistance from the manufacturer. Unfortunately I use 3 different cameras (2 GigE and 1 Firewire, legacy) and I have 3 different "solution configuration" to generate 3 different EXEs. It's quite a nuisance, so I'm thinking to move to GenICam. – Angelo Mascaro Aug 09 '16 at 06:02
  • The limited ability to configure the camera is definitely a setback, but IMHO the bigger issue with the OpenCV wrapper around PVApi is that it uses polling to fetch the frames. – Dan Mašek Apr 24 '17 at 20:37
9

You can do this! I used the Baumer GAPI SDK, which is a GenTL consumer. GenTL is a generic transport layer, which is a module within genIcam. You can read up on GenTL HERE. Using a GenTL consumer like Baumer's GAPI or Basler's API makes things a lot easier. They should work with any GigE camera.

I made a more comprehensive way to use Baumer's GAPI SDK in another answer HERE, so I will give a summary of what you need.

  1. Visual Studios
  2. openCV 3 for C++ (HERE is a youtube tutorial on how)
  3. Baumer GAPI SDK HERE
  4. (optional) Test your camera and network interface card using Baumer's Camera Explorer program. You need to enable jumbo packets. You may also need to configure the camera and car IP address using Baumer's IPconfig program.
  5. Setup your system Variables. refer to the programmer's guide in the Baumer GAPI SDK docs folder (should be in C:\Program Files\Baumer\Baumer GAPI SDK\Docs\Programmers_Guide). Refer to section 4.3.1.
  6. Create a new C++ project in Visual Studios and configure the properties. Refer to section 4.4.1.
  7. Go to the examples folder and look for 005_PixelTransformation example. It should be in (C:\Program Files\Baumer\Baumer GAPI SDK\Components\Examples\C++\src\0_Common\005_PixelTransformation). Copy the C++ file and paste it into the source directory of your new project.
  8. Verify you can build and compile. NOTE: You may find a problem with the part that adjusts camera parameters (exposure time for example). you should see pixel values written to the screen for the first 6 pixels in the first 6 rows, for 8 images.

Add these #include statements to the top of the .cpp source file:

#include <opencv2\core\core.hpp 
#include <opencv2\highgui\highgui.hpp> 
#include <opencv2\video\video.hpp>

Add these variable declarations at the beginning of the main() function

// OPENCV VARIABLE DECLARATIONS
cv::VideoWriter cvVideoCreator;                 // Create OpenCV video creator
cv::Mat openCvImage;                            // create an OpenCV image
cv::String videoFileName = "openCvVideo.avi";   // Define video filename
cv::Size frameSize = cv::Size(2048, 1088);      // Define video frame size
cvVideoCreator.open(videoFileName, CV_FOURCC('D', 'I', 'V', 'X'), 20, frameSize, true); // set the codec type and frame rate

In the original 005_PixelTransformation.cpp file, line 569 has a for loop that loops over 8 images, which says for(int i = 0; i < 8; i++). We want to change this to run continuously. I did this by changing it to a while loop that says

while (pDataStream->GetIsGrabbing())

Within the while loop there's an if and else statement to check the image pixel format. After the else statement closing brace and before the pImage->Release(); statement, add the following lines

// OPEN CV STUFF
openCvImage = cv::Mat(pTransformImage->GetHeight(), pTransformImage->GetWidth(), CV_8U, (int *)pTransformImage->GetBuffer());

// create OpenCV window ----
cv::namedWindow("OpenCV window: Cam", CV_WINDOW_NORMAL);

//display the current image in the window ----
cv::imshow("OpenCV window : Cam", openCvImage);
cv::waitKey(1);

Make sure you chose the correct pixel format for your openCvImage object. I chose CV_8U because my camera is mono 8 bit.

When you build and compile, you should get an openCV window which displays the live feed from your camera!

Like I said, it can be done, because I've done it. If you run into problems, refer to the programmer's guide.

enter image description here

mark jay
  • 1,256
  • 14
  • 23
  • thanks for clearing up the stack, Basler SDK on top of GeniCam. Does Basler implement the GeniCam standard in camera driver? I guess GeniCam DLLs calls Basler driver to do the frame transportation. Correct? – Rio Wing Feb 14 '18 at 03:43
  • Ok, what i feel may be a stupid question: I have a Basler USB3 camera and working code to talk to it. I use GenICam where possible, but that is only for parameter setting etc. Discovering cameras, grabbing, etc, go through Pylon functions that i assume are Basler-camera-specific. When you say Baumer and Basler APIs should work with any camera, do you mean i should be able to take my code written against the Basler API and use it with any GenTL-compatible camera? So e.g. a Baumer camera? I sadly do not have cameras from other brands, so can't test... Thanks for info! – Diederick C. Niehorster Nov 06 '19 at 12:34
4

I use an uEye GigE camera (5240) with OpenCV. It works as a cv::VideoCapture out of the box. Nevertheless using the API allows for much more control over the cameras parameters.

Herr von Wurst
  • 2,571
  • 5
  • 32
  • 53
  • Hi, I just landed here after an exhausting Google search for how to read this uEye with OpenCV, could you provide a quick code snippet for how you're doing that? EDIT: You wouldn't happen to be doing this in Linux would you? – Chriszuma Oct 23 '12 at 18:56
  • I don't have the camera here at the moment. But I remember what I did: I used the sample code for getting images from a webcam. Something like `frame = cv::Mat; cap = cv::VideoCapture(0); cap.open(); frame << cap`. When I forgot to connect the webcam OpenCV opened the uEye camera since it was the only camera connected (through GigE) to the PC. Pretty sure I was using Windows at the time though. – Herr von Wurst Oct 24 '12 at 08:39
  • so you don't need to specify the camera's ip? – user391339 Nov 20 '17 at 04:07
  • As long as there is only one camera in the system you don't need to give an IP. – Herr von Wurst Nov 21 '17 at 09:07
3

You don't mention the type of the camera and your platform. On Windows, according to the OpenCV documentation:

Currently two camera interfaces can be used on Windows: Video for Windows (VFW) and Matrox Imaging Library (MIL)

It is unlikely that your GigE camera driver supports VFW, and for MIL you need the MIL library, which is not free AFAIK.

Most GigE cameras will have an API that you can use to capture images. In most cases the API will be based on GenICam. Probably your best approach is to use the API that came with your camera, and then convert the captured image to an IplImage structure (C) or Mat class (C++).

Dani van der Meer
  • 6,169
  • 3
  • 26
  • 45
  • Thanks, yes, its on Windows. So OpenCV needs a driver to communicate with the camera? – Dr. Snoopy Jul 29 '10 at 15:45
  • Probably yes. More precisely the camera probably needs a driver to communicate with windows. And you need an API (SDK) of the camera to communicate to it from the software. I say probably, because it depends on the specific camera. – Dani van der Meer Jul 30 '10 at 07:49