Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleCMU1394.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  BIAS is free software; you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation; either version 2.1 of the License, or
12  (at your option) any later version.
13 
14  BIAS is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with BIAS; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23 
24 /** @example ExampleCMU1394.cpp
25  @ingroup g_examples
26  @ingroup g_videosource
27  @brief Example for capturing images using CMU1394 library directly
28  without an instance of BIAS::VideoSource_DCAM.
29  @author esquivel
30  @date 11/2013
31 */
32 
33 #include <Base/Image/Image.hh>
34 #include <Base/Image/ImageIO.hh>
35 #include <1394Camera.h>
36 #include <stdio.h>
37 #include <stdint.h>
38 #include <stdlib.h>
39 #include <iostream>
40 #include <vector>
41 
42 using namespace BIAS;
43 using namespace std;
44 
45 enum ColorFormat { RGB8, RGB16, MONO8, MONO16, YUV411, YUV422, YUV444 };
46 
47 std::ostream& operator<< (std::ostream &out, const ColorFormat &color)
48 {
49  switch (color)
50  {
51  case RGB8: return out << "RGB 8";
52  case RGB16: return out << "RGB 16";
53  case MONO8: return out << "MONO 8";
54  case MONO16: return out << "MONO 16";
55  case YUV411: return out << "YUV 4:1:1";
56  case YUV422: return out << "YUV 4:2:2";
57  case YUV444: return out << "YUV 4:4:4";
58  }
59  return out;
60 }
61 
62 struct VideoSetting
63 {
64  int vformat, vmode;
65  int width, height;
66  ColorFormat color;
67 
68  VideoSetting(int f, int m, int w, int h, ColorFormat c)
69  {
70  vformat = f;
71  vmode = m;
72  width = w;
73  height = h;
74  color = c;
75  }
76 };
77 
78 class VideoSettings
79 {
80 public:
81  VideoSettings() : INVALID_SETTING(-1, -1, 0, 0, RGB8)
82  {
83  // Create video modes, video formats and color formats
84  // @todo Verify that these values are general and do not apply to PointGrey Grasshopper only!
85  settings.reserve(24);
86  settings.push_back(VideoSetting(0, 0, 160, 120, YUV444));
87  settings.push_back(VideoSetting(0, 1, 320, 240, YUV422));
88  settings.push_back(VideoSetting(0, 2, 640, 480, YUV411));
89  settings.push_back(VideoSetting(0, 3, 640, 480, YUV422));
90  settings.push_back(VideoSetting(0, 4, 640, 480, RGB8));
91  settings.push_back(VideoSetting(0, 5, 640, 480, MONO8));
92  settings.push_back(VideoSetting(0, 6, 640, 480, MONO16));
93 
94  settings.push_back(VideoSetting(1, 0, 800, 600, YUV422));
95  settings.push_back(VideoSetting(1, 1, 800, 600, RGB8));
96  settings.push_back(VideoSetting(1, 2, 800, 600, MONO8));
97  settings.push_back(VideoSetting(1, 6, 800, 600, MONO16));
98  settings.push_back(VideoSetting(1, 3, 1024, 768, YUV422));
99  settings.push_back(VideoSetting(1, 4, 1024, 768, RGB8));
100  settings.push_back(VideoSetting(1, 5, 1024, 768, MONO8));
101  settings.push_back(VideoSetting(1, 7, 1024, 768, MONO16));
102 
103  settings.push_back(VideoSetting(2, 0, 1280, 960, YUV422));
104  settings.push_back(VideoSetting(2, 1, 1280, 960, RGB8));
105  settings.push_back(VideoSetting(2, 2, 1280, 960, MONO8));
106  settings.push_back(VideoSetting(2, 6, 1280, 960, MONO16));
107  settings.push_back(VideoSetting(2, 3, 1600, 1200, YUV422));
108  settings.push_back(VideoSetting(2, 4, 1600, 1200, RGB8));
109  settings.push_back(VideoSetting(2, 5, 1600, 1200, MONO8));
110  settings.push_back(VideoSetting(2, 7, 1600, 1200, MONO16));
111  }
112 
113  VideoSetting findFormatAndMode(int vformat, int vmode) const
114  {
115  std::vector<VideoSetting>::const_iterator it;
116  for (it = settings.begin(); it != settings.end(); it++)
117  if (it->vformat == vformat && it->vmode == vmode)
118  return *it;
119  return INVALID_SETTING;
120  }
121 
122  VideoSetting findSizeAndColor(int width, int height, ColorFormat color) const
123  {
124  std::vector<VideoSetting>::const_iterator it;
125  for (it = settings.begin(); it != settings.end(); it++)
126  if (it->width == width && it->height == height && it->color == color)
127  return *it;
128  return INVALID_SETTING;
129  }
130 
131 private:
132  std::vector<VideoSetting> settings;
133  const VideoSetting INVALID_SETTING;
134 };
135 
136 int main(int argc, char *argv[])
137 {
138  cout << "Usage : ExampleCMU1394 [<cam id>] [<video format>] [<video mode>] [<fps mode>] [<image num>]"
139  << endl << endl;
140 
141  // Create configurations
142  float fps[8] = { 1.875f, 3.75f, 7.5f, 15.0f, 30.0f, 60.0f, 120.0f, 240.0f };
143  VideoSettings vsettings;
144 
145  // Read parameters
146  int cam_id = argc > 1 ? atoi(argv[1]) : 0;
147  int vformat_id = argc > 2 ? atoi(argv[2]) : 2;
148  int vmode_id = argc > 3 ? atoi(argv[3]) : 3;
149  int fps_id = argc > 4 ? atoi(argv[4]) : 2;
150  int image_number = argc > 5 ? atoi(argv[5]) : 1;
151 
152  // Initialize camera
153  C1394Camera camera;
154  if (camera.RefreshCameraList() == 0) {
155  cout << "Warning : No cameras found!" << endl;
156  return 0;
157  }
158  if (camera.SelectCamera(cam_id) != CAM_SUCCESS) {
159  cout << "Error : Failed to select camera " << cam_id << "!" << endl;
160  return -1;
161  }
162  if (camera.InitCamera() != CAM_SUCCESS) {
163  cout << "Error : Failed to initialize camera " << cam_id << "!" << endl;
164  return -1;
165  }
166 
167  // Try to set parameters
168  camera.SetVideoFormat(vformat_id);
169  camera.SetVideoMode(vmode_id);
170  camera.SetVideoFrameRate(fps_id);
171 
172  // Read actual video setting
173  unsigned long width = 0, height = 0;
174  camera.GetVideoFrameDimensions(&width, &height);
175  vformat_id = camera.GetVideoFormat();
176  vmode_id = camera.GetVideoMode();
177  fps_id = camera.GetVideoFrameRate();
178  VideoSetting vsetting = vsettings.findFormatAndMode(vformat_id, vmode_id);
179  const bool grey_image = (vsetting.color == MONO8 || vsetting.color == MONO16);
180  const ColorFormat color_format = vsetting.color;
181  if (vsetting.width != (int)width || vsetting.height != (int)height) {
182  cout << "Warning : Video setting is not consistent with specification!" << endl;
183  }
184 
185  // Create image for data storage
186  const unsigned int channels = 3;
187  unsigned long bytes = width * height * channels;
188  BIAS::Image<unsigned char> image((int)width, (int)height, channels);
189 
190  // Start capturing
191  if (camera.StartImageAcquisition() != CAM_SUCCESS) {
192  cout << "Error : Failed to start image capture!" << endl;
193  return -1;
194  }
195  cout << "Started capture with image size " << width << " x " << height
196  << ", color format " << color_format << " at " << fps[fps_id] << " fps" << endl
197  << "(video format " << vformat_id << ", video mode " << vmode_id << ")" << endl
198  << "Capturing " << image_number << " image" << (image_number != 1 ? "s" : "") << endl;
199 
200  for (int image_no = 0; image_no < image_number; image_no++)
201  {
202  // Capture one frame
203  if (camera.AcquireImage() != CAM_SUCCESS) {
204  cout << "Error : Failed to capture image!" << endl;
205  continue;
206  }
207 
208  // Read image frame
209  switch (color_format)
210  {
211  case RGB8:
212  camera.getRGB(image.GetImageData(), bytes);
213  break;
214  case RGB16:
215  camera.RGB16toRGB(image.GetImageData(), bytes);
216  break;
217  case MONO8:
218  camera.YtoRGB(image.GetImageData(), bytes);
219  break;
220  case MONO16:
221  camera.Y16toRGB(image.GetImageData(), bytes);
222  break;
223  case YUV411:
224  camera.YUV411toRGB(image.GetImageData(), bytes);
225  break;
226  case YUV422:
227  camera.YUV422toRGB(image.GetImageData(), bytes);
228  break;
229  case YUV444:
230  camera.YUV444toRGB(image.GetImageData(), bytes);
231  break;
232  }
233 
234  // Save image to file
235  char filename[32];
236  sprintf(filename, "image-%04d.%s", image_no, grey_image ? "pgm" : "ppm");
237  //sprintf(filename, "image_%d_%d-%04d.ppm", vformat_id, vmode_id, image_no);
238  BIAS::ImageIO::Save(filename, image);
239  cout << "Wrote " << filename << endl << flush;
240  }
241 
242  // Close camera
243  if (camera.IsAcquiring())
244  camera.StopImageAcquisition();
245  cout << "Closed camera connection" << endl;
246 
247  return 0;
248 }
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
std::ostream & operator<<(std::ostream &os, const Array2D< T > &arg)
Definition: Array2D.hh:260