cap_pvapi.cpp.patch

Erich Michler, 2012-05-11 04:31 pm

Download (14.2 kB)

 
/modules/highgui/src/cap_pvapi.cpp (original) Mon Apr 23 11:17:25 2012
57 57
#endif
58 58

  
59 59
#include <PvApi.h>
60
#ifdef WIN32
61
#include <io.h>
62
#elif
63 60
#include <unistd.h>
64
#endif
65 61
#include <string>
66 62
//#include <arpa/inet.h>
67 63

  
......
73 69
{
74 70
public:
75 71
    CvCaptureCAM_PvAPI();
76
    virtual ~CvCaptureCAM_PvAPI()
72
    virtual ~CvCaptureCAM_PvAPI() 
77 73
    {
78 74
        close();
79 75
    }
......
90 86
    }
91 87

  
92 88
protected:
93
    #ifndef WIN32
94
    virtual void Sleep(unsigned int time);
95
    #endif
89
	virtual void Sleep(unsigned int time);
96 90

  
97 91
    typedef struct
98 92
    {
99
        unsigned long UID;
100
        tPvHandle     Handle;
101
        tPvFrame      Frame;
102
    } tCamera;
93
        unsigned long   UID;
94
        tPvHandle       Handle;
95
        tPvFrame        Frame;
96

  
103 97

  
98
    } tCamera;
104 99
    IplImage *frame;
105 100
    IplImage *grayframe;
106
    tCamera  Camera;
107
    tPvErr   Errcode;
108
    bool     monocrome;
101
    tCamera Camera;
102
    tPvErr          Errcode;
103
    bool monocrome;
104

  
105

  
109 106
};
110 107

  
111 108

  
112 109
CvCaptureCAM_PvAPI::CvCaptureCAM_PvAPI()
113 110
{
114
    monocrome = false;
111
	monocrome=false;
115 112
    memset(&this->Camera, 0, sizeof(this->Camera));
116 113
}
117
#ifndef WIN32
118 114
void CvCaptureCAM_PvAPI::Sleep(unsigned int time)
119 115
{
120 116
    struct timespec t,r;
121 117
    
122 118
    t.tv_sec    = time / 1000;
123
    t.tv_nsec   = (time % 1000) * 1000000;
119
    t.tv_nsec   = (time % 1000) * 1000000;    
124 120
    
125 121
    while(nanosleep(&t,&r)==-1)
126 122
        t = r;
127 123
}
128
#endif
129 124
void CvCaptureCAM_PvAPI::close()
130 125
{
131
    // Stop the acquisition & free the camera
132
    PvCommandRun(Camera.Handle, "AcquisitionStop");
133
    PvCaptureEnd(Camera.Handle);
126
	// Stop the acquisition & free the camera
127
	PvCommandRun(Camera.Handle, "AcquisitionStop");
128
	PvCaptureEnd(Camera.Handle);
134 129
    PvCameraClose(Camera.Handle);
135 130
    PvUnInitialize();
136 131
}
......
139 134
bool CvCaptureCAM_PvAPI::open( int index )
140 135
{
141 136
    tPvCameraInfo cameraList[MAX_CAMERAS];
142

  
143
    tPvCameraInfo camInfo;
137
    
138
    tPvCameraInfo  camInfo;
144 139
    tPvIpSettings ipSettings;
145 140

  
146

  
147
    if (PvInitialize())
148
    {
149
    }
150

  
151
    // One second sleep is too long and not flexible enought
152
    int timercounter=0;
153

  
154
    while(!(PvCameraCount() > 1||timercounter > 25))
155
    {
156
       Sleep(25);
157
       timercounter = timercounter + 1;
141
 
142
    if (PvInitialize()) {
158 143
    }
144
        //return false;
145
        
146
    Sleep(1000);
159 147

  
148
    //close();
149
    
160 150
    int numCameras=PvCameraList(cameraList, MAX_CAMERAS, NULL);
161 151

  
162 152
    if (numCameras <= 0 || index >= numCameras)
163 153
        return false;
164 154

  
165
    Camera.UID = cameraList[index].UniqueId;
155
    Camera.UID = cameraList[index].UniqueId;    
156

  
157
    if (!PvCameraInfo(Camera.UID,&camInfo) && !PvCameraIpSettingsGet(Camera.UID,&ipSettings)) {
158
		/*
159
		struct in_addr addr;
160
		addr.s_addr = ipSettings.CurrentIpAddress;
161
		printf("Current address:\t%s\n",inet_ntoa(addr));
162
		addr.s_addr = ipSettings.CurrentIpSubnet;
163
		printf("Current subnet:\t\t%s\n",inet_ntoa(addr));
164
		addr.s_addr = ipSettings.CurrentIpGateway;
165
		printf("Current gateway:\t%s\n",inet_ntoa(addr));
166
		*/
167
	}	
168
	else {
169
		fprintf(stderr,"ERROR: could not retrieve camera IP settings.\n");
170
		return false;
171
	}	
166 172

  
167
    if (!PvCameraInfo(Camera.UID,&camInfo) && !PvCameraIpSettingsGet(Camera.UID,&ipSettings))
168
    {
169
        /*
170
        struct in_addr addr;
171
        addr.s_addr = ipSettings.CurrentIpAddress;
172
        printf("Current address:\t%s\n",inet_ntoa(addr));
173
        addr.s_addr = ipSettings.CurrentIpSubnet;
174
        printf("Current subnet:\t\t%s\n",inet_ntoa(addr));
175
        addr.s_addr = ipSettings.CurrentIpGateway;
176
        printf("Current gateway:\t%s\n",inet_ntoa(addr));
177
        */
178
    }
179
    else
180
    {
181
        fprintf(stderr,"ERROR: could not retrieve camera IP settings.\n");
182
        return false;
183
    }
184 173

  
185 174
    if (PvCameraOpen(Camera.UID, ePvAccessMaster, &(Camera.Handle))==ePvErrSuccess)
186 175
    {
176
    
187 177
        //Set Pixel Format to BRG24 to follow conventions 
188 178
        /*Errcode = PvAttrEnumSet(Camera.Handle, "PixelFormat", "Bgr24");
189 179
        if (Errcode != ePvErrSuccess)
......
194 184
        */
195 185
        tPvUint32 frameWidth, frameHeight, frameSize;
196 186
        unsigned long maxSize;
197
        char pixelFormat[256];
187
		char pixelFormat[256];
198 188
        PvAttrUint32Get(Camera.Handle, "TotalBytesPerFrame", &frameSize);
199 189
        PvAttrUint32Get(Camera.Handle, "Width", &frameWidth);
200 190
        PvAttrUint32Get(Camera.Handle, "Height", &frameHeight);
......
202 192
        maxSize = 8228;
203 193
        //PvAttrUint32Get(Camera.Handle,"PacketSize",&maxSize);
204 194
        if (PvCaptureAdjustPacketSize(Camera.Handle,maxSize)!=ePvErrSuccess)
205
            return false;
206
        if (strcmp(pixelFormat, "Mono8")==0)
207
        {
208
            grayframe = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_8U, 1);
209
            grayframe->widthStep = (int)frameWidth;
210
            frame = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_8U, 3);
211
            frame->widthStep = (int)frameWidth*3;
212
            Camera.Frame.ImageBufferSize = frameSize;
213
            Camera.Frame.ImageBuffer = grayframe->imageData;
214
        }
215
        else if (strcmp(pixelFormat, "Mono16")==0)
216
        {
217
            grayframe = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_16U, 1);
218
            grayframe->widthStep = (int)frameWidth;
219
            frame = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_16U, 3);
