-
-
Save sbrichardson/3a7a2ade56f8403d95592b2bf2f3304b 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment