cap_pvapi.cpp

Ricardo Crudo, 2012-04-07 02:48 am

Download (11.1 kB)

 
1
////////////////////////////////////////////////////////////////////////////////////////
2
//
3
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
//  By downloading, copying, installing or using the software you agree to this license.
6
//  If you do not agree to this license, do not download, install,
7
//  copy or use the software.
8
//
9
//
10
//                        Intel License Agreement
11
//                For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000, Intel Corporation, all rights reserved.
14
// Third party copyrights are property of their respective owners.
15
//
16
// Redistribution and use in source and binary forms, with or without modification,
17
// are permitted provided that the following conditions are met:
18
//
19
//   * Redistribution's of source code must retain the above copyright notice,
20
//     this list of conditions and the following disclaimer.
21
//
22
//   * Redistribution's in binary form must reproduce the above copyright notice,
23
//     this list of conditions and the following disclaimer in the documentation
24
//     and/or other materials provided with the distribution.
25
//
26
//   * The name of Intel Corporation may not be used to endorse or promote products
27
//     derived from this software without specific prior written permission.
28
//
29
// This software is provided by the copyright holders and contributors "as is" and
30
// any express or implied warranties, including, but not limited to, the implied
31
// warranties of merchantability and fitness for a particular purpose are disclaimed.
32
// In no event shall the Intel Corporation or contributors be liable for any direct,
33
// indirect, incidental, special, exemplary, or consequential damages
34
// (including, but not limited to, procurement of substitute goods or services;
35
// loss of use, data, or profits; or business interruption) however caused
36
// and on any theory of liability, whether in contract, strict liability,
37
// or tort (including negligence or otherwise) arising in any way out of
38
// the use of this software, even if advised of the possibility of such damage.
39
//
40
//
41
42
//
43
// The code has been contributed by Justin G. Eskesen on 2010 Jan
44
//
45
46
#include "precomp.hpp"
47
48
#ifdef HAVE_PVAPI
49
#if !defined WIN32 && !defined _WIN32 && !defined _LINUX
50
#define _LINUX
51
#endif
52
53
#if defined(_x64) || defined (__x86_64) || defined (_M_X64)
54
#define _x64 1
55
#elif defined(_x86) || defined(__i386) || defined (_M_IX86)
56
#define _x86 1
57
#endif
58
59
#include <PvApi.h>
60
#include <unistd.h>
61
#include <string>
62
#include <arpa/inet.h>
63
64
#define MAX_CAMERAS 10
65
66
/********************* Capturing video from camera via PvAPI *********************/
67
68
typedef struct
69
{
70
    unsigned long   UID;
71
    tPvHandle       Handle;
72
    tPvFrame        Frame;
73
} tCamera;
74
75
76
class CvCaptureCAM_PvAPI : public CvCapture
77
{
78
public:
79
    CvCaptureCAM_PvAPI();
80
    virtual ~CvCaptureCAM_PvAPI() 
81
    {
82
        close();
83
    }
84
85
    virtual bool open( int index );
86
    virtual void close();
87
    virtual double getProperty(int);
88
    virtual bool setProperty(int, double);
89
    virtual bool grabFrame();
90
    virtual IplImage* retrieveFrame(int);
91
    virtual int getCaptureDomain() 
92
    {
93
        return CV_CAP_PVAPI;
94
    }
95
    tCamera Camera;
96
97
protected:
98
        virtual void Sleep(unsigned int time);
99
100
    IplImage *frame;
101
    IplImage *grayframe;    
102
    tPvErr    Errcode;
103
    bool monocrome;
104
105
106
};
107
108
109
CvCaptureCAM_PvAPI::CvCaptureCAM_PvAPI()
110
{
111
        monocrome=false;
112
}
113
void CvCaptureCAM_PvAPI::Sleep(unsigned int time)
114
{
115
    struct timespec t,r;
116
    
117
    t.tv_sec    = time / 1000;
118
    t.tv_nsec   = (time % 1000) * 1000000;    
119
    
120
    while(nanosleep(&t,&r)==-1)
121
        t = r;
122
}
123
void CvCaptureCAM_PvAPI::close()
124
{
125
        // Stop the acquisition & free the camera
126
        PvCommandRun(Camera.Handle, "AcquisitionStop");
127
        PvCaptureEnd(Camera.Handle);
128
        PvCameraClose(Camera.Handle);        
129
}
130
131
// Initialize camera input
132
bool CvCaptureCAM_PvAPI::open( int index )
133
{
134
    tPvCameraInfo cameraList[MAX_CAMERAS];
135
    
136
    tPvCameraInfo  camInfo;
137
    tPvIpSettings ipSettings;
138
    tPvErr errCode;
139
140
 
141
        if((errCode = PvInitialize()) != ePvErrSuccess)
142
    { 
143
        fprintf(stderr,"PvInitialize err: %u\n", errCode);
144
        return false;
145
        }        
146
        
147
    Sleep(1000);
148
149
    //close();    
150
    
151
    int numCameras=PvCameraList(cameraList, MAX_CAMERAS, NULL);
152
153
    if (numCameras <= 0 || index >= numCameras)
154
        return false;
155
156
    Camera.UID = cameraList[index].UniqueId;    
157
158
    if (!PvCameraInfo(Camera.UID,&camInfo) && !PvCameraIpSettingsGet(Camera.UID,&ipSettings)) {
159
                
160
                /*
161
        struct in_addr addr;
162
                addr.s_addr = ipSettings.CurrentIpAddress;
163
                printf("Current address:\t%s\n",inet_ntoa(addr));
164
                addr.s_addr = ipSettings.CurrentIpSubnet;
165
                printf("Current subnet:\t\t%s\n",inet_ntoa(addr));
166
                addr.s_addr = ipSettings.CurrentIpGateway;
167
                printf("Current gateway:\t%s\n",inet_ntoa(addr));
168
                */
169
        }        
170
        else {
171
                fprintf(stderr,"ERROR: could not retrieve camera IP settings.\n");
172
                return false;
173
        }        
174
175
176
    if (PvCameraOpen(Camera.UID, ePvAccessMaster, &(Camera.Handle))==ePvErrSuccess)
177
    {
178
    
179
        //Set Pixel Format to BRG24 to follow conventions 
180
        /*Errcode = PvAttrEnumSet(Camera.Handle, "PixelFormat", "Bgr24");
181
        if (Errcode != ePvErrSuccess)
182
        {
183
            fprintf(stderr, "PvAPI: couldn't set PixelFormat to Bgr24\n");
184
            return NULL;
185
        }
186
        */
187
        tPvUint32 frameWidth, frameHeight, frameSize;
188
        unsigned long maxSize;
189
                char pixelFormat[256];
190
        PvAttrUint32Get(Camera.Handle, "TotalBytesPerFrame", &frameSize);
191
        PvAttrUint32Get(Camera.Handle, "Width", &frameWidth);
192
        PvAttrUint32Get(Camera.Handle, "Height", &frameHeight);
193
        PvAttrEnumGet(Camera.Handle, "PixelFormat", pixelFormat,256,NULL);
194
        maxSize = 8228;
195
        //PvAttrUint32Get(Camera.Handle,"PacketSize",&maxSize);
196
        if (PvCaptureAdjustPacketSize(Camera.Handle,maxSize)!=ePvErrSuccess)
197
                        return false;
198
        if (strcmp(pixelFormat, "Mono8")==0) {
199
                                grayframe = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_8U, 1);
200
                                grayframe->widthStep = (int)frameWidth;
201
                                frame = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_8U, 3);
202
                                frame->widthStep = (int)frameWidth*3;                 
203
                                Camera.Frame.ImageBufferSize = frameSize;
204
                                Camera.Frame.ImageBuffer = grayframe->imageData;   
205
                }            
