Skip to content

Instantly share code, notes, and snippets.

@kevinhughes27
Last active January 24, 2024 19:39
Show Gist options
  • Star 35 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save kevinhughes27/5543668 to your computer and use it in GitHub Desktop.
Save kevinhughes27/5543668 to your computer and use it in GitHub Desktop.
A simple program showing how to capture from a Point Grey Research Camera and display the image using OpenCV
#include "FlyCapture2.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace FlyCapture2;
int main()
{
Error error;
Camera camera;
CameraInfo camInfo;
// Connect the camera
error = camera.Connect( 0 );
if ( error != PGRERROR_OK )
{
std::cout << "Failed to connect to camera" << std::endl;
return false;
}
// Get the camera info and print it out
error = camera.GetCameraInfo( &camInfo );
if ( error != PGRERROR_OK )
{
std::cout << "Failed to get camera info from camera" << std::endl;
return false;
}
std::cout << camInfo.vendorName << " "
<< camInfo.modelName << " "
<< camInfo.serialNumber << std::endl;
error = camera.StartCapture();
if ( error == PGRERROR_ISOCH_BANDWIDTH_EXCEEDED )
{
std::cout << "Bandwidth exceeded" << std::endl;
return false;
}
else if ( error != PGRERROR_OK )
{
std::cout << "Failed to start image capture" << std::endl;
return false;
}
// capture loop
char key = 0;
while(key != 'q')
{
// Get the image
Image rawImage;
Error error = camera.RetrieveBuffer( &rawImage );
if ( error != PGRERROR_OK )
{
std::cout << "capture error" << std::endl;
continue;
}
// convert to rgb
Image rgbImage;
rawImage.Convert( FlyCapture2::PIXEL_FORMAT_BGR, &rgbImage );
// convert to OpenCV Mat
unsigned int rowBytes = (double)rgbImage.GetReceivedDataSize()/(double)rgbImage.GetRows();
cv::Mat image = cv::Mat(rgbImage.GetRows(), rgbImage.GetCols(), CV_8UC3, rgbImage.GetData(),rowBytes);
cv::imshow("image", image);
key = cv::waitKey(30);
}
error = camera.StopCapture();
if ( error != PGRERROR_OK )
{
// This may fail when the camera was removed, so don't show
// an error message
}
camera.Disconnect();
return 0;
}
Copy link

ghost commented Jan 31, 2015

Thanks, Kevin! This really great!

@jonnew
Copy link

jonnew commented Mar 25, 2015

Thanks worked well for me! Note that the call the rgbImage.GetData() returns a pointer to the point grey image data. So if this this is used within a function that returns a cv::Mat, one must put the Image rawImage and Image rgbImage declarations within the scope that the resulting converted image (cv::Mat) rather than within the function body or a segfault will result.

@vandanv
Copy link

vandanv commented Apr 11, 2015

How is the above code compiled? I tried g++ -o outputfile filename.cpp 'pkg-config opencv --cflags --libs'.
Im an getting an error as :Undefined reference to 'Flycapture2: : Error : : Error()' and so on..Is there any other method?
Thank you.

@burakkurtt
Copy link

hey vandanv,
try: g++ -o balltrack balltrack.cpp pkg-config opencv --cflags --libs -L ../flycapture.2.9.3.43_armhf/lib -lflycapture
it is worked for me

Copy link

ghost commented Dec 7, 2016

I get a segmentaion fault when calling

camera.StartCapture();

Does anyone know this issue?

Update:
this helped:

sudo sh -c 'echo 1000 > /sys/module/usbcore/parameters/usbfs_memory_mb'

@ashnarayan13
Copy link

How do I run this file? I tried adding the package to CMakeLists but I get the same errors as @vandanv. I tried the g++ option, i get g++: error: pkg-config: No such file or directory
Thanks!

@BhanuKiranChaluvadi
Copy link

BhanuKiranChaluvadi commented Jul 19, 2017

Thank you for sharing the code. It took no time to interface, just like a plug and play.

@arteagac
Copy link

arteagac commented Apr 8, 2018

@ashnarayan13 to run the file you need to make sure that you include headers and libs for flycapture and opencv. You can use for example:

g++ -std=c++11 mycppfile.cpp -I/usr/include/flycapture -lflycapture `pkg-config --libs --cflags opencv` -o mycppfile

Also, make sure FlyCaptureSDK is properly installed.

@anilyuce
Copy link

Thanks for the code Kevin. Do you (or anyone else) by any chance have any experience with the SetUserBuffers function from FlyCapture2? The documentation is quite scarce and I was wondering if the image.convert operation also uses the same buffer for the rgbimage in this case.

@vgenten
Copy link

vgenten commented Jul 27, 2018

My program gets stuck in line 53 on "camera.RetrieveBuffer( &rawImage );"
But I don't get any error message . I tried to make a console output immediately after that call, but I don't receive anything.
Do you have an idea what the problem could be?

@vefak
Copy link

vefak commented Aug 26, 2020

hi, when run the code, the image that grabbing is in Gray format not RGB? Anyone have same problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment