firewirecameras.cpp
来自「一个语言识别引擎」· C++ 代码 · 共 1,029 行 · 第 1/3 页
CPP
1,029 行
{
printf("\n"
"unable to setup camera %d:\n"
" - the camera may already be in use\n"
" - the requested video mode, framerate or format,\n"
" as specified on line %d of file \"%s\",\n"
" may not be supported by the camera\n\n",
camera_num, __LINE__,__FILE__);
//this command seems to seg fault here, not having it may lead to a memory leak
//dc1394_release_camera(handle, &(cameras[camera_num].capture));
return false;
}
}
/*
//not sure if this is needed
//dragonfly cameras may only support one mode anyway
// set trigger mode
if( dc1394_set_trigger_mode(handle, cameras[camera_num].capture.node, TRIGGER_MODE_0)
!= DC1394_SUCCESS)
{
printf("unable to set camera trigger mode\n");
dc1394_release_camera(handle,&(cameras[camera_num].capture));
dc1394_destroy_handle(handle);
exit(1);
}
*/
// Start isochronous data transmission
if (dc1394_start_iso_transmission(handle, cameras[camera_num].capture.node)
!=DC1394_SUCCESS)
{
printf("unable to start camera iso transmission\n");
if(handle!=NULL) {
dc1394_release_camera(handle, &(cameras[camera_num].capture));
}
return false;
}
return true;
}
void FWCameras::shutdown_camera( int camera_num )
{
if(!cam_num_ok(camera_num)) {
printf("camera %d was not properly started,\nso no attempt will be made to shut it down.\n", camera_num);
return;
}
if(handle==NULL) {
printf("Failure when trying to shutdown camera %d\n", camera_num);
printf("handle == NULL, so it was previously released or never initialized\n");
}
printf("trying to stop iso_transmission of camera #%d...", camera_num);
if (dc1394_stop_iso_transmission(handle, cameras[camera_num].capture.node)
!=DC1394_SUCCESS)
{
printf("couldn't stop iso_transmission of camera #%d!\n", camera_num);
}
else
{
printf("stopped.\n");
}
if(!dma_on)
{
printf("trying to release camera #%d, no dma...", camera_num);
dc1394_release_camera(handle, &(cameras[camera_num].capture));
printf("released.\n");
}
else
{
printf("trying to release camera #%d, with dma...", camera_num);
dc1394_dma_unlisten(handle, &(cameras[camera_num].capture));
dc1394_dma_release_camera(handle, &(cameras[camera_num].capture));
printf("released.\n");
}
}
void FWCameras::shutdown_firewire()
{
if(a_camera_is_ready())
{
printf("\nshutdown_firewire:\n");
printf("shutdown %d firewire camera(s):\n", num_cameras);
int c;
for(c = 0; c<num_cameras; c++)
{
printf("trying to shutdown camera #%d:\n", c);
if(handle!=NULL) shutdown_camera(c);
printf("camera #%d has been shutdown successfully.\n", c);
}
printf("all cameras have been shutdown.\n");
} else {
printf("no cameras need to be shutdown,\n"
"since none are currently valid and started\n");
}
printf("now trying to destroy handle for dc1394...");
if(handle != NULL) {
dc1394_destroy_handle(handle);
printf("destroyed.\n");
} else printf("handle is already NULL\n"
"apparently, firewire was not properly initialized\n");
printf("firewire shutdown complete.\n\n");
init_variables(dma_on);
}
bool FWCameras::capture_a_frame(int camera_num)
{
if(!cam_num_ok(camera_num)) return(false);
if(PRINTF_ON) printf("capture_a_frame from camera %d\n", camera_num);
if(!dma_on)
{
if(PRINTF_ON) printf("dc1394_single_capture(%d, %d)\n", (int)handle, (int)(&(cameras[camera_num].capture)));
if (dc1394_single_capture(handle, &(cameras[camera_num].capture))!=DC1394_SUCCESS)
{
printf("unable to capture a frame from camera #%d\n", camera_num);
printf("shutting down firewire...\n");
shutdown_firewire();
printf("done.\n");
exit(1);
}
} else
{
if(PRINTF_ON) printf("executing dc1394_dma_single_capture\n");
if (dc1394_dma_single_capture(&(cameras[camera_num].capture))!=DC1394_SUCCESS)
{
printf("unable to capture a frame from camera #%d\n", camera_num);
printf("shutting down firewire...\n");
shutdown_firewire();
printf("done.\n");
exit(1);
}
if(PRINTF_ON) printf("completed dc1394_dma_single_capture\n");
}
return(true);
}
bool FWCameras::release_capture_buffer(dc1394_cameracapture * camera, int camera_num)
{
if(dma_on) dc1394_dma_done_with_buffer(&(cameras[camera_num].capture));
return(true);
}
bool FWCameras::Capture(unsigned char *imout, int camera_num)
{
if(!cam_num_ok(camera_num)) return(false);
unsigned char * buf;
int buf_w, buf_h, buf_length;
switch(capture_size)
{
case _320x240:
buf_w=320; buf_h=240;
buf_length = buf_w*buf_h*3;
buf = new unsigned char[buf_length];
capture_320x240_color_image(buf, camera_num);
memcpy(imout, (char *) buf, buf_length);
delete [] buf;
break;
case _160x120:
buf_w=160; buf_h=120;
buf_length = buf_w*buf_h*3;
buf = new unsigned char[buf_length];
capture_color_image_3x3(buf, camera_num);
memcpy(imout, (char *) buf, buf_length);
delete [] buf;
break;
case _160x120_5x5:
buf_w=160; buf_h=120;
buf_length = buf_w*buf_h*3;
buf = new unsigned char[buf_length];
capture_color_image_5x5(buf, camera_num);
memcpy(imout, (char *) buf, buf_length);
delete [] buf;
break;
case _160x120_3x3:
buf_w=160; buf_h=120;
buf_length = buf_w*buf_h*3;
buf = new unsigned char[buf_length];
capture_color_image_3x3(buf, camera_num);
memcpy(imout, (char *) buf, buf_length);
delete [] buf;
break;
default:
buf_w=320; buf_h=240;
buf_length = buf_w*buf_h*3;
buf = new unsigned char[buf_length];
capture_320x240_color_image(buf, camera_num);
memcpy(imout, (char *) buf, buf_length);
delete [] buf;
break;
}
return true;
}
void FWCameras::capture_320x240_color_image(unsigned char * & buff_out, int camera_num )
{
if(!cam_num_ok(camera_num)) return;
capture_a_frame(camera_num);
int frame_width, frame_height;
frame_width = cameras[camera_num].capture.frame_width;
frame_height = cameras[camera_num].capture.frame_height;
unsigned char * buffer;
buffer = (unsigned char *) (cameras[camera_num].capture.capture_buffer);
if(PRINTF_ON) printf("frame_height = %d, frame_width = %d \n", frame_height, frame_width);
int dh, dw;
// dh = 4; dw = 4;
dh = 2; dw = 2;
int tw,th; th = frame_height/dh; tw = frame_width/dw;
//int sz = th*tw*3;
//int r_int;
int g_int, b_int;
//unsigned char r,g,b;
int h,w;
int index;
//set to 0
int temp_width, temp_height, temp_size;
temp_width = 320;
temp_height = 240;
temp_size = temp_width * temp_height * 3;
for(index=0; index<temp_size; index++)
buff_out[index] = 0;
for(h = 2; h < (frame_height-1); h=h+dh)
{
for(w = 2; w < (frame_width-1); w=w+dw)
{
index = ( (w/dw) + ((h/dh)*tw) ) * 3;
buff_out[index] = buffer[(h*frame_width)+w];
g_int = ( buffer[(h*frame_width)+(w-1)] + buffer[(h*frame_width)+(w+1)] +
buffer[((h-1)*frame_width)+w] + buffer[((h+1)*frame_width)+w])/4;
buff_out[index+1] = (unsigned char) g_int;
b_int = ( buffer[((h-1)*frame_width)+(w-1)] + buffer[((h+1)*frame_width)+(w+1)] +
buffer[((h-1)*frame_width)+(w+1)] + buffer[((h+1)*frame_width)+(w-1)])/4;
buff_out[index+2] = (unsigned char) b_int;
}
}
release_capture_buffer(&(cameras[camera_num].capture), camera_num);
}
void FWCameras::capture_color_image_5x5(unsigned char * & buff_out, int camera_num )
{
if(!cam_num_ok(camera_num)) return;
static unsigned char temp_buff [320*240*3];
capture_a_frame(camera_num);
int frame_width, frame_height;
frame_width = cameras[camera_num].capture.frame_width;
frame_height = cameras[camera_num].capture.frame_height;
unsigned char * buffer;
buffer = (unsigned char *) (cameras[camera_num].capture.capture_buffer);
if(PRINTF_ON) printf("frame_height = %d, frame_width = %d \n", frame_height, frame_width);
int dh, dw;
dh = 2; dw = 2;
int tw,th; th = frame_height/dh; tw = frame_width/dw;
//int sz = th*tw*3;
//int r_int
int g_int, b_int;
//unsigned char r,g,b;
int h,w;
int index;
int temp_width, temp_height, temp_size;
temp_width = 320; temp_height = 240; temp_size = temp_width * temp_height * 3;
//set to 0
for(index=0; index<temp_size; index++) temp_buff[index] = 0;
for(h = 2; h < (frame_height-1); h=h+dh)
{
for(w = 2; w < (frame_width-1); w=w+dw)
{
index = ( (w/dw) + ((h/dh)*tw) ) * 3;
temp_buff[index] = buffer[(h*frame_width)+w];
g_int = ( buffer[(h*frame_width)+(w-1)] + buffer[(h*frame_width)+(w+1)] +
buffer[((h-1)*frame_width)+w] + buffer[((h+1)*frame_width)+w])/4;
temp_buff[index+1] = (unsigned char) g_int;
b_int = ( buffer[((h-1)*frame_width)+(w-1)] + buffer[((h+1)*frame_width)+(w+1)] +
buffer[((h-1)*frame_width)+(w+1)] + buffer[((h+1)*frame_width)+(w-1)])/4;
temp_buff[index+2] = (unsigned char) b_int;
}
}
release_capture_buffer(&(cameras[camera_num].capture), camera_num);
/////////////////////////////////////////////
//perform a 5x5 separable convolution and downsample by 2
int out_width, out_height, out_size;
out_width = 160; out_height = 120; out_size = out_width * out_height * 3;
for(index=0; index < out_size; index++) buff_out[index] = 0;
int * row;
row = new int [out_width*3];
for(index=0; index < out_width*3; index++) row[index]=0;
int n; int x; int y;
int s = (temp_width*3);
for(h = 0; h < temp_height; h++)
{
index = h*(temp_width*3);
for(w = 2; w < out_width-2; w++)
{
x = 3 * w;
n = index + (2*x);
row[x+0] = (temp_buff[n-6] + (temp_buff[n-3]*4) + (temp_buff[n+0]*6) +
(temp_buff[n+3]*4) + temp_buff[n+6])/16;
row[x+1] = (temp_buff[n-5] + (temp_buff[n-2]*4) + (temp_buff[n+1]*6) +
(temp_buff[n+4]*4) + temp_buff[n+7])/16;
row[x+2] = (temp_buff[n-4] + (temp_buff[n-1]*4) + (temp_buff[n+2]*6) +
(temp_buff[n+5]*4) + temp_buff[n+8])/16;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?