206
                else if (strcmp(pixelFormat, "Mono16")==0) {
207
                                grayframe = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_16U, 1);
208
                                grayframe->widthStep = (int)frameWidth;        
209
                                frame = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_16U, 3);
210
                                frame->widthStep = (int)frameWidth*3;
211
                                Camera.Frame.ImageBufferSize = frameSize;
212
                                Camera.Frame.ImageBuffer = grayframe->imageData;
213
                }          
214
                else if        (strcmp(pixelFormat, "Bgr24")==0) {
215
                                frame = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_8U, 3);
216
                                frame->widthStep = (int)frameWidth*3;
217
                                Camera.Frame.ImageBufferSize = frameSize;
218
                                Camera.Frame.ImageBuffer = frame->imageData;
219
                }                
220
                else
221
                                return false;
222
        // Start the camera
223
        PvCaptureStart(Camera.Handle);
224
225
        // Set the camera to capture continuously
226
        if(PvAttrEnumSet(Camera.Handle, "AcquisitionMode", "Continuous")!= ePvErrSuccess)
227
        {
228
            fprintf(stderr,"Could not set Prosilica Acquisition Mode\n");
229
            return false;
230
        }
231
        
232
        if(PvCommandRun(Camera.Handle, "AcquisitionStart")!= ePvErrSuccess)
233
        {
234
            fprintf(stderr,"Could not start Prosilica acquisition\n");
235
            return false;
236
        }
237
        
238
        if(PvAttrEnumSet(Camera.Handle, "FrameStartTriggerMode", "Freerun")!= ePvErrSuccess)
239
        {
240
            fprintf(stderr,"Error setting Prosilica trigger to \"Freerun\"");
241
            return false;
242
        }
243
        
244
        return true;
245
    }
246
    fprintf(stderr,"Error cannot open camera\n");
247
    return false;