220
            frame->widthStep = (int)frameWidth*3;
221
            Camera.Frame.ImageBufferSize = frameSize;
222
            Camera.Frame.ImageBuffer = grayframe->imageData;
223
        }
224
        else if (strcmp(pixelFormat, "Bgr24")==0)
225
        {
226
            frame = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_8U, 3);
227
            frame->widthStep = (int)frameWidth*3;
228
            Camera.Frame.ImageBufferSize = frameSize;
229
            Camera.Frame.ImageBuffer = frame->imageData;
230
        }
231
        else
232
           return false;
195
			return false;
196
        if (strcmp(pixelFormat, "Mono8")==0) {
197
				grayframe = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_8U, 1);
198
				grayframe->widthStep = (int)frameWidth;
199
				frame = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_8U, 3);
200
				frame->widthStep = (int)frameWidth*3;		 
201
				Camera.Frame.ImageBufferSize = frameSize;
202
				Camera.Frame.ImageBuffer = grayframe->imageData;   
203
		}	    
204
		else if (strcmp(pixelFormat, "Mono16")==0) {
205
				grayframe = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_16U, 1);
206
				grayframe->widthStep = (int)frameWidth;	
207
				frame = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_16U, 3);
208
				frame->widthStep = (int)frameWidth*3;
209
				Camera.Frame.ImageBufferSize = frameSize;
210
				Camera.Frame.ImageBuffer = grayframe->imageData;
211
		}	  
212
		else if	(strcmp(pixelFormat, "Bgr24")==0) {
213
				frame = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_8U, 3);
214
				frame->widthStep = (int)frameWidth*3;
215
				Camera.Frame.ImageBufferSize = frameSize;
216
				Camera.Frame.ImageBuffer = frame->imageData;
