Skip to content

Instantly share code, notes, and snippets.

@ptweir
Created July 7, 2016 00:51
Show Gist options
  • Select an option

  • Save ptweir/a9237454db3eb3ceb1a0f851a92d370a to your computer and use it in GitHub Desktop.

Select an option

Save ptweir/a9237454db3eb3ceb1a0f851a92d370a to your computer and use it in GitHub Desktop.
my modified version of the Point Grey camera save to avi example
//=============================================================================
// Copyright © 2009 Point Grey Research, Inc. All Rights Reserved.
//
// This software is the confidential and proprietary information of Point
// Grey Research, Inc. ("Confidential Information"). You shall not
// disclose such Confidential Information and shall use it only in
// accordance with the terms of the license agreement you entered into
// with PGR.
//
// PGR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
// SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE, OR NON-INFRINGEMENT. PGR SHALL NOT BE LIABLE FOR ANY DAMAGES
// SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
// THIS SOFTWARE OR ITS DERIVATIVES.
//=============================================================================
//=============================================================================
// $Id: SaveImageToAviEx.cpp,v 1.10 2009-12-08 18:58:36 soowei Exp $
//=============================================================================
#include "stdafx.h"
#include "FlyCapture2.h"
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace FlyCapture2;
using namespace std;
enum AviType
{
UNCOMPRESSED,
MJPG,
H264
};
void PrintBuildInfo()
{
FC2Version fc2Version;
Utilities::GetLibraryVersion( &fc2Version );
ostringstream version;
version << "FlyCapture2 library version: " << fc2Version.major << "." << fc2Version.minor << "." << fc2Version.type << "." << fc2Version.build;
cout << version.str() << endl;
ostringstream timeStamp;
timeStamp << "Application build date: " << __DATE__ << " " << __TIME__;
cout << timeStamp.str() << endl << endl;
}
void PrintCameraInfo( CameraInfo* pCamInfo )
{
cout << endl;
cout << "*** CAMERA INFORMATION ***" << endl;
cout << "Serial number -" << pCamInfo->serialNumber << endl;
cout << "Camera model - " << pCamInfo->modelName << endl;
cout << "Camera vendor - " << pCamInfo->vendorName << endl;
cout << "Sensor - " << pCamInfo->sensorInfo << endl;
cout << "Resolution - " << pCamInfo->sensorResolution << endl;
cout << "Firmware version - " << pCamInfo->firmwareVersion << endl;
cout << "Firmware build time - " << pCamInfo->firmwareBuildTime << endl << endl;
}
void PrintError( Error error )
{
error.PrintErrorTrace();
}
void SaveAviHelper(
AviType aviType,
std::vector<Image>& vecImages,
std::string aviFileName,
float frameRate)
{
Error error;
AVIRecorder aviRecorder;
// Open the AVI file for appending images
switch (aviType)
{
case UNCOMPRESSED:
{
AVIOption option;
option.frameRate = frameRate;
error = aviRecorder.AVIOpen(aviFileName.c_str(), &option);
}
break;
case MJPG:
{
MJPGOption option;
option.frameRate = frameRate;
option.quality = 75;
error = aviRecorder.AVIOpen(aviFileName.c_str(), &option);
}
break;
case H264:
{
H264Option option;
option.frameRate = frameRate;
option.bitrate = 1000000;
option.height = vecImages[0].GetRows();
option.width = vecImages[0].GetCols();
error = aviRecorder.AVIOpen(aviFileName.c_str(), &option);
}
break;
}
if (error != PGRERROR_OK)
{
PrintError(error);
return;
}
cout << endl;
cout << "Appending " << vecImages.size() << " images to AVI file: " << aviFileName.c_str() << endl;
for (int imageCnt = 0; imageCnt < vecImages.size(); imageCnt++)
{
// Append the image to AVI file
error = aviRecorder.AVIAppend(&vecImages[imageCnt]);
if (error != PGRERROR_OK)
{
PrintError(error);
continue;
}
cout << "Appended image " << imageCnt << "..." << endl;
}
// Close the AVI file
error = aviRecorder.AVIClose( );
if (error != PGRERROR_OK)
{
PrintError(error);
return;
}
}
int RunCamera( PGRGuid guid )
{
const int k_numImages = 3550;
Error error;
Camera cam;
// Connect to a camera
error = cam.Connect(&guid);
if (error != PGRERROR_OK)
{
PrintError(error);
return -1;
}
// Get the camera information
CameraInfo camInfo;
error = cam.GetCameraInfo(&camInfo);
if (error != PGRERROR_OK)
{
PrintError(error);
return -1;
}
PrintCameraInfo(&camInfo);
// Start capturing images
cout << "Starting capture... " << endl;
error = cam.StartCapture();
if (error != PGRERROR_OK)
{
PrintError(error);
return -1;
}
std::vector<Image> vecImages;
vecImages.resize(k_numImages);
// Grab images
Image rawImage;
for ( int imageCnt=0; imageCnt < k_numImages; imageCnt++ )
{
error = cam.RetrieveBuffer(&rawImage);
if (error != PGRERROR_OK)
{
cout << "Error grabbing image " << imageCnt << endl;
continue;
}
else
{
cout << "Grabbed image " << imageCnt << endl;
}
vecImages[imageCnt].DeepCopy(&rawImage);
}
// Stop capturing images
cout << "Stopping capture... " << endl;
error = cam.StopCapture();
if (error != PGRERROR_OK)
{
PrintError(error);
return -1;
}
// Check if the camera supports the FRAME_RATE property
cout << "Detecting frame rate from camera... " << endl;
PropertyInfo propInfo;
propInfo.type = FRAME_RATE;
error = cam.GetPropertyInfo( &propInfo );
if (error != PGRERROR_OK)
{
PrintError(error);
return -1;
}
float frameRateToUse = 50.0f;
if ( propInfo.present == true )
{
// Get the frame rate
Property prop;
prop.type = FRAME_RATE;
error = cam.GetProperty( &prop );
if (error != PGRERROR_OK)
{
PrintError(error);
}
else
{
// Set the frame rate.
// Note that the actual recording frame rate may be slower,
// depending on the bus speed and disk writing speed.
frameRateToUse = prop.absValue;
}
}
cout << "Using frame rate of " << fixed << setprecision(1) << frameRateToUse << endl;
ostringstream aviFileName;
aviFileName << "uncompressed-" << camInfo.serialNumber;
SaveAviHelper(UNCOMPRESSED, vecImages, aviFileName.str().c_str(), frameRateToUse);
// Disconnect the camera
error = cam.Disconnect();
if (error != PGRERROR_OK)
{
PrintError(error);
return -1;
}
return 0;
}
int main(int /*argc*/, char** /*argv*/)
{
PrintBuildInfo();
Error error;
// Since this application saves images in the current folder
// we must ensure that we have permission to write to this folder.
// If we do not have permission, fail right away.
FILE* tempFile = fopen("test.txt", "w+");
if (tempFile == NULL)
{
cout << "Failed to create file in current folder. Please check permissions." << endl;
return -1;
}
fclose(tempFile);
remove("test.txt");
BusManager busMgr;
unsigned int numCameras;
error = busMgr.GetNumOfCameras(&numCameras);
if (error != PGRERROR_OK)
{
PrintError(error);
return -1;
}
if ( numCameras < 1 )
{
cout << "No camera detected." << endl;
return -1;
}
else
{
cout << "Number of cameras detected: " << numCameras << endl;
}
PGRGuid guid;
error = busMgr.GetCameraFromIndex(0, &guid);
if (error != PGRERROR_OK)
{
PrintError(error);
return -1;
}
cout << "Running the first camera." << endl;
RunCamera( guid );
cout << "Done!" << endl;
return 0;
}
@ptweir
Copy link
Author

ptweir commented Jul 7, 2016

related to [https://www.ptgrey.com/KB/10548]

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