FireWire Arayüzü ile Nasıl Görüntü Alınabilir ?

FireWire arayüzünü kullanarak FireWire destekleyen bir kameradan görüntü alınmasına ilişkin detaylı bir anlatıma aşağıdaki linkten ulaşabilirsiniz. Linux işletim sistemi kullanarak ilgili işlemleri kolayca uygulamanız ve sonuç almanız mümkün olmaktadır. Ben Pardus 2007 ile denedim ve sorunsuz bir şekilde FireWire kameradan görüntü alıp kameranın değiştirilebilir parametrelerine müdahale edebildim.

Kod şu şekilde;




#include <stdio.h>
#include <libraw1394/raw1394.h>
#include <libdc1394/dc1394_control.h>
#include <stdlib.h>


#define IMAGE_FILE_NAME "Image.pgm"

int main(int argc, char *argv[])
{
FILE* imagefile;
dc1394_cameracapture camera;
int numNodes;
int numCameras;
int value;
raw1394handle_t handle;
nodeid_t * camera_nodes;
dc1394_feature_set features;
int format=-1, mode=-1;
quadlet_t quadlet;
int exposure;
/*-----------------------------------------------------------------------
* Open ohci and asign handle to it
*-----------------------------------------------------------------------*/
handle = dc1394_create_handle(0);
if (handle==NULL)
{
fprintf( stderr, "Unable to aquire a raw1394 handle\n\n"
"Please check \n"
" - if the kernel modules `ieee1394',`raw1394' and `ohci1394' are loaded \n"
" - if you have read/write access to /dev/raw1394\n\n");
exit(1);
}
/*-----------------------------------------------------------------------
* get the camera nodes and describe them as we find them
*-----------------------------------------------------------------------*/
numNodes = raw1394_get_nodecount(handle);
camera_nodes = dc1394_get_camera_nodes(handle,&numCameras,1);
fflush(stdout);
if (numCameras<1)
{
fprintf( stderr, "no cameras found :(\n");
dc1394_destroy_handle(handle);
exit(1);
}
/*-----------------------------------------------------------------------
* to prevent the iso-transfer bug from raw1394 system, check if
* camera is highest node. For details see
* http://linux1394.sourceforge.net/faq.html#DCbusmgmt
* and
* http://sourceforge.net/tracker/index.php?func=detail&aid=435107&group_id=8157&atid=108157
*-----------------------------------------------------------------------*/
if( camera_nodes[0] == numNodes-1)
{
fprintf( stderr, "\n"
"Sorry, your camera is the highest numbered node\n"
"of the bus, and has therefore become the root node.\n"
"The root node is responsible for maintaining \n"
"the timing of isochronous transactions on the IEEE \n"
"1394 bus. However, if the root node is not cycle master \n"
"capable (it doesn't have to be), then isochronous \n"
"transactions will not work. The host controller card is \n"
"cycle master capable, however, most cameras are not.\n"
"\n"
"The quick solution is to add the parameter \n"
"attempt_root=1 when loading the OHCI driver as a \n"
"module. So please do (as root):\n"
"\n"
" rmmod ohci1394\n"
" insmod ohci1394 attempt_root=1\n"
"\n"
"for more information see the FAQ at \n"
"http://linux1394.sourceforge.net/faq.html#DCbusmgmt\n"
"\n");
dc1394_destroy_handle(handle);
dc1394_free_camera_nodes(camera_nodes);
exit( 1);
}

/*-----------------------------------------------------------------------
* find a supported format/mode
*-----------------------------------------------------------------------*/
dc1394_query_supported_formats(handle,camera_nodes[0],&quadlet);
if (quadlet & (1<<29)) {
// check medium resolution formats
dc1394_query_supported_modes(handle,camera_nodes[0],FORMAT_SVGA_NONCOMPRESSED_2,&quadlet);
if (quadlet & (1<< (31-5))) {
fprintf(stderr,"Using format2/mode5\n");
format=FORMAT_SVGA_NONCOMPRESSED_2;
mode=MODE_1600x1200_MONO;
} else if (quadlet & (1<< (31-2))) {
fprintf(stderr,"Using format2/mode2\n");
format=FORMAT_SVGA_NONCOMPRESSED_2;
mode=MODE_1280x960_MONO;
}
} else if (quadlet & (1<<30)) {
// check medium resolution formats
dc1394_query_supported_modes(handle,camera_nodes[0],FORMAT_SVGA_NONCOMPRESSED_1,&quadlet);
if (quadlet & (1<< (31-5))) {
fprintf(stderr,"Using format1/mode2\n");
format=FORMAT_SVGA_NONCOMPRESSED_1;
mode=MODE_1024x768_MONO;
} else if (quadlet & (1<< (31-2))) {
fprintf(stderr,"Using format1/mode5\n");
format=FORMAT_SVGA_NONCOMPRESSED_1;
mode=MODE_800x600_MONO;
}
} else if (quadlet & (1<<31)) {
// check for VGA format
dc1394_query_supported_modes(handle,camera_nodes[0],FORMAT_VGA_NONCOMPRESSED,&quadlet);
if (quadlet & (1<< (31-5))) {
fprintf(stderr,"Using format0/mode5\n");
format=FORMAT_VGA_NONCOMPRESSED;
mode=MODE_640x480_MONO;
}
} else {
fprintf(stderr,"Your camera does not seem to support an 8-bit grayscale format.\nAborting...\n");
exit(1);
}

format=FORMAT_SVGA_NONCOMPRESSED_1;
mode=MODE_800x600_MONO;
/*dc1394_get_min_value(handle,camera.node,416, &value);
printf("Min Gamma:%d ",value);
dc1394_get_max_value(handle,camera.node,416, &value);
printf("Max Gamma:%d\n",value); */
dc1394_set_brightness(handle, camera.node,90);
dc1394_set_exposure(handle, camera.node,200);
dc1394_set_gain(handle, camera.node,13);
dc1394_set_shutter(handle, camera.node,4095);
dc1394_set_gamma(handle, camera.node,0.9);
//dc1394_get_exposure(handle, camera.node,&exposure);
dc1394_set_capture_size(handle,camera.node,800);
printf("SIZE:%d\n",exposure);

/*-----------------------------------------------------------------------
* setup capture
*-----------------------------------------------------------------------*/
if (dc1394_setup_capture(handle,camera_nodes[0],
0, /* channel */
format, mode,
SPEED_400,
FRAMERATE_7_5,
&camera)!=DC1394_SUCCESS)
{
fprintf( stderr,"unable to setup camera-\n"
"check line %d of %s to make sure\n"
"that the video mode,framerate and format are\n"
"supported by your camera\n",
__LINE__,__FILE__);
dc1394_release_camera(handle,&camera);
dc1394_destroy_handle(handle);
dc1394_free_camera_nodes(camera_nodes);
exit(1);
}
dc1394_free_camera_nodes(camera_nodes);

/* set trigger mode */
if( dc1394_set_trigger_mode(handle, camera.node, TRIGGER_MODE_0)
!= DC1394_SUCCESS)
{
fprintf( stderr, "unable to set camera trigger mode\n");
#if 0
dc1394_release_camera(handle,&camera);
dc1394_destroy_handle(handle);
exit(1);
#endif
}


/*-----------------------------------------------------------------------
* report camera's features
*-----------------------------------------------------------------------*/
if(dc1394_get_camera_feature_set(handle, camera.node,&features)
!=DC1394_SUCCESS)
{
fprintf( stderr, "unable to get feature set\n");
}
else
{
dc1394_print_feature_set(&features);
}

sleep(2);
/*-----------------------------------------------------------------------
* have the camera start sending us data
*-----------------------------------------------------------------------*/
if (dc1394_start_iso_transmission(handle,camera.node)
!=DC1394_SUCCESS)
{
fprintf( stderr, "unable to start camera iso transmission\n");
dc1394_release_camera(handle,&camera);
dc1394_destroy_handle(handle);
exit(1);
}



/*-----------------------------------------------------------------------
* capture one frame
*-----------------------------------------------------------------------*/
if (dc1394_single_capture(handle,&camera)!=DC1394_SUCCESS)
{
fprintf( stderr, "unable to capture a frame\n");
dc1394_release_camera(handle,&camera);
dc1394_destroy_handle(handle);
exit(1);
}

/*-----------------------------------------------------------------------
* Stop data transmission
*-----------------------------------------------------------------------*/
if (dc1394_stop_iso_transmission(handle,camera.node)!=DC1394_SUCCESS)
{
printf("couldn't stop the camera?\n");
}

/*-----------------------------------------------------------------------
* save image as 'Image.pgm'
*-----------------------------------------------------------------------*/
imagefile=fopen(IMAGE_FILE_NAME, "w");

if( imagefile == NULL)
{
perror( "Can't create '" IMAGE_FILE_NAME "'");
dc1394_release_camera(handle,&camera);
dc1394_destroy_handle(handle);
exit( 1);
}


fprintf(imagefile,"P5\n%u %u 255\n", camera.frame_width,
camera.frame_height );
fwrite((const char *)camera.capture_buffer, 1,
camera.frame_height*camera.frame_width, imagefile);
fclose(imagefile);
printf("wrote: " IMAGE_FILE_NAME "\n");

/*-----------------------------------------------------------------------
* Close camera
*-----------------------------------------------------------------------*/
dc1394_release_camera(handle,&camera);
dc1394_destroy_handle(handle);
return 0;
}



Kodun derlenmesi ise şu şekilde yapılabilir;


gcc grabSingleImage.c -lraw1394 -ldc1394_control -o grabSingleImage

Comments

Popular posts from this blog

Latex'te Denklem İçerisine Ufak Boşluklar Koymak

LaTeX'te Sunum Hazırlamak

Octave'da Grafik Çizdirme