2010-05-12 01:44:00 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Intel License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2000, Intel Corporation, all rights reserved.
|
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of Intel Corporation may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
// The code has been contributed by Justin G. Eskesen on 2010 Jan
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "precomp.hpp"
|
|
|
|
|
|
|
|
#ifdef HAVE_PVAPI
|
|
|
|
#if !defined WIN32 && !defined _WIN32 && !defined _LINUX
|
|
|
|
#define _LINUX
|
|
|
|
#endif
|
|
|
|
|
2010-07-17 06:38:57 +08:00
|
|
|
#if defined(_x64) || defined (__x86_64) || defined (_M_X64)
|
2010-05-12 01:44:00 +08:00
|
|
|
#define _x64 1
|
2010-07-17 06:38:57 +08:00
|
|
|
#elif defined(_x86) || defined(__i386) || defined (_M_IX86)
|
2010-06-24 21:38:57 +08:00
|
|
|
#define _x86 1
|
2010-05-12 01:44:00 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <PvApi.h>
|
2012-05-30 16:47:34 +08:00
|
|
|
#ifdef WIN32
|
|
|
|
# include <io.h>
|
|
|
|
#else
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2011-06-08 20:44:01 +08:00
|
|
|
#include <string>
|
2010-06-24 21:38:57 +08:00
|
|
|
//#include <arpa/inet.h>
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
#define MAX_CAMERAS 10
|
|
|
|
|
|
|
|
/********************* Capturing video from camera via PvAPI *********************/
|
|
|
|
|
|
|
|
class CvCaptureCAM_PvAPI : public CvCapture
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CvCaptureCAM_PvAPI();
|
2012-05-30 16:47:34 +08:00
|
|
|
virtual ~CvCaptureCAM_PvAPI()
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool open( int index );
|
|
|
|
virtual void close();
|
|
|
|
virtual double getProperty(int);
|
|
|
|
virtual bool setProperty(int, double);
|
|
|
|
virtual bool grabFrame();
|
|
|
|
virtual IplImage* retrieveFrame(int);
|
2012-05-30 16:47:34 +08:00
|
|
|
virtual int getCaptureDomain()
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
return CV_CAP_PVAPI;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2012-05-30 16:47:34 +08:00
|
|
|
#ifndef WIN32
|
|
|
|
virtual void Sleep(unsigned int time);
|
|
|
|
#endif
|
2010-06-24 21:38:57 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
unsigned long UID;
|
|
|
|
tPvHandle Handle;
|
|
|
|
tPvFrame Frame;
|
|
|
|
} tCamera;
|
2012-05-30 16:47:34 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
IplImage *frame;
|
2010-06-24 21:38:57 +08:00
|
|
|
IplImage *grayframe;
|
2012-05-30 16:47:34 +08:00
|
|
|
tCamera Camera;
|
|
|
|
tPvErr Errcode;
|
2010-06-24 21:38:57 +08:00
|
|
|
bool monocrome;
|
2010-05-12 01:44:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CvCaptureCAM_PvAPI::CvCaptureCAM_PvAPI()
|
|
|
|
{
|
2012-05-30 16:47:34 +08:00
|
|
|
monocrome=false;
|
2012-04-12 21:50:23 +08:00
|
|
|
memset(&this->Camera, 0, sizeof(this->Camera));
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2012-05-30 16:47:34 +08:00
|
|
|
|
|
|
|
#ifndef WIN32
|
2010-06-24 21:38:57 +08:00
|
|
|
void CvCaptureCAM_PvAPI::Sleep(unsigned int time)
|
|
|
|
{
|
|
|
|
struct timespec t,r;
|
2012-05-30 16:47:34 +08:00
|
|
|
|
2010-06-24 21:38:57 +08:00
|
|
|
t.tv_sec = time / 1000;
|
2012-05-30 16:47:34 +08:00
|
|
|
t.tv_nsec = (time % 1000) * 1000000;
|
|
|
|
|
2010-06-24 21:38:57 +08:00
|
|
|
while(nanosleep(&t,&r)==-1)
|
|
|
|
t = r;
|
|
|
|
}
|
2012-05-30 16:47:34 +08:00
|
|
|
#endif
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
void CvCaptureCAM_PvAPI::close()
|
|
|
|
{
|
2012-05-30 16:47:34 +08:00
|
|
|
// Stop the acquisition & free the camera
|
|
|
|
PvCommandRun(Camera.Handle, "AcquisitionStop");
|
|
|
|
PvCaptureEnd(Camera.Handle);
|
2012-04-20 21:29:10 +08:00
|
|
|
PvCameraClose(Camera.Handle);
|
|
|
|
PvUnInitialize();
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize camera input
|
2014-10-09 17:30:42 +08:00
|
|
|
bool CvCaptureCAM_PvAPI::open( int )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-06-24 21:38:57 +08:00
|
|
|
tPvCameraInfo cameraList[MAX_CAMERAS];
|
|
|
|
tPvCameraInfo camInfo;
|
|
|
|
tPvIpSettings ipSettings;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-10-09 17:30:42 +08:00
|
|
|
// Initialization parameters [500 x 10 ms = 5000 ms timeout]
|
|
|
|
int initializeTimeOut = 500;
|
2012-05-30 16:47:34 +08:00
|
|
|
|
2014-10-09 17:30:42 +08:00
|
|
|
// Disregard any errors, since this might be called several times and only needs to be called once or it will return an
|
|
|
|
// Important when wanting to use more than 1 AVT camera at the same time
|
|
|
|
PvInitialize();
|
2012-05-30 16:47:34 +08:00
|
|
|
|
2014-10-09 17:30:42 +08:00
|
|
|
while((!PvCameraCount()) && (initializeTimeOut--))
|
|
|
|
Sleep(10);
|
2010-06-24 21:38:57 +08:00
|
|
|
|
2014-10-09 17:30:42 +08:00
|
|
|
if (!initializeTimeOut){
|
|
|
|
fprintf(stderr,"ERROR: camera intialisation timeout [5000ms].\n");
|
|
|
|
return false;
|
|
|
|
}
|
2012-05-30 16:47:34 +08:00
|
|
|
|
2014-10-09 17:30:42 +08:00
|
|
|
unsigned int numCameras = PvCameraList(cameraList, MAX_CAMERAS, NULL);
|
2010-06-24 21:38:57 +08:00
|
|
|
|
2014-10-09 17:30:42 +08:00
|
|
|
// If no cameras are found
|
|
|
|
if(!numCameras)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: No cameras found.\n");
|
2010-05-12 01:44:00 +08:00
|
|
|
return false;
|
2014-10-09 17:30:42 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-10-09 17:30:42 +08:00
|
|
|
// Try opening the cameras in the list, one-by-one until a camera that is not used is found
|
|
|
|
unsigned int findNewCamera;
|
|
|
|
for(findNewCamera=0; findNewCamera<numCameras; findNewCamera++)
|
|
|
|
{
|
|
|
|
Camera.UID = cameraList[findNewCamera].UniqueId;
|
|
|
|
if(PvCameraOpen(Camera.UID, ePvAccessMaster, &(Camera.Handle))==ePvErrSuccess)
|
|
|
|
break;
|
2012-05-30 16:47:34 +08:00
|
|
|
}
|
2014-10-09 17:30:42 +08:00
|
|
|
|
|
|
|
if(findNewCamera == numCameras)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Could not find a new camera to connect to.\n");
|
2012-05-30 16:47:34 +08:00
|
|
|
return false;
|
|
|
|
}
|
2010-06-24 21:38:57 +08:00
|
|
|
|
2014-10-09 17:30:42 +08:00
|
|
|
if(PvCameraIpSettingsGet(Camera.UID,&ipSettings)==ePvErrNotFound)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "The specified camera UID %lu could not be found, PvCameraIpSettingsGet().\n", Camera.UID);
|
|
|
|
return false;
|
|
|
|
}
|
2010-06-24 21:38:57 +08:00
|
|
|
|
2014-10-09 17:30:42 +08:00
|
|
|
if(PvCameraInfo(Camera.UID,&camInfo)==ePvErrNotFound)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-10-09 17:30:42 +08:00
|
|
|
fprintf(stderr, "The specified camera UID %lu could not be found, PvCameraInfo().\n", Camera.UID);
|
|
|
|
return false;
|
|
|
|
}
|
2012-05-30 16:47:34 +08:00
|
|
|
|
2014-10-09 17:30:42 +08:00
|
|
|
tPvUint32 frameWidth, frameHeight, frameSize;
|
|
|
|
char pixelFormat[256];
|
|
|
|
PvAttrUint32Get(Camera.Handle, "TotalBytesPerFrame", &frameSize);
|
|
|
|
PvAttrUint32Get(Camera.Handle, "Width", &frameWidth);
|
|
|
|
PvAttrUint32Get(Camera.Handle, "Height", &frameHeight);
|
|
|
|
PvAttrEnumGet(Camera.Handle, "PixelFormat", pixelFormat,256,NULL);
|
2012-05-30 16:47:34 +08:00
|
|
|
|
2014-10-09 17:30:42 +08:00
|
|
|
// Start the camera
|
|
|
|
PvCaptureStart(Camera.Handle);
|
2012-05-30 16:47:34 +08:00
|
|
|
|
2014-10-09 17:30:42 +08:00
|
|
|
// Set the camera explicitly to capture data frames continuously
|
|
|
|
if(PvAttrEnumSet(Camera.Handle, "AcquisitionMode", "Continuous")!= ePvErrSuccess)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"Could not set Acquisition Mode\n");
|
|
|
|
return false;
|
|
|
|
}
|
2012-05-30 16:47:34 +08:00
|
|
|
|
2014-10-09 17:30:42 +08:00
|
|
|
if(PvCommandRun(Camera.Handle, "AcquisitionStart")!= ePvErrSuccess)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"Could not start acquisition\n");
|
|
|
|
return false;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-10-09 17:30:42 +08:00
|
|
|
if(PvAttrEnumSet(Camera.Handle, "FrameStartTriggerMode", "Freerun")!= ePvErrSuccess)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"Error setting trigger to \"Freerun\"");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Settings depending on the pixelformat
|
|
|
|
// This works for all AVT camera models that use the PvAPI interface
|
|
|
|
if (strcmp(pixelFormat, "Mono8")==0) {
|
|
|
|
monocrome = true;
|
|
|
|
grayframe = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_8U, 1);
|
|
|
|
grayframe->widthStep = (int)frameWidth;
|
|
|
|
Camera.Frame.ImageBufferSize = frameSize;
|
|
|
|
Camera.Frame.ImageBuffer = grayframe->imageData;
|
|
|
|
}
|
|
|
|
else if (strcmp(pixelFormat, "Mono16")==0) {
|
|
|
|
monocrome = true;
|
|
|
|
grayframe = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_16U, 1);
|
|
|
|
grayframe->widthStep = (int)frameWidth*2;
|
|
|
|
Camera.Frame.ImageBufferSize = frameSize;
|
|
|
|
Camera.Frame.ImageBuffer = grayframe->imageData;
|
|
|
|
}
|
|
|
|
else if (strcmp(pixelFormat, "Bgr24")==0) {
|
|
|
|
monocrome = false;
|
|
|
|
frame = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_8U, 3);
|
|
|
|
frame->widthStep = (int)frameWidth*3;
|
|
|
|
Camera.Frame.ImageBufferSize = frameSize;
|
|
|
|
Camera.Frame.ImageBuffer = frame->imageData;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
fprintf(stderr, "Pixel format %s not supported; only Mono8, Mono16 and Bgr24 are currently supported.\n", pixelFormat);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CvCaptureCAM_PvAPI::grabFrame()
|
|
|
|
{
|
2012-05-30 16:47:34 +08:00
|
|
|
return PvCaptureQueueFrame(Camera.Handle, &(Camera.Frame), NULL) == ePvErrSuccess;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
IplImage* CvCaptureCAM_PvAPI::retrieveFrame(int)
|
|
|
|
{
|
|
|
|
|
2010-06-24 21:38:57 +08:00
|
|
|
if (PvCaptureWaitForFrameDone(Camera.Handle, &(Camera.Frame), 1000) == ePvErrSuccess) {
|
2012-05-30 16:47:34 +08:00
|
|
|
if (!monocrome) {
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
return grayframe;
|
|
|
|
}
|
|
|
|
else return NULL;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
double CvCaptureCAM_PvAPI::getProperty( int property_id )
|
|
|
|
{
|
|
|
|
tPvUint32 nTemp;
|
|
|
|
|
|
|
|
switch ( property_id )
|
|
|
|
{
|
|
|
|
case CV_CAP_PROP_FRAME_WIDTH:
|
|
|
|
PvAttrUint32Get(Camera.Handle, "Width", &nTemp);
|
|
|
|
return (double)nTemp;
|
|
|
|
case CV_CAP_PROP_FRAME_HEIGHT:
|
|
|
|
PvAttrUint32Get(Camera.Handle, "Height", &nTemp);
|
|
|
|
return (double)nTemp;
|
2010-06-25 21:18:40 +08:00
|
|
|
case CV_CAP_PROP_EXPOSURE:
|
2014-10-09 17:30:42 +08:00
|
|
|
PvAttrUint32Get(Camera.Handle,"ExposureValue",&nTemp);
|
|
|
|
return (double)nTemp;
|
2011-06-08 20:44:01 +08:00
|
|
|
case CV_CAP_PROP_FPS:
|
2014-10-09 17:30:42 +08:00
|
|
|
tPvFloat32 nfTemp;
|
2011-06-08 20:44:01 +08:00
|
|
|
PvAttrFloat32Get(Camera.Handle, "StatFrameRate", &nfTemp);
|
|
|
|
return (double)nfTemp;
|
2011-06-09 15:54:10 +08:00
|
|
|
case CV_CAP_PROP_PVAPI_MULTICASTIP:
|
2014-10-09 17:30:42 +08:00
|
|
|
char mEnable[2];
|
|
|
|
char mIp[11];
|
|
|
|
PvAttrEnumGet(Camera.Handle,"MulticastEnable",mEnable,sizeof(mEnable),NULL);
|
|
|
|
if (strcmp(mEnable, "Off") == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
long int ip;
|
|
|
|
int a,b,c,d;
|
|
|
|
PvAttrStringGet(Camera.Handle, "MulticastIPAddress",mIp,sizeof(mIp),NULL);
|
|
|
|
sscanf(mIp, "%d.%d.%d.%d", &a, &b, &c, &d); ip = ((a*256 + b)*256 + c)*256 + d;
|
|
|
|
return (double)ip;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
return -1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CvCaptureCAM_PvAPI::setProperty( int property_id, double value )
|
|
|
|
{
|
|
|
|
switch ( property_id )
|
|
|
|
{
|
|
|
|
/* TODO: Camera works, but IplImage must be modified for the new size
|
|
|
|
case CV_CAP_PROP_FRAME_WIDTH:
|
|
|
|
PvAttrUint32Set(Camera.Handle, "Width", (tPvUint32)value);
|
|
|
|
break;
|
|
|
|
case CV_CAP_PROP_FRAME_HEIGHT:
|
|
|
|
PvAttrUint32Set(Camera.Handle, "Heigth", (tPvUint32)value);
|
|
|
|
break;
|
|
|
|
*/
|
2010-06-24 21:38:57 +08:00
|
|
|
case CV_CAP_PROP_MONOCROME:
|
2010-06-25 21:18:40 +08:00
|
|
|
if (value==1) {
|
2012-05-30 16:47:34 +08:00
|
|
|
char pixelFormat[256];
|
|
|
|
PvAttrEnumGet(Camera.Handle, "PixelFormat", pixelFormat,256,NULL);
|
|
|
|
if ((strcmp(pixelFormat, "Mono8")==0) || strcmp(pixelFormat, "Mono16")==0) {
|
|
|
|
monocrome=true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
monocrome=false;
|
|
|
|
break;
|
2011-06-07 03:23:43 +08:00
|
|
|
case CV_CAP_PROP_EXPOSURE:
|
2012-05-30 16:47:34 +08:00
|
|
|
if ((PvAttrUint32Set(Camera.Handle,"ExposureValue",(tPvUint32)value)==ePvErrSuccess))
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
return false;
|
2011-06-07 03:23:43 +08:00
|
|
|
case CV_CAP_PROP_PVAPI_MULTICASTIP:
|
2012-05-30 16:47:34 +08:00
|
|
|
|
|
|
|
if (value==-1) {
|
|
|
|
if ((PvAttrEnumSet(Camera.Handle,"MulticastEnable", "Off")==ePvErrSuccess))
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
std::string ip=cv::format("%d.%d.%d.%d", ((int)value>>24)&255, ((int)value>>16)&255, ((int)value>>8)&255, (int)value&255);
|
|
|
|
if ((PvAttrEnumSet(Camera.Handle,"MulticastEnable", "On")==ePvErrSuccess) &&
|
|
|
|
(PvAttrStringSet(Camera.Handle, "MulticastIPAddress", ip.c_str())==ePvErrSuccess))
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CvCapture* cvCreateCameraCapture_PvAPI( int index )
|
|
|
|
{
|
|
|
|
CvCaptureCAM_PvAPI* capture = new CvCaptureCAM_PvAPI;
|
|
|
|
|
|
|
|
if ( capture->open( index ))
|
|
|
|
return capture;
|
|
|
|
|
|
|
|
delete capture;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|