cap_pvapi.cpp.patch

Erich Michler, 2012-05-25 09:56 am

Download (14.2 kB)

 
/modules/highgui/src/cap_pvapi.cpp (modified) Fri May 25 09:18:41 2012
57 57
#endif
58 58

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

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

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

  
91 97
    typedef struct
92 98
    {
93
        unsigned long   UID;
94
        tPvHandle       Handle;
95
        tPvFrame        Frame;
96

  
97

  
99
        unsigned long UID;
100
        tPvHandle     Handle;
101
        tPvFrame      Frame;
98 102
    } tCamera;
103

  
99 104
    IplImage *frame;
100 105
    IplImage *grayframe;
101
    tCamera Camera;
102
    tPvErr          Errcode;
103
    bool monocrome;
104

  
105

  
106
    tCamera  Camera;
107
    tPvErr   Errcode;
108
    bool     monocrome;
106 109
};
107 110

  
108 111

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

  
143
    tPvCameraInfo camInfo;
139 144
    tPvIpSettings ipSettings;
140 145

  
141
 
142
    if (PvInitialize()) {
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;
143 158
    }
144
        //return false;
145
        
146
    Sleep(1000);
147 159

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

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

  
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
	}	
165
    Camera.UID = cameraList[index].UniqueId;
172 166

  
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
    }
173 184

  
174 185
    if (PvCameraOpen(Camera.UID, ePvAccessMaster, &(Camera.Handle))==ePvErrSuccess)
175 186
    {
176
    
177 187
        //Set Pixel Format to BRG24 to follow conventions 
178 188
        /*Errcode = PvAttrEnumSet(Camera.Handle, "PixelFormat", "Bgr24");
179 189
        if (Errcode != ePvErrSuccess)
......
184 194
        */
185 195
        tPvUint32 frameWidth, frameHeight, frameSize;
186 196
        unsigned long maxSize;
187
		char pixelFormat[256];
197
        char pixelFormat[256];
188 198
        PvAttrUint32Get(Camera.Handle, "TotalBytesPerFrame", &frameSize);
189 199
        PvAttrUint32Get(Camera.Handle, "Width", &frameWidth);
190 200
        PvAttrUint32Get(Camera.Handle, "Height", &frameHeight);
......
192 202
        maxSize = 8228;
193 203
        //PvAttrUint32Get(Camera.Handle,"PacketSize",&maxSize);
194 204
        if (PvCaptureAdjustPacketSize(Camera.Handle,maxSize)!=ePvErrSuccess)
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;
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;
220 233
        // Start the camera
221 234
        PvCaptureStart(Camera.Handle);
222 235

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

  
247 256
}
248 257

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

  
255

  
256 264
IplImage* CvCaptureCAM_PvAPI::retrieveFrame(int)
257 265
{
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;		
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;
267 276
}
268 277

  
269 278
double CvCaptureCAM_PvAPI::getProperty( int property_id )
......
279 288
        PvAttrUint32Get(Camera.Handle, "Height", &nTemp);
280 289
        return (double)nTemp;
281 290
    case CV_CAP_PROP_EXPOSURE:
282
	PvAttrUint32Get(Camera.Handle,"ExposureValue",&nTemp);
283
	return (double)nTemp;
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;
284 307
    case CV_CAP_PROP_FPS:
285
	tPvFloat32 nfTemp;
308
        tPvFloat32 nfTemp;
286 309
        PvAttrFloat32Get(Camera.Handle, "StatFrameRate", &nfTemp);
287 310
        return (double)nfTemp;
288 311
    case CV_CAP_PROP_PVAPI_MULTICASTIP:
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
	}
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
        }
302 327
    }
303 328
    return -1.0;
304 329
}
......
316 341
        break;
317 342
    */
318 343
    case CV_CAP_PROP_MONOCROME:
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;	
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;
331 358
    case CV_CAP_PROP_EXPOSURE:
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
	    }
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
        }
352 400
    default:
353 401
        return false;
354 402
    }