YaDICs  V04.14.a
Yet another Digital Image Correlation software: platform dedicated to 2/3D Fluid and Solid kinematics field measurements.
 All Classes Files Functions Variables Pages
Cimage_grab.h
Go to the documentation of this file.
1 /**********************************************************************
2  * Copyright (C) 2012, The YaDICs Project Developers.
3  * See the COPYRIGHT file at the top-level directory of this distribution ./COPYRIGHT.
4  * See ./COPYING file for copying and redistribution conditions.
5  *
6  * This file is part of YaDICs.
7  * YaDICs is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * YaDICs is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with YaDICs. If not, see <http://www.gnu.org/licenses/>.
19  *
20  * Information about how to use the software are provided at http://yadic.univ-lille1.fr/
21  **********************************************************************/
22 
23 
24 #ifndef CIMAGE_GRAB
25 #define CIMAGE_GRAB
26 
36 //--------------------------XIMEA---------------------------------
37 #include "../XIMEA/examples/xiSample/stdafx.h"
38 #include "../XIMEA/include/xiApi.h"
39 #include "../XIMEA/include/xiExt.h"
40 #define FORMAT XI_MONO8
41 //--------------------------XIMEA---------------------------------
42 
43 #include "Cimage_file.h"
44 
45 template<typename Timg>
46 class Cimage_grab: public Cimage_file<Timg>
47 {
48 
49  public:
50 
51  CImgList<double> m_time;
52  CImg<unsigned char> m_cam_get;//unsigned char for mono8
53 
54  int m_image_exposure, m_frame;
55  int m_image_width, m_image_height, m_offset_x, m_offset_y;
56  float m_fps;
57 
58  XI_IMG m_image;
59  HANDLE m_xiH;
60 
61 
63  {// constructor
64 
65  this->class_name = "Image : grab";
66 
67  }
68 
69  ~Cimage_grab()
70  {//destructor
71 
72  if (m_xiH)
73  {
74  xiStopAcquisition(m_xiH);
75  xiCloseDevice(m_xiH);
76  }
77 
78  }
79 
80 
81 
82  virtual void load_paths(CParameterNetCDF &fp)
83  {// attributes from cdl
84 
86 
87  int error(1);
88 
89  //-------------------------------------
90  m_frame = 0;
91  std::string att_name = "exposure";
92  error = fp.loadAttribute(att_name,m_image_exposure);
93  att_name = "offset_x";
94  error = fp.loadAttribute(att_name,m_offset_x);//X position of the cropped window left upper corner
95  att_name = "offset_y";
96  error = fp.loadAttribute(att_name,m_offset_y);//Y position of the cropped window left upper corner
97  att_name = "width";
98  error = fp.loadAttribute(att_name,m_image_width);//img width
99  att_name = "height";
100  error = fp.loadAttribute(att_name,m_image_height);//img height
101  att_name = "fps";
102  error = fp.loadAttribute(att_name,m_fps);//frame per second
103  //-------------------------------------
104 
105  if (open_device(m_xiH, m_image))
106  {//if device is openned
107 
108  //set and start acquisition
109  error = set_device(m_xiH, m_image_exposure, m_offset_x, m_offset_y, m_image_width, m_image_height, m_fps);
110 
111  //assign CImg buffer for capture
112  m_cam_get.assign(m_image_width, m_image_height, 1, 1, 0);
113  //assgin Image couple for correlation
114  this->m_img.assign(this->m_dim_image, m_image_width, m_image_height, 1, 1, 0);
115  m_time.assign(3,this->m_dim_sample,1,1,1,0);//frame number, frame time, capture frequency from the latest image
116  //define space dimensions
117  this->m_dim = 2;
118 
119  }
120  else
121  {
122  printf("Warning : device no opened");
123  }
124 
125  }
126 
127  int open_device(HANDLE &xiH, XI_IMG &image)
128  {//open device
129 
130  image.size = sizeof(XI_IMG);
131  image.bp = NULL;
132  image.bp_size = 0;
133 
134  //init
135  xiH = NULL;
136  int error = 1;
137 
138  DWORD dwNumberOfDevices = 0;// Get number of camera devices
139  DWORD m_get_delay = 10000;//timeOut
140 
141  if (!xiGetNumberDevices(&dwNumberOfDevices))
142  {
143  printf("number of devices detected : %i",dwNumberOfDevices);
144  // Retrieving a handle to the camera device
145  xiOpenDevice(0, &xiH);
146  }
147  else
148  {
149  printf("xiGetNumberDevices (no camera found)");
150  error = 0;
151  }
152 
153  return error;
154 
155  }
156 
157  int set_device(HANDLE &xiH, int &exposure, int &offset_x, int &offset_y, int &width, int &height, float &fps)
158  {//set and start
159 
160  int error(0), tmpW(0), tmpH(0);
161  float min_fps(0), max_fps(0);
162 
163  xiSetParamInt(xiH, XI_PRM_EXPOSURE, exposure);//set exposure
164 
165  xiGetParamInt( xiH, XI_PRM_WIDTH, &tmpW);//get full image width
166  xiGetParamInt( xiH, XI_PRM_HEIGHT, &tmpH);//get full image height
167  printf("\nFullFrame resolution -> [%i, %i]\n",tmpW, tmpH);
168 
169 
170  xiSetParamInt( xiH, XI_PRM_OFFSET_X, offset_x);//set offset_y (must be even)
171  xiSetParamInt( xiH, XI_PRM_OFFSET_Y, offset_y);//set offset_y (must be even)
172  xiSetParamInt( xiH, XI_PRM_WIDTH, width);//set image_width (must be divisible by 4)
173  xiSetParamInt( xiH, XI_PRM_HEIGHT, height);//set image_height (must be even)
174 
175  xiGetParamInt( xiH, XI_PRM_WIDTH, &width);//get effective image width
176  xiGetParamInt( xiH, XI_PRM_HEIGHT, &height);//get effective image height
177 
178  printf("\nCaptured image -> offset [%i, %i] - dimensions [%i, %i]\n", offset_x, offset_y, width, height);
179 
180  xiSetParamInt( xiH, XI_PRM_BUFFERS_QUEUE_SIZE, 2);//only one image within camera buffer (Pb with XI_PRM_RECENT_FRAME = 1)
181 
182  xiSetParamInt( xiH, XI_PRM_IMAGE_DATA_FORMAT, FORMAT);//data format
183 
184  if(fps)
185  {//trigger
186 
187  std::cerr<<"Information: use internal trigger.\n";
188 
189  xiGetParamFloat( xiH, XI_PRM_FRAMERATE XI_PRM_INFO_MIN, &min_fps);//detect mini fps for such exposure
190  xiGetParamFloat( xiH, XI_PRM_FRAMERATE XI_PRM_INFO_MAX, &max_fps);//detect maxi fps for such exposure
191 
192  if (fps>max_fps or fps<min_fps)
193  {//over the appropriate range
194  printf("Warning ! : required fps isn't in the appropriate range, its value will be set to (min_fps + max_fps)/2\n");
195  fps = (min_fps + max_fps)/2;
196  }
197 
198  xiSetParamFloat( xiH, XI_PRM_FRAMERATE, fps);//set fps (must be in [min_fps, max_fps]
199  xiGetParamFloat( xiH, XI_PRM_FRAMERATE, &fps);
200 
201  printf("\nFPS settings [min: %f, max: %f, current: %f]\n", min_fps, max_fps, fps);
202 
203  }
204  else
205  {
206 
207  std::cerr<<"Information: use external trigger.\n";
208  xiSetParamInt( xiH, XI_PRM_TRG_SOURCE, XI_TRG_EDGE_RISING);
209  xiSetParamInt( xiH, XI_PRM_GPI_SELECTOR, 1);
210  xiSetParamInt( xiH, XI_PRM_GPI_MODE, XI_GPI_TRIGGER);
211 
212  }
213 
214  xiStartAcquisition(xiH);//start acquisition
215 
216  return error;
217 
218  }
219 
220  void grab(HANDLE &xiH, XI_IMG &image, CImg<unsigned char> &buffer, CImgList<double> &time, int const &bframe, int &frame)
221  {//grab one frame and stores it within buffer[bframe]
222 
223  int lastFrame = -1;
224 
225  do
226  {//while the frame number doesn't change
227 
228  xiGetImage(xiH, 10000, &image);
229 
230  //condition
231  if(frame){lastFrame = time[0][frame-1];}
232 
233  }
234  while ( image.nframe == lastFrame );
235 
236  memcpy( buffer.data(), image.bp, image.width*image.height);
237 
238  //copy data information
239  time[0][frame] = image.nframe;//get frame number
240  time[1][frame] = (double)image.tsSec + (double)image.tsUSec/1e6;//get time
241  if (!frame){time[2][frame] = 1.0/(time[1][frame]-time[1][frame-1]);}else{time[2][frame] = 1.0/time[1][frame];}//get effective freq
242 
243  printf("image : %i, frame %f, at time %f, with a frequency of %f\n",bframe, m_time[0][frame], m_time[1][frame], m_time[2][frame]);
244 
245 
246  }
247 
248  virtual void load_images(CImg<char> &list, std::string &dir, const int &sample)
249  {// load images from paths
250 
251 
252  for (int bframe = 0; bframe<this->m_dim_image; bframe++)
253  {// for the number of frame to correlate
254 
255  grab(m_xiH, m_image, m_cam_get, m_time, bframe, m_frame);
256 
257  //copy data within m_img
259  this->m_img[bframe] = m_cam_get;
260 
261  m_frame++;
262 
263  }
264 
265  }
266 
267 
268 };
269 
270 #endif
Definition: Cimage_file.h:38
load image and mask from cdl and store fields. It also creates a resized version of data in the case ...
virtual void load_images(CImg< char > &list, std::string &dir, const int &sample)
Definition: Cimage_grab.h:248
Definition: Cimage_grab.h:46
virtual void load_paths(CParameterNetCDF &fp)
Definition: Cimage_file.h:59