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

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

Author
esquivel (adapted from libDC1394 examples)
Date
09/2012
/*
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 ExampleDC1394.cpp
@ingroup g_examples
@ingroup g_videosource
@brief Example for capturing images using DC1394 library directly
without an instance of BIAS::VideoSource_DCAM.
@author esquivel (adapted from libDC1394 examples)
@date 09/2012
*/
#include <Base/Image/Image.hh>
#include <Base/Image/ImageIO.hh>
#include <dc1394/dc1394.h>
#include <inttypes.h>
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#define NUM_DMA_BUFFERS 4
using namespace BIAS;
using namespace std;
/*-----------------------------------------------------------------------
* Releases the cameras and exits
*-----------------------------------------------------------------------*/
void cleanup_and_exit(dc1394camera_t *camera)
{
dc1394_video_set_transmission(camera, DC1394_OFF);
dc1394_capture_stop(camera);
dc1394_camera_free(camera);
exit(1);
}
int main(int argc, char *argv[])
{
dc1394camera_t *camera;
dc1394framerate_t framerate = DC1394_FRAMERATE_15;
dc1394video_mode_t video_mode = DC1394_VIDEO_MODE_800x600_MONO8;
dc1394color_coding_t color_coding = DC1394_COLOR_CODING_MONO8;
dc1394video_frame_t *frame = NULL;
dc1394_t *dc = NULL;
dc1394error_t err;
int image_number = 1;
bool show_features = false;
bool detect_video_mode = true;
if (argc > 1) {
image_number = atoi(argv[1]);
if (image_number < 1) image_number = 1;
}
dc = dc1394_new();
if (!dc) return 1;
dc1394camera_list_t *list;
err = dc1394_camera_enumerate (dc, &list);
DC1394_ERR_RTN(err, "Failed to enumerate cameras");
if (list->num == 0) {
dc1394_log_error("No cameras found");
return 1;
}
camera = dc1394_camera_new(dc, list->ids[0].guid);
if (!camera) {
dc1394_log_error("Failed to initialize camera with guid %d", (int)list->ids[0].guid);
return 1;
}
dc1394_camera_free_list(list);
cout << "Using camera with GUID " << camera->guid << endl;
/*-----------------------------------------------------------------------
* detect the best video mode and highest framerate automatically
*-----------------------------------------------------------------------*/
if (detect_video_mode)
{
// get video modes
dc1394video_modes_t video_modes;
err = dc1394_video_get_supported_modes(camera, &video_modes);
DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera), "Can't get video modes");
// select highest resolution mode for grey images
bool found_mode = false;
for (int i = video_modes.num-1; i>= 0; i--) {
if (!dc1394_is_video_mode_scalable(video_modes.modes[i])) {
dc1394_get_color_coding_from_video_mode(camera, video_modes.modes[i], &color_coding);
if (color_coding == DC1394_COLOR_CODING_MONO8) {
video_mode = video_modes.modes[i];
found_mode = true;
break;
}
}
}
if (!found_mode) {
dc1394_log_error("Could not get a valid MONO8 mode");
cleanup_and_exit(camera);
}
err = dc1394_get_color_coding_from_video_mode(camera, video_mode, &color_coding);
DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera), "Could not get color coding");
// get highest framerate
dc1394framerates_t framerates;
err = dc1394_video_get_supported_framerates(camera, video_mode, &framerates);
DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera), "Could not get framerates");
framerate = framerates.framerates[framerates.num-1];
}
/*-----------------------------------------------------------------------
* setup capture
*-----------------------------------------------------------------------*/
if (color_coding != DC1394_COLOR_CODING_MONO8 &&
color_coding != DC1394_COLOR_CODING_RGB8) {
dc1394_log_error("Color coding has to be MONO8 or RGB8");
cleanup_and_exit(camera);
}
err = dc1394_video_set_iso_speed(camera, DC1394_ISO_SPEED_400);
DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera), "Could not set iso speed");
err = dc1394_video_set_mode(camera, video_mode);
DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera), "Could not set video mode");
err = dc1394_video_set_framerate(camera, framerate);
DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera), "Could not set framerate");
err = dc1394_capture_setup(camera, NUM_DMA_BUFFERS, DC1394_CAPTURE_FLAGS_DEFAULT);
DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera), "Could not setup camera\n"
"Make sure that the video mode and framerate are\n"
"supported by your camera");
/*-----------------------------------------------------------------------
* report camera's features
*-----------------------------------------------------------------------*/
if (show_features) {
dc1394featureset_t features;
err = dc1394_feature_get_all(camera, &features);
if (err != DC1394_SUCCESS) {
dc1394_log_warning("Could not get feature set");
}
else {
dc1394_feature_print_all(&features, stdout);
}
}
/*-----------------------------------------------------------------------
* create image for data storage
*-----------------------------------------------------------------------*/
unsigned int width, height;
float fps;
dc1394_get_image_size_from_video_mode(camera, video_mode, &width, &height);
dc1394_framerate_as_float(framerate, &fps);
const bool grey_image = (color_coding == DC1394_COLOR_CODING_MONO8);
const unsigned int channels = (grey_image ? 1 : 3);
const unsigned int bytes = channels * width * height;
//unsigned char *buffer = new unsigned char[bytes];
BIAS::Image<unsigned char> image(width, height, channels);
cout << "Starting capture with image size " << width << " x "
<< height << " (" << (grey_image ? "MONO8" : "RGB8") << ") with "
<< fps << " frames/sec" << endl;
/*-----------------------------------------------------------------------
* have the camera start sending us data
*-----------------------------------------------------------------------*/
err = dc1394_video_set_transmission(camera, DC1394_ON);
DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera),
"Could not start camera iso transmission");
for (int image_no = 0; image_no < image_number; image_no++)
{
/*-----------------------------------------------------------------------
* capture one frame
*-----------------------------------------------------------------------*/
err = dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_WAIT, &frame);
DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera), "Could not capture a frame");
// memcpy(buffer, frame->image, bytes);
memcpy(image.GetImageData(), frame->image, bytes);
/*-----------------------------------------------------------------------
* save image to file
*-----------------------------------------------------------------------*/
char filename[32];
sprintf(filename, "image-%04d.%s", image_no, grey_image ? "pgm" : "ppm");
/*
FILE* imagefile;
imagefile = fopen(filename, "wb");
if (imagefile == NULL) {
char errormessage[128];
sprintf(errormessage, "Can't create '%s')", filename);
perror(errormessage);
cleanup_and_exit(camera);
}
if (grey_image)
fprintf(imagefile, "P5\n%u %u 255\n", width, height);
else
fprintf(imagefile, "P6\n%u %u 255\n", width, height);
fwrite(buffer, 1, bytes, imagefile);
fclose(imagefile);
*/
BIAS::ImageIO::Save(filename, image);
cout << "Wrote " << filename << endl << flush;
/*-----------------------------------------------------------------------
* enqueue frame in DMA buffer
*-----------------------------------------------------------------------*/
err = dc1394_capture_enqueue(camera, frame);
}
/*-----------------------------------------------------------------------
* close camera
*-----------------------------------------------------------------------*/
dc1394_video_set_transmission(camera, DC1394_OFF);
DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera), "Could not stop the camera");
dc1394_capture_stop(camera);
dc1394_camera_free(camera);
dc1394_free(dc);
cout << "Closed camera connection" << endl;
// release memory
//delete[] buffer;
return 0;
}