Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleCMU1394.cpp

Example for capturing images using CMU1394 library directly without an instance of BIAS::VideoSource_DCAM.

Author
esquivel
Date
11/2013
/*
This file is part of the BIAS library (Basic ImageAlgorithmS).
Copyright (C) 2003-2009 (see file CONTACT for details)
Multimediale Systeme der Informationsverarbeitung
Institut fuer Informatik
Christian-Albrechts-Universitaet Kiel
BIAS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
BIAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with BIAS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/** @example ExampleCMU1394.cpp
@ingroup g_examples
@ingroup g_videosource
@brief Example for capturing images using CMU1394 library directly
without an instance of BIAS::VideoSource_DCAM.
@author esquivel
@date 11/2013
*/
#include <Base/Image/Image.hh>
#include <Base/Image/ImageIO.hh>
#include <1394Camera.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
using namespace BIAS;
using namespace std;
enum ColorFormat { RGB8, RGB16, MONO8, MONO16, YUV411, YUV422, YUV444 };
std::ostream& operator<< (std::ostream &out, const ColorFormat &color)
{
switch (color)
{
case RGB8: return out << "RGB 8";
case RGB16: return out << "RGB 16";
case MONO8: return out << "MONO 8";
case MONO16: return out << "MONO 16";
case YUV411: return out << "YUV 4:1:1";
case YUV422: return out << "YUV 4:2:2";
case YUV444: return out << "YUV 4:4:4";
}
return out;
}
struct VideoSetting
{
int vformat, vmode;
int width, height;
ColorFormat color;
VideoSetting(int f, int m, int w, int h, ColorFormat c)
{
vformat = f;
vmode = m;
width = w;
height = h;
color = c;
}
};
class VideoSettings
{
public:
VideoSettings() : INVALID_SETTING(-1, -1, 0, 0, RGB8)
{
// Create video modes, video formats and color formats
// @todo Verify that these values are general and do not apply to PointGrey Grasshopper only!
settings.reserve(24);
settings.push_back(VideoSetting(0, 0, 160, 120, YUV444));
settings.push_back(VideoSetting(0, 1, 320, 240, YUV422));
settings.push_back(VideoSetting(0, 2, 640, 480, YUV411));
settings.push_back(VideoSetting(0, 3, 640, 480, YUV422));
settings.push_back(VideoSetting(0, 4, 640, 480, RGB8));
settings.push_back(VideoSetting(0, 5, 640, 480, MONO8));
settings.push_back(VideoSetting(0, 6, 640, 480, MONO16));
settings.push_back(VideoSetting(1, 0, 800, 600, YUV422));
settings.push_back(VideoSetting(1, 1, 800, 600, RGB8));
settings.push_back(VideoSetting(1, 2, 800, 600, MONO8));
settings.push_back(VideoSetting(1, 6, 800, 600, MONO16));
settings.push_back(VideoSetting(1, 3, 1024, 768, YUV422));
settings.push_back(VideoSetting(1, 4, 1024, 768, RGB8));
settings.push_back(VideoSetting(1, 5, 1024, 768, MONO8));
settings.push_back(VideoSetting(1, 7, 1024, 768, MONO16));
settings.push_back(VideoSetting(2, 0, 1280, 960, YUV422));
settings.push_back(VideoSetting(2, 1, 1280, 960, RGB8));
settings.push_back(VideoSetting(2, 2, 1280, 960, MONO8));
settings.push_back(VideoSetting(2, 6, 1280, 960, MONO16));
settings.push_back(VideoSetting(2, 3, 1600, 1200, YUV422));
settings.push_back(VideoSetting(2, 4, 1600, 1200, RGB8));
settings.push_back(VideoSetting(2, 5, 1600, 1200, MONO8));
settings.push_back(VideoSetting(2, 7, 1600, 1200, MONO16));
}
VideoSetting findFormatAndMode(int vformat, int vmode) const
{
std::vector<VideoSetting>::const_iterator it;
for (it = settings.begin(); it != settings.end(); it++)
if (it->vformat == vformat && it->vmode == vmode)
return *it;
return INVALID_SETTING;
}
VideoSetting findSizeAndColor(int width, int height, ColorFormat color) const
{
std::vector<VideoSetting>::const_iterator it;
for (it = settings.begin(); it != settings.end(); it++)
if (it->width == width && it->height == height && it->color == color)
return *it;
return INVALID_SETTING;
}
private:
std::vector<VideoSetting> settings;
const VideoSetting INVALID_SETTING;
};
int main(int argc, char *argv[])
{
cout << "Usage : ExampleCMU1394 [<cam id>] [<video format>] [<video mode>] [<fps mode>] [<image num>]"
<< endl << endl;
// Create configurations
float fps[8] = { 1.875f, 3.75f, 7.5f, 15.0f, 30.0f, 60.0f, 120.0f, 240.0f };
VideoSettings vsettings;
// Read parameters
int cam_id = argc > 1 ? atoi(argv[1]) : 0;
int vformat_id = argc > 2 ? atoi(argv[2]) : 2;
int vmode_id = argc > 3 ? atoi(argv[3]) : 3;
int fps_id = argc > 4 ? atoi(argv[4]) : 2;
int image_number = argc > 5 ? atoi(argv[5]) : 1;
// Initialize camera
C1394Camera camera;
if (camera.RefreshCameraList() == 0) {
cout << "Warning : No cameras found!" << endl;
return 0;
}
if (camera.SelectCamera(cam_id) != CAM_SUCCESS) {
cout << "Error : Failed to select camera " << cam_id << "!" << endl;
return -1;
}
if (camera.InitCamera() != CAM_SUCCESS) {
cout << "Error : Failed to initialize camera " << cam_id << "!" << endl;
return -1;
}
// Try to set parameters
camera.SetVideoFormat(vformat_id);
camera.SetVideoMode(vmode_id);
camera.SetVideoFrameRate(fps_id);
// Read actual video setting
unsigned long width = 0, height = 0;
camera.GetVideoFrameDimensions(&width, &height);
vformat_id = camera.GetVideoFormat();
vmode_id = camera.GetVideoMode();
fps_id = camera.GetVideoFrameRate();
VideoSetting vsetting = vsettings.findFormatAndMode(vformat_id, vmode_id);
const bool grey_image = (vsetting.color == MONO8 || vsetting.color == MONO16);
const ColorFormat color_format = vsetting.color;
if (vsetting.width != (int)width || vsetting.height != (int)height) {
cout << "Warning : Video setting is not consistent with specification!" << endl;
}
// Create image for data storage
const unsigned int channels = 3;
unsigned long bytes = width * height * channels;
BIAS::Image<unsigned char> image((int)width, (int)height, channels);
// Start capturing
if (camera.StartImageAcquisition() != CAM_SUCCESS) {
cout << "Error : Failed to start image capture!" << endl;
return -1;
}
cout << "Started capture with image size " << width << " x " << height
<< ", color format " << color_format << " at " << fps[fps_id] << " fps" << endl
<< "(video format " << vformat_id << ", video mode " << vmode_id << ")" << endl
<< "Capturing " << image_number << " image" << (image_number != 1 ? "s" : "") << endl;
for (int image_no = 0; image_no < image_number; image_no++)
{
// Capture one frame
if (camera.AcquireImage() != CAM_SUCCESS) {
cout << "Error : Failed to capture image!" << endl;
continue;
}
// Read image frame
switch (color_format)
{
case RGB8:
camera.getRGB(image.GetImageData(), bytes);
break;
case RGB16:
camera.RGB16toRGB(image.GetImageData(), bytes);
break;
case MONO8:
camera.YtoRGB(image.GetImageData(), bytes);
break;
case MONO16:
camera.Y16toRGB(image.GetImageData(), bytes);
break;
case YUV411:
camera.YUV411toRGB(image.GetImageData(), bytes);
break;
case YUV422:
camera.YUV422toRGB(image.GetImageData(), bytes);
break;
case YUV444:
camera.YUV444toRGB(image.GetImageData(), bytes);
break;
}
// Save image to file
char filename[32];
sprintf(filename, "image-%04d.%s", image_no, grey_image ? "pgm" : "ppm");
//sprintf(filename, "image_%d_%d-%04d.ppm", vformat_id, vmode_id, image_no);
BIAS::ImageIO::Save(filename, image);
cout << "Wrote " << filename << endl << flush;
}
// Close camera
if (camera.IsAcquiring())
camera.StopImageAcquisition();
cout << "Closed camera connection" << endl;
return 0;
}