Add support for monocrome cmos ccd

Adapted mtu and packed size
Some minor fixes in gstreamer because not implemented support of v4l v4l2 in the wrapper
default beahvior must be v4l v4l2 or libv4l backends
This commit is contained in:
Stefano Fabri 2010-06-24 13:38:57 +00:00
parent 4ec4cf6876
commit 686737e24f
3 changed files with 112 additions and 39 deletions

View File

@ -291,7 +291,8 @@ enum
CV_CAP_PROP_EXPOSURE =15, CV_CAP_PROP_EXPOSURE =15,
CV_CAP_PROP_CONVERT_RGB =16, CV_CAP_PROP_CONVERT_RGB =16,
CV_CAP_PROP_WHITE_BALANCE =17, CV_CAP_PROP_WHITE_BALANCE =17,
CV_CAP_PROP_RECTIFICATION =18 CV_CAP_PROP_RECTIFICATION =18,
CV_CAP_PROP_MONOCROME =19
}; };
/* retrieve or set capture properties */ /* retrieve or set capture properties */

View File

@ -325,7 +325,7 @@ bool CvCapture_GStreamer::open( int type, const char* filename )
bool stream=false; bool stream=false;
char *uri=NULL; char *uri=NULL;
//const char *sourcetypes[] = {"dv1394src", "v4lsrc", "v4l2src", "filesrc"}; //const char *sourcetypes[] = {"dv1394src", "v4lsrc", "v4l2src", "filesrc"};
//printf("entered capturecreator %s\n", sourcetypes[type]); //printf("entered capturecreator %d\n", type);
if (type == CV_CAP_GSTREAMER_FILE) { if (type == CV_CAP_GSTREAMER_FILE) {
if (!gst_uri_is_valid(filename)) { if (!gst_uri_is_valid(filename)) {
uri=realpath(filename,NULL); uri=realpath(filename,NULL);
@ -346,8 +346,12 @@ bool CvCapture_GStreamer::open( int type, const char* filename )
} }
uridecodebin = gst_element_factory_make ("uridecodebin", NULL); uridecodebin = gst_element_factory_make ("uridecodebin", NULL);
g_object_set(G_OBJECT(uridecodebin),"uri",uri, NULL); g_object_set(G_OBJECT(uridecodebin),"uri",uri, NULL);
if(!uridecodebin) {
close();
return false;
}
} }
if(!uridecodebin) { else {
close(); close();
return false; return false;
} }
@ -595,10 +599,6 @@ void CvCapture_GStreamer::close()
} }
if(buffer) if(buffer)
gst_buffer_unref(buffer); gst_buffer_unref(buffer);
if(frame)
cvReleaseImage(&frame);
} }
double CvCapture_GStreamer::getProperty( int propId ) double CvCapture_GStreamer::getProperty( int propId )

View File

@ -52,9 +52,13 @@
#if defined(_x64) || defined (__x86_64) || defined (_WIN64) #if defined(_x64) || defined (__x86_64) || defined (_WIN64)
#define _x64 1 #define _x64 1
#elif defined(_x86) || defined(__i386) || defined (_WIN32)
#define _x86 1
#endif #endif
#include <PvApi.h> #include <PvApi.h>
#include <unistd.h>
//#include <arpa/inet.h>
#define MAX_CAMERAS 10 #define MAX_CAMERAS 10
@ -81,16 +85,21 @@ public:
} }
protected: protected:
virtual void Sleep(unsigned int time);
typedef struct typedef struct
{ {
unsigned long UID; unsigned long UID;
tPvHandle Handle; tPvHandle Handle;
tPvFrame Frame; tPvFrame Frame;
} tCamera; } tCamera;
IplImage *frame; IplImage *frame;
IplImage *grayframe;
tCamera Camera; tCamera Camera;
tPvErr Errcode; tPvErr Errcode;
bool monocrome;
}; };
@ -100,61 +109,109 @@ CvCaptureCAM_PvAPI::CvCaptureCAM_PvAPI()
{ {
} }
void CvCaptureCAM_PvAPI::Sleep(unsigned int time)
{
struct timespec t,r;
t.tv_sec = time / 1000;
t.tv_nsec = (time % 1000) * 1000000;
while(nanosleep(&t,&r)==-1)
t = r;
}
void CvCaptureCAM_PvAPI::close() void CvCaptureCAM_PvAPI::close()
{ {
// Stop the acquisition & free the camera
// Stop the acquisition & free the camera PvCommandRun(Camera.Handle, "AcquisitionStop");
PvCommandRun(Camera.Handle, "AcquisitionStop"); PvCaptureEnd(Camera.Handle);
PvCameraClose(Camera.Handle);
PvCaptureEnd(Camera.Handle);
PvCameraClose(Camera.Handle);
cvReleaseImage(&frame);
} }
// Initialize camera input // Initialize camera input
bool CvCaptureCAM_PvAPI::open( int index ) bool CvCaptureCAM_PvAPI::open( int index )
{ {
tPvCameraInfo cameraList[MAX_CAMERAS];
tPvCameraInfo camInfo;
tPvIpSettings ipSettings;
tPvCameraInfo cameraInfo[MAX_CAMERAS];
if (PvInitialize()) if (PvInitialize())
return false; return false;
usleep(250000); Sleep(1000);
//close(); //close();
int numCameras = PvCameraList(cameraInfo, MAX_CAMERAS, NULL);
int numCameras=PvCameraList(cameraList, MAX_CAMERAS, NULL);
if (numCameras <= 0 || index >= numCameras) if (numCameras <= 0 || index >= numCameras)
return false; return false;
Camera.UID = cameraInfo[index].UniqueId; Camera.UID = cameraList[index].UniqueId;
if (!PvCameraInfo(Camera.UID,&camInfo) && !PvCameraIpSettingsGet(Camera.UID,&ipSettings)) {
/*
struct in_addr addr;
addr.s_addr = ipSettings.CurrentIpAddress;
printf("Current address:\t%s\n",inet_ntoa(addr));
addr.s_addr = ipSettings.CurrentIpSubnet;
printf("Current subnet:\t\t%s\n",inet_ntoa(addr));
addr.s_addr = ipSettings.CurrentIpGateway;
printf("Current gateway:\t%s\n",inet_ntoa(addr));
*/
}
else {
fprintf(stderr,"ERROR: could not retrieve camera IP settings.\n");
return false;
}
if (PvCameraOpen(Camera.UID, ePvAccessMaster, &(Camera.Handle))==ePvErrSuccess) if (PvCameraOpen(Camera.UID, ePvAccessMaster, &(Camera.Handle))==ePvErrSuccess)
{ {
//Set Pixel Format to BRG24 to follow conventions //Set Pixel Format to BRG24 to follow conventions
Errcode = PvAttrEnumSet(Camera.Handle, "PixelFormat", "Bgr24"); /*Errcode = PvAttrEnumSet(Camera.Handle, "PixelFormat", "Bgr24");
if (Errcode != ePvErrSuccess) if (Errcode != ePvErrSuccess)
{ {
fprintf(stderr, "PvAPI: couldn't set PixelFormat to Bgr24\n"); fprintf(stderr, "PvAPI: couldn't set PixelFormat to Bgr24\n");
return NULL; return NULL;
} }
*/
tPvUint32 frameWidth, frameHeight, frameSize; tPvUint32 frameWidth, frameHeight, frameSize, maxSize;
char pixelFormat[256];
PvAttrUint32Get(Camera.Handle, "TotalBytesPerFrame", &frameSize); PvAttrUint32Get(Camera.Handle, "TotalBytesPerFrame", &frameSize);
PvAttrUint32Get(Camera.Handle, "Width", &frameWidth); PvAttrUint32Get(Camera.Handle, "Width", &frameWidth);
PvAttrUint32Get(Camera.Handle, "Height", &frameHeight); PvAttrUint32Get(Camera.Handle, "Height", &frameHeight);
//PvAttrEnumGet(Camera.Handle, "pixelFormat", pixelFormat,256,NULL);
maxSize = 8228;
// Create an image (24 bits RGB Color image) PvAttrUint32Get(Camera.Handle,"PacketSize",&maxSize);
frame = cvCreateImage(cvSize(frameWidth, frameHeight), IPL_DEPTH_8U, 3); PvCaptureAdjustPacketSize(Camera.Handle,maxSize);
frame->widthStep = frameWidth*3; //printf ("Pixel Format %s %d %d\n ", pixelFormat,frameWidth,frameHeight);
if (strncmp(pixelFormat, "Mono8",NULL)==0) {
Camera.Frame.ImageBufferSize = frameSize; grayframe = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_8U, 1);
Camera.Frame.ImageBuffer = frame->imageData; grayframe->widthStep = (int)frameWidth;
frame = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_8U, 3);
frame->widthStep = (int)frameWidth*3;
Camera.Frame.ImageBufferSize = frameSize;
Camera.Frame.ImageBuffer = grayframe->imageData;
}
else if (strncmp(pixelFormat, "Mono16",NULL)==0) {
grayframe = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_16U, 1);
grayframe->widthStep = (int)frameWidth;
frame = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_16U, 3);
frame->widthStep = (int)frameWidth*3;
Camera.Frame.ImageBufferSize = frameSize;
Camera.Frame.ImageBuffer = grayframe->imageData;
}
else if (strncmp(pixelFormat, "Bgr24",NULL)==0) {
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
return false;
// Start the camera // Start the camera
PvCaptureStart(Camera.Handle); PvCaptureStart(Camera.Handle);
@ -179,21 +236,27 @@ bool CvCaptureCAM_PvAPI::open( int index )
return true; return true;
} }
fprintf(stderr,"Error cannot open camera\n");
return false; return false;
} }
bool CvCaptureCAM_PvAPI::grabFrame() bool CvCaptureCAM_PvAPI::grabFrame()
{ {
return PvCaptureQueueFrame(Camera.Handle, &(Camera.Frame), NULL) == ePvErrSuccess; //if(Camera.Frame.Status != ePvErrUnplugged && Camera.Frame.Status != ePvErrCancelled)
return PvCaptureQueueFrame(Camera.Handle, &(Camera.Frame), NULL) == ePvErrSuccess;
} }
IplImage* CvCaptureCAM_PvAPI::retrieveFrame(int) IplImage* CvCaptureCAM_PvAPI::retrieveFrame(int)
{ {
return (PvCaptureWaitForFrameDone(Camera.Handle, &(Camera.Frame), 1000) == ePvErrSuccess) if (PvCaptureWaitForFrameDone(Camera.Handle, &(Camera.Frame), 1000) == ePvErrSuccess) {
? frame : NULL; if (!monocrome)
cvMerge(grayframe,grayframe,grayframe,NULL,frame);
return frame;
}
else return NULL;
} }
double CvCaptureCAM_PvAPI::getProperty( int property_id ) double CvCaptureCAM_PvAPI::getProperty( int property_id )
@ -224,6 +287,15 @@ bool CvCaptureCAM_PvAPI::setProperty( int property_id, double value )
PvAttrUint32Set(Camera.Handle, "Heigth", (tPvUint32)value); PvAttrUint32Set(Camera.Handle, "Heigth", (tPvUint32)value);
break; break;
*/ */
case CV_CAP_PROP_MONOCROME:
char pixelFormat[256];
PvAttrEnumGet(Camera.Handle, "PixelFormat", pixelFormat,256,NULL);
if ((strncmp(pixelFormat, "Mono8",NULL)==0) || strncmp(pixelFormat, "Mono16",NULL)==0) {
monocrome=true;
break;
}
else
return false;
default: default:
return false; return false;
} }