217
		}		
218
		else
219
				return false;
233 220
        // Start the camera
234 221
        PvCaptureStart(Camera.Handle);
235 222

  
......
239 226
            fprintf(stderr,"Could not set Prosilica Acquisition Mode\n");
240 227
            return false;
241 228
        }
229
        
242 230
        if(PvCommandRun(Camera.Handle, "AcquisitionStart")!= ePvErrSuccess)
243 231
        {
244 232
            fprintf(stderr,"Could not start Prosilica acquisition\n");
245 233
            return false;
246 234
        }
235
        
247 236
        if(PvAttrEnumSet(Camera.Handle, "FrameStartTriggerMode", "Freerun")!= ePvErrSuccess)
248 237
        {
249 238
            fprintf(stderr,"Error setting Prosilica trigger to \"Freerun\"");
250 239
            return false;
251 240
        }
241
        
252 242
        return true;
253 243
    }
254 244
    fprintf(stderr,"Error cannot open camera\n");
255 245
    return false;
246

  
256 247
}
257 248

  
258 249
bool CvCaptureCAM_PvAPI::grabFrame()
259 250
{
260
    //if(Camera.Frame.Status != ePvErrUnplugged && Camera.Frame.Status != ePvErrCancelled)
261
    return PvCaptureQueueFrame(Camera.Handle, &(Camera.Frame), NULL) == ePvErrSuccess;
251
	//if(Camera.Frame.Status != ePvErrUnplugged && Camera.Frame.Status != ePvErrCancelled)
252
	return PvCaptureQueueFrame(Camera.Handle, &(Camera.Frame), NULL) == ePvErrSuccess;
262 253
}
263 254

  
255

  
264 256
IplImage* CvCaptureCAM_PvAPI::retrieveFrame(int)
265 257
{
266
    if (PvCaptureWaitForFrameDone(Camera.Handle, &(Camera.Frame), 1000) == ePvErrSuccess)
267
    {
268
        if (!monocrome)
269
        {
270
            cvMerge(grayframe,grayframe,grayframe,NULL,frame);
271
            return frame;
272
        }
273
        return grayframe;
274
    }
275
    else return NULL;
258

  
259
    if (PvCaptureWaitForFrameDone(Camera.Handle, &(Camera.Frame), 1000) == ePvErrSuccess) {
260
		if (!monocrome) {
261
			cvMerge(grayframe,grayframe,grayframe,NULL,frame); 
262
			return frame;	
263
		}	
264
		return grayframe;
265
    }		
266
    else return NULL;		
276 267
}
277 268

  
278 269
double CvCaptureCAM_PvAPI::getProperty( int property_id )
......
288 279
        PvAttrUint32Get(Camera.Handle, "Height", &nTemp);
289 280
        return (double)nTemp;
290 281
    case CV_CAP_PROP_EXPOSURE:
291
        PvAttrUint32Get(Camera.Handle,"ExposureValue",&nTemp);
292
        return (double)nTemp;
293
    case CV_CAP_PROP_GAIN:
294
        PvAttrUint32Get(Camera.Handle, "GainValue", &nTemp);
295
        return (double)nTemp;
296
    case CV_CAP_PROP_GAINMODE:
297
        char gainMode[256];
298
        PvAttrEnumGet(Camera.Handle, "GainMode", gainMode, sizeof(gainMode), NULL);
299
        if (strcmp(gainMode, "Manual") == 0)
300
            return 1;
301
        if (strcmp(gainMode, "Auto") == 0)
302
            return 2;
303
        if (strcmp(gainMode, "AutoOnce") == 0)
304
            return 3;
305
        else
306
            return -1;
282
	PvAttrUint32Get(Camera.Handle,"ExposureValue",&nTemp);
283
	return (double)nTemp;
307 284
    case CV_CAP_PROP_FPS:
308
        tPvFloat32 nfTemp;
285
	tPvFloat32 nfTemp;
309 286
        PvAttrFloat32Get(Camera.Handle, "StatFrameRate", &nfTemp);
310 287
        return (double)nfTemp;
311 288
    case CV_CAP_PROP_PVAPI_MULTICASTIP:
312
        char mEnable[2];
313
        char mIp[11];
314
        PvAttrEnumGet(Camera.Handle,"MulticastEnable",mEnable,sizeof(mEnable),NULL);
315
        if (strcmp(mEnable, "Off") == 0)
316
        {
317
            return -1;
318
        }
319
        else
320
        {
321
            long int ip;
322
            int a,b,c,d;
323
            PvAttrStringGet(Camera.Handle, "MulticastIPAddress",mIp,sizeof(mIp),NULL);
324
            sscanf(mIp, "%d.%d.%d.%d", &a, &b, &c, &d); ip = ((a*256 + b)*256 + c)*256 + d;
325
            return (double)ip;
326
        }
