Created
February 3, 2014 08:38
-
-
Save dotchang/8780586 to your computer and use it in GitHub Desktop.
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 "stdafx.h" | |
#include "GigECameraCap.h" | |
using namespace FlyCapture2; | |
using namespace cv; | |
using namespace std; | |
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( version ); | |
char timeStamp[512]; | |
sprintf( timeStamp, "Application build date: %s %s\n\n", __DATE__, __TIME__ ); | |
printf( timeStamp ); | |
} | |
void PrintCameraInfo( CameraInfo* pCamInfo ) | |
{ | |
char macAddress[64]; | |
sprintf( | |
macAddress, | |
"%02X:%02X:%02X:%02X:%02X:%02X", | |
pCamInfo->macAddress.octets[0], | |
pCamInfo->macAddress.octets[1], | |
pCamInfo->macAddress.octets[2], | |
pCamInfo->macAddress.octets[3], | |
pCamInfo->macAddress.octets[4], | |
pCamInfo->macAddress.octets[5]); | |
char ipAddress[32]; | |
sprintf( | |
ipAddress, | |
"%u.%u.%u.%u", | |
pCamInfo->ipAddress.octets[0], | |
pCamInfo->ipAddress.octets[1], | |
pCamInfo->ipAddress.octets[2], | |
pCamInfo->ipAddress.octets[3]); | |
char subnetMask[32]; | |
sprintf( | |
subnetMask, | |
"%u.%u.%u.%u", | |
pCamInfo->subnetMask.octets[0], | |
pCamInfo->subnetMask.octets[1], | |
pCamInfo->subnetMask.octets[2], | |
pCamInfo->subnetMask.octets[3]); | |
char defaultGateway[32]; | |
sprintf( | |
defaultGateway, | |
"%u.%u.%u.%u", | |
pCamInfo->defaultGateway.octets[0], | |
pCamInfo->defaultGateway.octets[1], | |
pCamInfo->defaultGateway.octets[2], | |
pCamInfo->defaultGateway.octets[3]); | |
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" | |
"GigE version - %u.%u\n" | |
"User defined name - %s\n" | |
"XML URL 1 - %s\n" | |
"XML URL 2 - %s\n" | |
"MAC address - %s\n" | |
"IP address - %s\n" | |
"Subnet mask - %s\n" | |
"Default gateway - %s\n\n", | |
pCamInfo->serialNumber, | |
pCamInfo->modelName, | |
pCamInfo->vendorName, | |
pCamInfo->sensorInfo, | |
pCamInfo->sensorResolution, | |
pCamInfo->firmwareVersion, | |
pCamInfo->firmwareBuildTime, | |
pCamInfo->gigEMajorVersion, | |
pCamInfo->gigEMinorVersion, | |
pCamInfo->userDefinedName, | |
pCamInfo->xmlURL1, | |
pCamInfo->xmlURL2, | |
macAddress, | |
ipAddress, | |
subnetMask, | |
defaultGateway ); | |
} | |
void PrintStreamChannelInfo( GigEStreamChannel* pStreamChannel ) | |
{ | |
char ipAddress[32]; | |
sprintf( | |
ipAddress, | |
"%u.%u.%u.%u", | |
pStreamChannel->destinationIpAddress.octets[0], | |
pStreamChannel->destinationIpAddress.octets[1], | |
pStreamChannel->destinationIpAddress.octets[2], | |
pStreamChannel->destinationIpAddress.octets[3]); | |
printf( | |
"Network interface: %u\n" | |
"Host post: %u\n" | |
"Do not fragment bit: %s\n" | |
"Packet size: %u\n" | |
"Inter packet delay: %u\n" | |
"Destination IP address: %s\n" | |
"Source port (on camera): %u\n\n", | |
pStreamChannel->networkInterfaceIndex, | |
pStreamChannel->hostPost, | |
pStreamChannel->doNotFragment == true ? "Enabled" : "Disabled", | |
pStreamChannel->packetSize, | |
pStreamChannel->interPacketDelay, | |
ipAddress, | |
pStreamChannel->sourcePort ); | |
} | |
void PrintError( Error error ) | |
{ | |
error.PrintErrorTrace(); | |
} | |
GigEBusManager::GigEBusManager() | |
: numCamInfo(10) | |
{ | |
} | |
GigEBusManager::~GigEBusManager() | |
{ | |
if(_guid){ | |
delete [] _guid; | |
} | |
if(_interfaceType){ | |
delete [] _interfaceType; | |
} | |
} | |
int GigEBusManager::Init() | |
{ | |
error = BusManager::DiscoverGigECameras( camInfo, &numCamInfo ); | |
if (error != PGRERROR_OK) | |
{ | |
PrintError( error ); | |
return -1; | |
} | |
printf( "Number of cameras discovered: %u\n", numCamInfo ); | |
for (unsigned int i=0; i < numCamInfo; i++) | |
{ | |
PrintCameraInfo( &camInfo[i] ); | |
} | |
error = busMgr.GetNumOfCameras(&numCameras); | |
if (error != PGRERROR_OK) | |
{ | |
PrintError( error ); | |
return -1; | |
} | |
printf( "Number of cameras enumerated: %u\n", numCameras ); | |
_guid = new PGRGuid[numCameras]; | |
_interfaceType = new InterfaceType[numCameras]; | |
for(unsigned int i=0; i<numCameras; i++){ | |
error = busMgr.GetCameraFromIndex(i, &_guid[i]); | |
if(error != PGRERROR_OK) | |
{ | |
PrintError( error ); | |
return -1; | |
} | |
error = busMgr.GetInterfaceTypeFromGuid( &_guid[i], &_interfaceType[i] ); | |
if(error != PGRERROR_OK) | |
{ | |
PrintError( error ); | |
return -1; | |
} | |
} | |
return 0; | |
} | |
unsigned int GigEBusManager::num_cameras() | |
{ | |
return numCameras; | |
} | |
PGRGuid* GigEBusManager::guid(unsigned int id) | |
{ | |
if(id>=numCameras) return NULL; | |
return &_guid[id]; | |
} | |
GigECameraCap::GigECameraCap() | |
: numStreamChannels(0), imageCnt(0) | |
{ | |
} | |
GigECameraCap::~GigECameraCap() | |
{ | |
printf( "Stopping capture...\n" ); | |
if(cam.IsConnected()){ | |
// Stop capturing images | |
error = cam.StopCapture(); | |
if (error != PGRERROR_OK) | |
{ | |
PrintError( error ); | |
} | |
// Disconnect the camera | |
error = cam.Disconnect(); | |
if (error != PGRERROR_OK) | |
{ | |
PrintError( error ); | |
} | |
} | |
} | |
int GigECameraCap::Init(FlyCapture2::PGRGuid* guid) | |
{ | |
_guid = guid; | |
// Connect to a camera | |
error = cam.Connect(_guid); | |
if(error != PGRERROR_OK) | |
{ | |
PrintError( error ); | |
return -1; | |
} | |
// Get the camera information | |
error = cam.GetCameraInfo(&camInfo); | |
if(error != PGRERROR_OK) | |
{ | |
PrintError( error ); | |
return -1; | |
} | |
PrintCameraInfo(&camInfo); | |
error = cam.GetNumStreamChannels( &numStreamChannels ); | |
if(error != PGRERROR_OK) | |
{ | |
PrintError( error ); | |
return -1; | |
} | |
for (unsigned int i=0; i < numStreamChannels; i++) | |
{ | |
GigEStreamChannel streamChannel; | |
error = cam.GetGigEStreamChannelInfo( i, &streamChannel ); | |
if (error != PGRERROR_OK) | |
{ | |
PrintError( error ); | |
return -1; | |
} | |
printf( "\nPrinting stream channel information for channel %u:\n", i ); | |
PrintStreamChannelInfo( &streamChannel ); | |
} | |
printf( "Querying GigE image setting information...\n" ); | |
error = cam.GetGigEImageSettingsInfo( &imageSettingsInfo ); | |
if (error != PGRERROR_OK) | |
{ | |
PrintError( error ); | |
return -1; | |
} | |
imageSettings.offsetX = 0; | |
imageSettings.offsetY = 0; | |
imageSettings.height = imageSettingsInfo.maxHeight; | |
imageSettings.width = imageSettingsInfo.maxWidth; | |
imageSettings.pixelFormat = PIXEL_FORMAT_RAW8;// PIXEL_FORMAT_MONO8; | |
printf( "Setting GigE image settings...\n" ); | |
error = cam.SetGigEImageSettings( &imageSettings ); | |
if (error != PGRERROR_OK) | |
{ | |
PrintError( error ); | |
return -1; | |
} | |
printf( "Starting image capture...\n" ); | |
error = cam.StartCapture(); | |
if(error != PGRERROR_OK) | |
{ | |
PrintError( error ); | |
return -1; | |
} | |
cvImage = cvCreateImage( cvSize( imageSettings.width, imageSettings.height), IPL_DEPTH_8U, 3); // OpenCV画像の作成(8ビット3チャネル) | |
mat = Mat(cvImage); | |
return 0; | |
} | |
int GigECameraCap::Capture() | |
{ | |
// Retrieve an image | |
for(int i=0; i<10; i++){ // 10回試す | |
error = cam.RetrieveBuffer( &rawImage ); | |
if (error != PGRERROR_OK) { | |
PrintError( error ); | |
continue; | |
} | |
else{ | |
#ifdef _DEBUG | |
printf( "Grabbed image %d\n", imageCnt++ ); | |
#endif | |
break; | |
} | |
} | |
// Convert the raw image | |
error = rawImage.Convert( PIXEL_FORMAT_BGR, &convertedImage); | |
if (error != PGRERROR_OK){ | |
PrintError( error ); | |
return -1; | |
} | |
return 0; | |
} | |
int GigECameraCap::grap(Mat &img) | |
{ | |
Capture(); | |
memcpy( mat.data, convertedImage.GetData(), convertedImage.GetDataSize()); | |
mat.copyTo(img); | |
return 1; | |
} | |
IplImage* GigECameraCap::iplgrap() | |
{ | |
Capture(); | |
memcpy( cvImage->imageData, convertedImage.GetData(), convertedImage.GetDataSize()); | |
return cvImage; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment