Created
November 16, 2013 05:14
-
-
Save trentbrooks/7496260 to your computer and use it in GitHub Desktop.
Point Grey modified
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" | |
using namespace FlyCapture2; | |
//ImageEventCallback callbackFn = NULL, | |
//const void* pCallbackData = NULL ); | |
// : public ofThread | |
class PGRCamera : public ofThread | |
{ | |
public: | |
static const int camWidth = 2048;//1600;//2080;//48;//80; | |
static const int camHeight = 1536;//1200;//1552;//36;//1552; | |
Camera* mCamera; | |
//ofImage* mCamImage; | |
ofTexture* mTexture; | |
//ofMutex mutex; | |
Image* pgrImage; | |
//Image pgrImage2; | |
bool isFrameNew; | |
bool isOk; | |
bool useRawInput; | |
int frameCountSinceNew; | |
//ImageEventCallback (*TG_GetDriverVersion)(); | |
bool doShader; | |
ofShader shader; | |
bool flip; | |
int sleepTime; | |
int restartCounter; | |
PGRCamera() | |
{ | |
// custom | |
isFrameNew = false; | |
useRawInput = true; | |
doShader = false; | |
flip = true; | |
isOk = false; | |
sleepTime = 24; | |
restartCounter = 0; | |
frameCountSinceNew = 0; | |
} | |
// function pointers; | |
//ImageEventCallback (*TG_GetDriverVersion)(); | |
~PGRCamera() | |
{ | |
kill(); | |
/*waitForThread(); | |
mCamera->StopCapture(); | |
mCamera->Disconnect(); | |
delete mCamera; | |
//delete mCamImage; | |
delete mTexture; | |
pgrImage->ReleaseBuffer(); | |
delete pgrImage;*/ | |
} | |
void kill() { | |
ofLog() << "Force kill PGR"; | |
stopThread(); | |
ofLog() << "Thread stopped PGR"; | |
catchError(mCamera->StopCapture()); | |
catchError(mCamera->Disconnect()); | |
shader.unload(); | |
isFrameNew = false; | |
isOk = false; | |
frameCountSinceNew = 0; | |
delete mCamera; | |
//delete mCamImage; | |
mTexture->clear(); | |
delete mTexture; | |
mTexture = NULL; | |
pgrImage->ReleaseBuffer(); | |
delete pgrImage; | |
restartCounter++; | |
} | |
void catchError(Error error) | |
{ | |
if(error != PGRERROR_OK) | |
error.PrintErrorTrace(); | |
} | |
void catchError2(Error error) | |
{ | |
//if(error != PGRERROR_OK) | |
error.PrintErrorTrace(); | |
} | |
void printInfo(const CameraInfo& cameraInfo) | |
{ | |
printf( | |
"\n*** CAMERA INFORMATION ***\n" | |
"Serial number - %u\n" | |
"Camera model - %s\n" | |
"Camera vendor - %s\n" | |
"Sensor - %s\n" | |
"Resolution - %s\n" | |
"Firmware version - %s\n" | |
"Firmware build time - %s\n\n", | |
cameraInfo.serialNumber, | |
cameraInfo.modelName, | |
cameraInfo.vendorName, | |
cameraInfo.sensorInfo, | |
cameraInfo.sensorResolution, | |
cameraInfo.firmwareVersion, | |
cameraInfo.firmwareBuildTime); | |
} | |
void printBuildInfo() | |
{ | |
FC2Version fc2Version; | |
Utilities::GetLibraryVersion( &fc2Version ); | |
char version[128]; | |
sprintf(version,"FlyCapture2 library version: %d.%d.%d.%d\n",fc2Version.major, fc2Version.minor, fc2Version.type, fc2Version.build ); | |
printf( "%s", version ); | |
char timeStamp[512]; | |
sprintf( timeStamp, "Application build date: %s %s\n\n", __DATE__, __TIME__ ); | |
printf( "%s", timeStamp ); | |
} | |
void printFormat7Capabilities(Format7Info fmt7Info) | |
{ | |
printf( | |
"Max image pixels: (%u, %u)\n" | |
"Image Unit size: (%u, %u)\n" | |
"Offset Unit size: (%u, %u)\n" | |
"Pixel format bitfield: 0x%08x\n", | |
fmt7Info.maxWidth, | |
fmt7Info.maxHeight, | |
fmt7Info.imageHStepSize, | |
fmt7Info.imageVStepSize, | |
fmt7Info.offsetHStepSize, | |
fmt7Info.offsetVStepSize, | |
fmt7Info.pixelFormatBitField ); | |
} | |
void setup() | |
{ | |
ofLog() << "Starting PGR Camera: " << useRawInput;// << width << ", " << height; | |
printBuildInfo(); | |
BusManager busMgr; | |
unsigned int numCameras; | |
catchError(busMgr.GetNumOfCameras(&numCameras)); | |
printf("Number of cameras detected: %u\n",numCameras); | |
if(numCameras < 1) | |
{ | |
printf("Insufficient number of cameras... exiting\n"); | |
return; | |
} | |
//Init Camera | |
const Mode fmt7Mode = MODE_0; | |
const PixelFormat fmt7PixFmt = (useRawInput) ? PIXEL_FORMAT_RAW8 : PIXEL_FORMAT_RGB;//PIXEL_FORMAT_RGB8;//PIXEL_FORMAT_411YUV8;//PIXEL_FORMAT_RGB8;//PIXEL_FORMAT_MONO8; | |
Camera* camera = new Camera(); | |
PGRGuid guid; | |
CameraInfo camInfo; | |
Format7Info fmt7Info; | |
//Get First Connected Camera | |
catchError(busMgr.GetCameraFromIndex(0,&guid)); | |
//Connect to Camera | |
catchError(camera->Connect(&guid)); | |
//Get Camera Information | |
catchError(camera->GetCameraInfo(&camInfo)); | |
printInfo(camInfo); | |
//Query Format 7 Modes | |
bool supported; | |
fmt7Info.mode = fmt7Mode; | |
catchError(camera->GetFormat7Info(&fmt7Info,&supported)); | |
printFormat7Capabilities(fmt7Info); | |
if ( (fmt7PixFmt & fmt7Info.pixelFormatBitField) == 0 ) | |
{ | |
// Pixel format not supported! | |
printf("Pixel format is not supported\n"); | |
return; | |
} | |
Format7ImageSettings fmt7ImageSettings; | |
unsigned int mw = camWidth; | |
unsigned int mh = camHeight; | |
fmt7ImageSettings.mode = fmt7Mode; | |
fmt7ImageSettings.offsetX = 16;//0; | |
fmt7ImageSettings.offsetY = 8;//0; | |
fmt7ImageSettings.width = mw;//fmt7Info.maxWidth; | |
fmt7ImageSettings.height = mh;//fmt7Info.maxHeight; | |
fmt7ImageSettings.pixelFormat = fmt7PixFmt; | |
bool valid; | |
Format7PacketInfo fmt7PacketInfo; | |
//Validate Format 7 Settings | |
catchError(camera->ValidateFormat7Settings(&fmt7ImageSettings,&valid,&fmt7PacketInfo)); | |
if(!valid) | |
{ | |
printf("Format7 Settings are not Valid\n"); | |
} | |
//Set Settings to Camera | |
catchError(camera->SetFormat7Configuration(&fmt7ImageSettings,fmt7PacketInfo.recommendedBytesPerPacket)); | |
//camera-> | |
//Start Capture | |
catchError(camera->StartCapture());//PGRCamera::onFrameReceivedCb, this)); | |
//Allocate Image | |
//ofImage* image = new ofImage(); | |
//image->allocate(camWidth,camHeight,OF_IMAGE_GRAYSCALE);//OF_IMAGE_GRAYSCALE); | |
mTexture = new ofTexture(); | |
//mTexture->allocate(camWidth,camHeight,GL_RGB); | |
//mTexture->allocate(camWidth,camHeight,GL_LUMINANCE); | |
//mTexture->setTextureMinMagFilter(GL_NEAREST,GL_NEAREST); | |
pgrImage = new Image(camWidth,camHeight,PIXEL_FORMAT_RGB8);//fmt7PixFmt); | |
//pgrImage->SetDimensions(camWidth,camHeight); | |
//Init Camera & Image | |
mCamera = camera; | |
//mCamImage = image; | |
// init shader | |
if(useRawInput) { | |
shader.load("shaders/convertRaw8.vert","shaders/convertRaw8.frag"); | |
shader.setUniformTexture("source", *mTexture,0); | |
mTexture->allocate(camWidth,camHeight,GL_RGB);//GL_LUMINANCE); | |
//mTexture->setTextureMinMagFilter(GL_NEAREST,GL_NEAREST); | |
} else { | |
mTexture->allocate(camWidth,camHeight,GL_RGB); | |
} | |
isOk = true; | |
ofLog() << "Starting PGR Thread"; | |
startThread(); | |
} | |
void update() | |
{ | |
/*Camera& camera = *mCamera; | |
Image rawImage; | |
//rawImage.ReleaseBuffer(); | |
//pgrImage2.ReleaseBuffer(); | |
int p = ofGetElapsedTimeMillis(); | |
catchError(camera.RetrieveBuffer(&rawImage));//&pgrImage2)); | |
//pgrImage2.DeepCopy(&rawImage); | |
pgrImage->DeepCopy(&rawImage); | |
ofLog() << (ofGetElapsedTimeMillis()-p);*/ | |
// convert to rgb | |
//int p = ofGetElapsedTimeMillis(); | |
//Image rgbImage; | |
//rawImage.Convert( PIXEL_FORMAT_RGB, &rgbImage ); | |
//ofLog() << (ofGetElapsedTimeMillis()-p); | |
// copy data | |
//memcpy(mCamImage->getPixels(),rawImage.GetData(),rawImage.GetDataSize()); | |
//memcpy(mCamImage->getPixels(),rgbImage.GetData(),rgbImage.GetDataSize()); | |
//mTexture->loadData(rawImage.GetData(), camWidth, camHeight, GL_LUMINANCE); | |
//mTexture->loadData(pgrImage->GetData(), camWidth, camHeight, GL_RGB); | |
//pgrImage2.ReleaseBuffer(); | |
//Flip Image | |
//mCamImage->mirror(true,false); | |
//mCamImage->update(); | |
/*mutex.lock(); | |
if(isFrameNew) { | |
mTexture->loadData(pgrImage->GetData(), camWidth, camHeight, GL_LUMINANCE); | |
isFrameNew = false; | |
} | |
mutex.unlock();*/ | |
mutex.lock(); | |
//if(mutex.tryLock()) { | |
if(isFrameNew) { | |
frameCountSinceNew = 0; | |
//mutex.lock(); | |
//int p = ofGetElapsedTimeMillis(); | |
//mTexture->loadData(pgrImage->GetData(), camWidth, camHeight, GL_RGB); | |
if(useRawInput) { | |
if(doShader) shader.setUniformTexture("source", *mTexture,0); | |
mTexture->loadData(pgrImage->GetData(), camWidth, camHeight, GL_RGB);//GL_LUMINANCE); | |
} else { | |
mTexture->loadData(pgrImage->GetData(), camWidth, camHeight, GL_RGB); | |
} | |
//ofLog() << (ofGetElapsedTimeMillis()-p); | |
//mutex.unlock(); | |
isFrameNew = false; | |
} else { | |
// if we haven't received a new frame in X count, kill the camera thread and restart | |
frameCountSinceNew++; | |
if(isOk && frameCountSinceNew > 600) { | |
mutex.unlock(); | |
kill(); | |
setup(); | |
return; | |
} | |
} | |
//mutex.unlock(); | |
//} | |
mutex.unlock(); | |
} | |
void threadedFunction(){ | |
while( isThreadRunning() ){ | |
Camera& camera = *mCamera; | |
Image rawImage; | |
//PGRERROR_OK | |
//ofLog() << "Grabbing frame: " << ofGetElapsedTimef(); | |
Error didReceive = camera.RetrieveBuffer(&rawImage); | |
//catchError(camera.RetrieveBuffer(&rawImage)); | |
//rawImage. | |
if(didReceive != PGRERROR_OK) { | |
ofLog() << "PGR error: ";// << (int)didReceive; | |
didReceive.PrintErrorTrace(); | |
} else { | |
// needs to be converted from raw8 to rgb | |
if(useRawInput) { | |
//float p = ofGetElapsedTimeMillis(); | |
Image convertedImage; | |
convertedImage.SetDefaultColorProcessing(IPP);//IPP); | |
//pgrImage->SetDefaultColorProcessing(NEAREST_NEIGHBOR); | |
//pgrImage->Convert(&rawImage); | |
rawImage.Convert(PIXEL_FORMAT_RGB8,&convertedImage); | |
//ofLog() << (ofGetElapsedTimeMillis()-p); | |
//ofLog() << "frame: " << ofGetFrameNum(); | |
//rawImage.SetDefaultColorProcessing(NEAREST_NEIGHBOR); | |
mutex.lock(); | |
pgrImage->ReleaseBuffer(); | |
pgrImage->DeepCopy(&convertedImage); | |
//pgrImage2 = rawImage; | |
isFrameNew = true; | |
mutex.unlock(); | |
} else { | |
// already rgb8 format | |
mutex.lock(); | |
pgrImage->ReleaseBuffer(); | |
pgrImage->DeepCopy(&rawImage); | |
isFrameNew = true; | |
mutex.unlock(); | |
} | |
} | |
if(sleepTime > 0) sleep(sleepTime); | |
} | |
} | |
/*void imageReady(Image* pImage) { | |
mutex.lock(); | |
isFrameNew = true; | |
pgrImage = pImage; | |
//mTexture->loadData(pImage->GetData(), camWidth, camHeight, GL_LUMINANCE); | |
mutex.unlock(); | |
}*/ | |
void draw(int x = 0, int y = 0) | |
{ | |
//mCamImage->draw(0,0); | |
if(useRawInput && doShader) { | |
shader.begin(); | |
ofPushMatrix(); | |
glBegin(GL_QUADS); | |
glTexCoord2f(0,0); glVertex3f(0,0,0); | |
glTexCoord2f(camWidth,0); glVertex3f(camWidth,0,0); | |
glTexCoord2f(camWidth,camHeight); glVertex3f(camWidth,camHeight,0); | |
glTexCoord2f(0,camHeight); glVertex3f(0,camHeight,0); | |
glEnd(); | |
ofPopMatrix(); | |
shader.end(); | |
} else if(mTexture != NULL) { | |
if(flip) { | |
mTexture->draw(x+camWidth,y,-camWidth,camHeight); | |
} else { | |
mTexture->draw(x,y,camWidth,camHeight); | |
} | |
} | |
} | |
/*static void onFrameReceivedCb(Image* pImage, const void* pCallbackData) { | |
//ofLog() << "callback achieved!"; | |
//cout << "cb" << endl; | |
//PGRCamera& pgr = *reinterpret_cast<PGRCamera*>(pCallbackData); | |
PGRCamera* pgr = (PGRCamera*)pCallbackData; | |
pgr->imageReady(pImage); | |
}*/ | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, can i use this code to set the camera resolution from 1280x960 to 640x480?... i a little stuck at
fail to validate setting...