289
	char mEnable[2];
290
	char mIp[11];
291
	PvAttrEnumGet(Camera.Handle,"MulticastEnable",mEnable,sizeof(mEnable),NULL);
292
	if (strcmp(mEnable, "Off") == 0) {
293
	    return -1;
294
	}
295
	else {
296
	    long int ip;
297
	    int a,b,c,d;
298
	    PvAttrStringGet(Camera.Handle, "MulticastIPAddress",mIp,sizeof(mIp),NULL);
299
	    sscanf(mIp, "%d.%d.%d.%d", &a, &b, &c, &d); ip = ((a*256 + b)*256 + c)*256 + d;
300
	    return (double)ip;
301
	}
327 302
    }
328 303
    return -1.0;
329 304
}
......
341 316
        break;
342 317
    */
343 318
    case CV_CAP_PROP_MONOCROME:
344
        if (value==1)
345
        {
346
            char pixelFormat[256];
347
            PvAttrEnumGet(Camera.Handle, "PixelFormat", pixelFormat,256,NULL);
348
            if ((strcmp(pixelFormat, "Mono8")==0) || strcmp(pixelFormat, "Mono16")==0)
349
            {
350
                monocrome=true;
351
            }
352
            else
353
                return false;
354
        }
355
        else
356
            monocrome=false;
357
        break;
319
        if (value==1) {
320
			char pixelFormat[256];
321
			PvAttrEnumGet(Camera.Handle, "PixelFormat", pixelFormat,256,NULL);
322
			if ((strcmp(pixelFormat, "Mono8")==0) || strcmp(pixelFormat, "Mono16")==0) {
323
				monocrome=true;
324
			}	
325
			else
326
				return false;
327
		}
328
		else
329
			monocrome=false;
330
		break;	
358 331
    case CV_CAP_PROP_EXPOSURE:
359
        if ((PvAttrUint32Set(Camera.Handle,"ExposureValue",(tPvUint32)value)==ePvErrSuccess)) 
360
            break;
361
        else
362
            return false;
363
    case CV_CAP_PROP_GAIN:
364
        if (!PvAttrUint32Set(Camera.Handle,"GainValue",(tPvUint32)value))
365
            break;
366
        else
367
            return false;
368
    case CV_CAP_PROP_GAINMODE:
369
        if (value==1)
370
        {
371
            if (!PvAttrEnumSet(Camera.Handle,"GainMode", "Manual"))
372
                break;
373
            else
374
                return false;
375
        }
376
        else
377
        {
378
            if ((PvAttrEnumSet(Camera.Handle,"GainMode", "Auto")==ePvErrSuccess))
379
                break;
380
            else
381
                return false;
382
        }
383
  case CV_CAP_PROP_PVAPI_MULTICASTIP:
384
        if (value==-1)
385
        {
386
            if ((PvAttrEnumSet(Camera.Handle,"MulticastEnable", "Off")==ePvErrSuccess))
387
                break;
388
            else
389
               return false;
390
        }
391
        else
392
        {
393
            std::string ip=cv::format("%d.%d.%d.%d", ((int)value>>24)&255, ((int)value>>16)&255, ((int)value>>8)&255, (int)value&255);
394
            if ((PvAttrEnumSet(Camera.Handle,"MulticastEnable", "On")==ePvErrSuccess) &&
395
               (PvAttrStringSet(Camera.Handle, "MulticastIPAddress", ip.c_str())==ePvErrSuccess))
396
                break;
397
            else
398
                return false;
399
        }
332
	    if ((PvAttrUint32Set(Camera.Handle,"ExposureValue",(tPvUint32)value)==ePvErrSuccess)) 
333
		break;
334
	    else
335
		return false;
336
    case CV_CAP_PROP_PVAPI_MULTICASTIP:
337
	    
338
    	    if (value==-1) {
339
		if ((PvAttrEnumSet(Camera.Handle,"MulticastEnable", "Off")==ePvErrSuccess)) 
340
		    break;
341
		else 
342
		    return false;
343
	    }
344
	    else {
345
		std::string ip=cv::format("%d.%d.%d.%d", ((int)value>>24)&255, ((int)value>>16)&255, ((int)value>>8)&255, (int)value&255);
346
		if ((PvAttrEnumSet(Camera.Handle,"MulticastEnable", "On")==ePvErrSuccess) &&
347
		(PvAttrStringSet(Camera.Handle, "MulticastIPAddress", ip.c_str())==ePvErrSuccess)) 
348
		    break;
349
		else
350
		    return false;
351
	    }
400 352
    default:
401 353
        return false;
402 354
    }