248
249
}
250
251
bool CvCaptureCAM_PvAPI::grabFrame()
252
{
253
        //if(Camera.Frame.Status != ePvErrUnplugged && Camera.Frame.Status != ePvErrCancelled)
254
        return PvCaptureQueueFrame(Camera.Handle, &(Camera.Frame), NULL) == ePvErrSuccess;
255
}
256
257
258
IplImage* CvCaptureCAM_PvAPI::retrieveFrame(int)
259
{
260
261
    if (PvCaptureWaitForFrameDone(Camera.Handle, &(Camera.Frame), 1000) == ePvErrSuccess) {
262
                if (!monocrome) {
263
                        cvMerge(grayframe,grayframe,grayframe,NULL,frame); 
264
                        return frame;        
265
                }        
266
                return grayframe;
267
    }                
268
    else return NULL;                
269
}
270
271
double CvCaptureCAM_PvAPI::getProperty( int property_id )
272
{
273
    tPvUint32 nTemp;
274
275
    switch ( property_id )
276
    {
277
    case CV_CAP_PROP_FRAME_WIDTH:
278
        PvAttrUint32Get(Camera.Handle, "Width", &nTemp);
279
        return (double)nTemp;
280
    case CV_CAP_PROP_FRAME_HEIGHT:
281
        PvAttrUint32Get(Camera.Handle, "Height", &nTemp);
282
        return (double)nTemp;
283
    case CV_CAP_PROP_EXPOSURE:
284
        PvAttrUint32Get(Camera.Handle,"ExposureValue",&nTemp);
285
        return (double)nTemp;
286
    case CV_CAP_PROP_FPS:
287
        tPvFloat32 nfTemp;
288
        PvAttrFloat32Get(Camera.Handle, "StatFrameRate", &nfTemp);
289
        return (double)nfTemp;
290
    case CV_CAP_PROP_PVAPI_MULTICASTIP:
291
        char mEnable[2];
292
        char mIp[11];
293
        PvAttrEnumGet(Camera.Handle,"MulticastEnable",mEnable,sizeof(mEnable),NULL);
294
        if (strcmp(mEnable, "Off") == 0) {
295
            return -1;
296
        }
297
        else {
298
            long int ip;
299
            int a,b,c,d;
300
            PvAttrStringGet(Camera.Handle, "MulticastIPAddress",mIp,sizeof(mIp),NULL);
301
            sscanf(mIp, "%d.%d.%d.%d", &a, &b, &c, &d); ip = ((a*256 + b)*256 + c)*256 + d;
302
            return (double)ip;
303
        }
304
    }
305
    return -1.0;
306
}
307
308
bool CvCaptureCAM_PvAPI::setProperty( int property_id, double value )
309
{
310
    switch ( property_id )
311
    {
312
    /*  TODO: Camera works, but IplImage must be modified for the new size
313
    case CV_CAP_PROP_FRAME_WIDTH:
314
        PvAttrUint32Set(Camera.Handle, "Width", (tPvUint32)value);
315
        break;
316
    case CV_CAP_PROP_FRAME_HEIGHT:
317
        PvAttrUint32Set(Camera.Handle, "Heigth", (tPvUint32)value);
318
        break;
319
    */
320
    case CV_CAP_PROP_MONOCROME:
321
        if (value==1) {
322
                        char pixelFormat[256];
323
                        PvAttrEnumGet(Camera.Handle, "PixelFormat", pixelFormat,256,NULL);
324
                        if ((strcmp(pixelFormat, "Mono8")==0) || strcmp(pixelFormat, "Mono16")==0) {
325
                                monocrome=true;
326
                        }        
327
                        else
328
                                return false;
329
                }
330
                else
331
                        monocrome=false;
332
                break;        
333
    case CV_CAP_PROP_EXPOSURE:
334
            if ((PvAttrUint32Set(Camera.Handle,"ExposureValue",(tPvUint32)value)==ePvErrSuccess)) 
335
                break;
336
            else
337
                return false;
338
    case CV_CAP_PROP_PVAPI_MULTICASTIP:
339
            
340
                if (value==-1) {
341
                if ((PvAttrEnumSet(Camera.Handle,"MulticastEnable", "Off")==ePvErrSuccess)) 
342
                    break;
343
                else 
344
                    return false;
345
            }
346
            else {
347
                std::string ip=cv::format("%d.%d.%d.%d", ((int)value>>24)&255, ((int)value>>16)&255, ((int)value>>8)&255, (int)value&255);
348
                if ((PvAttrEnumSet(Camera.Handle,"MulticastEnable", "On")==ePvErrSuccess) &&
349
                (PvAttrStringSet(Camera.Handle, "MulticastIPAddress", ip.c_str())==ePvErrSuccess)) 
350
                    break;
351
                else
352
                    return false;
353
            }
354
    default:
355
        return false;
356
    }
357
    return true;
358
}
359
360
361
CvCapture* cvCreateCameraCapture_PvAPI( int index )
362
{
363
    CvCaptureCAM_PvAPI* capture = new CvCaptureCAM_PvAPI;
364
365
    memset(&capture->Camera, 0, sizeof(tCamera));
366
    
367
    if ( capture->open( index ))
368
        return capture;
369
370
    delete capture;
371
    return NULL;
372
}
373
374
#ifdef _MSC_VER
375
#pragma comment(lib, "PvAPI.lib")
376
#endif
377
378
#endif