1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
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 |
|
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 |
|
126 | PvCommandRun(Camera.Handle, "AcquisitionStop");
|
127 | PvCaptureEnd(Camera.Handle);
|
128 | PvCameraClose(Camera.Handle);
|
129 | }
|
130 |
|
131 |
|
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 |
|
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 |
|
180 | |
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 |
|
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 |
|
223 | PvCaptureStart(Camera.Handle);
|
224 |
|
225 |
|
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 |
|
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 | |
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
|