Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleDC1394.cpp
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 /** @example ExampleDC1394.cpp
26  @ingroup g_examples
27  @ingroup g_videosource
28  @brief Example for capturing images using DC1394 library directly
29  without an instance of BIAS::VideoSource_DCAM.
30  @author esquivel (adapted from libDC1394 examples)
31  @date 09/2012
32 */
33 
34 #include <Base/Image/Image.hh>
35 #include <Base/Image/ImageIO.hh>
36 #include <dc1394/dc1394.h>
37 #include <inttypes.h>
38 #include <unistd.h>
39 #include <stdio.h>
40 #include <stdint.h>
41 #include <stdlib.h>
42 
43 #define NUM_DMA_BUFFERS 4
44 
45 using namespace BIAS;
46 using namespace std;
47 
48 /*-----------------------------------------------------------------------
49  * Releases the cameras and exits
50  *-----------------------------------------------------------------------*/
51 void cleanup_and_exit(dc1394camera_t *camera)
52 {
53  dc1394_video_set_transmission(camera, DC1394_OFF);
54  dc1394_capture_stop(camera);
55  dc1394_camera_free(camera);
56  exit(1);
57 }
58 
59 int main(int argc, char *argv[])
60 {
61  dc1394camera_t *camera;
62  dc1394framerate_t framerate = DC1394_FRAMERATE_15;
63  dc1394video_mode_t video_mode = DC1394_VIDEO_MODE_800x600_MONO8;
64  dc1394color_coding_t color_coding = DC1394_COLOR_CODING_MONO8;
65  dc1394video_frame_t *frame = NULL;
66  dc1394_t *dc = NULL;
67  dc1394error_t err;
68 
69  int image_number = 1;
70  bool show_features = false;
71  bool detect_video_mode = true;
72 
73  if (argc > 1) {
74  image_number = atoi(argv[1]);
75  if (image_number < 1) image_number = 1;
76  }
77 
78  dc = dc1394_new();
79  if (!dc) return 1;
80 
81  dc1394camera_list_t *list;
82  err = dc1394_camera_enumerate (dc, &list);
83  DC1394_ERR_RTN(err, "Failed to enumerate cameras");
84  if (list->num == 0) {
85  dc1394_log_error("No cameras found");
86  return 1;
87  }
88 
89  camera = dc1394_camera_new(dc, list->ids[0].guid);
90  if (!camera) {
91  dc1394_log_error("Failed to initialize camera with guid %d", (int)list->ids[0].guid);
92  return 1;
93  }
94  dc1394_camera_free_list(list);
95 
96  cout << "Using camera with GUID " << camera->guid << endl;
97 
98  /*-----------------------------------------------------------------------
99  * detect the best video mode and highest framerate automatically
100  *-----------------------------------------------------------------------*/
101  if (detect_video_mode)
102  {
103  // get video modes
104  dc1394video_modes_t video_modes;
105  err = dc1394_video_get_supported_modes(camera, &video_modes);
106  DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera), "Can't get video modes");
107 
108  // select highest resolution mode for grey images
109  bool found_mode = false;
110  for (int i = video_modes.num-1; i>= 0; i--) {
111  if (!dc1394_is_video_mode_scalable(video_modes.modes[i])) {
112  dc1394_get_color_coding_from_video_mode(camera, video_modes.modes[i], &color_coding);
113  if (color_coding == DC1394_COLOR_CODING_MONO8) {
114  video_mode = video_modes.modes[i];
115  found_mode = true;
116  break;
117  }
118  }
119  }
120  if (!found_mode) {
121  dc1394_log_error("Could not get a valid MONO8 mode");
122  cleanup_and_exit(camera);
123  }
124 
125  err = dc1394_get_color_coding_from_video_mode(camera, video_mode, &color_coding);
126  DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera), "Could not get color coding");
127 
128  // get highest framerate
129  dc1394framerates_t framerates;
130  err = dc1394_video_get_supported_framerates(camera, video_mode, &framerates);
131  DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera), "Could not get framerates");
132  framerate = framerates.framerates[framerates.num-1];
133  }
134 
135  /*-----------------------------------------------------------------------
136  * setup capture
137  *-----------------------------------------------------------------------*/
138 
139  if (color_coding != DC1394_COLOR_CODING_MONO8 &&
140  color_coding != DC1394_COLOR_CODING_RGB8) {
141  dc1394_log_error("Color coding has to be MONO8 or RGB8");
142  cleanup_and_exit(camera);
143  }
144 
145  err = dc1394_video_set_iso_speed(camera, DC1394_ISO_SPEED_400);
146  DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera), "Could not set iso speed");
147 
148  err = dc1394_video_set_mode(camera, video_mode);
149  DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera), "Could not set video mode");
150 
151  err = dc1394_video_set_framerate(camera, framerate);
152  DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera), "Could not set framerate");
153 
154  err = dc1394_capture_setup(camera, NUM_DMA_BUFFERS, DC1394_CAPTURE_FLAGS_DEFAULT);
155  DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera), "Could not setup camera\n"
156  "Make sure that the video mode and framerate are\n"
157  "supported by your camera");
158 
159  /*-----------------------------------------------------------------------
160  * report camera's features
161  *-----------------------------------------------------------------------*/
162  if (show_features) {
163  dc1394featureset_t features;
164  err = dc1394_feature_get_all(camera, &features);
165  if (err != DC1394_SUCCESS) {
166  dc1394_log_warning("Could not get feature set");
167  }
168  else {
169  dc1394_feature_print_all(&features, stdout);
170  }
171  }
172 
173  /*-----------------------------------------------------------------------
174  * create image for data storage
175  *-----------------------------------------------------------------------*/
176  unsigned int width, height;
177  float fps;
178  dc1394_get_image_size_from_video_mode(camera, video_mode, &width, &height);
179  dc1394_framerate_as_float(framerate, &fps);
180  const bool grey_image = (color_coding == DC1394_COLOR_CODING_MONO8);
181  const unsigned int channels = (grey_image ? 1 : 3);
182  const unsigned int bytes = channels * width * height;
183  //unsigned char *buffer = new unsigned char[bytes];
184  BIAS::Image<unsigned char> image(width, height, channels);
185 
186  cout << "Starting capture with image size " << width << " x "
187  << height << " (" << (grey_image ? "MONO8" : "RGB8") << ") with "
188  << fps << " frames/sec" << endl;
189 
190  /*-----------------------------------------------------------------------
191  * have the camera start sending us data
192  *-----------------------------------------------------------------------*/
193  err = dc1394_video_set_transmission(camera, DC1394_ON);
194  DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera),
195  "Could not start camera iso transmission");
196 
197  for (int image_no = 0; image_no < image_number; image_no++)
198  {
199  /*-----------------------------------------------------------------------
200  * capture one frame
201  *-----------------------------------------------------------------------*/
202  err = dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_WAIT, &frame);
203  DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera), "Could not capture a frame");
204  // memcpy(buffer, frame->image, bytes);
205  memcpy(image.GetImageData(), frame->image, bytes);
206 
207  /*-----------------------------------------------------------------------
208  * save image to file
209  *-----------------------------------------------------------------------*/
210 
211  char filename[32];
212  sprintf(filename, "image-%04d.%s", image_no, grey_image ? "pgm" : "ppm");
213 /*
214  FILE* imagefile;
215  imagefile = fopen(filename, "wb");
216  if (imagefile == NULL) {
217  char errormessage[128];
218  sprintf(errormessage, "Can't create '%s')", filename);
219  perror(errormessage);
220  cleanup_and_exit(camera);
221  }
222  if (grey_image)
223  fprintf(imagefile, "P5\n%u %u 255\n", width, height);
224  else
225  fprintf(imagefile, "P6\n%u %u 255\n", width, height);
226  fwrite(buffer, 1, bytes, imagefile);
227  fclose(imagefile);
228 */
229  BIAS::ImageIO::Save(filename, image);
230  cout << "Wrote " << filename << endl << flush;
231 
232  /*-----------------------------------------------------------------------
233  * enqueue frame in DMA buffer
234  *-----------------------------------------------------------------------*/
235  err = dc1394_capture_enqueue(camera, frame);
236  }
237 
238  /*-----------------------------------------------------------------------
239  * close camera
240  *-----------------------------------------------------------------------*/
241  dc1394_video_set_transmission(camera, DC1394_OFF);
242  DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera), "Could not stop the camera");
243  dc1394_capture_stop(camera);
244  dc1394_camera_free(camera);
245  dc1394_free(dc);
246 
247  cout << "Closed camera connection" << endl;
248 
249  // release memory
250  //delete[] buffer;
251 
252  return 0;
253 }
static int Save(const std::string &filename, const ImageBase &img, const enum TFileFormat FileFormat=FF_auto, const bool sync=BIAS_DEFAULT_SYNC, const int c_jpeg_quality=BIAS_DEFAULT_IMAGE_QUALITY, const bool forceNewID=BIAS_DEFAULT_FORCENEWID, const bool &writeMetaData=true)
Export image as file using extrnal libs.
Definition: ImageIO.cpp:725