int main(int argc, char *argv[]) { unsigned int i; dc1394_t * d; dc1394camera_list_t * list; dc1394error_t err; dc1394featureset_t features; d = dc1394_new(); if(!d) return 1; err = dc1394_camera_enumerate(d, &list); DC1394_ERR_RTN(err,"Failed to enumerate cameras"); if(list->num == 0) { dc1394_log_error("No cameras found"); return 1; } for(i = 0; i < list->num; i++) { dc1394camera_t *camera = dc1394_camera_new(d, list->ids[i].guid); if(camera) { unsigned int j; dc1394video_modes_t modes; // Print hardware informations. dc1394_camera_print_info(camera, stdout); // Print supported camera 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); } // Print a list of supported modes. printf("------ Supported Video Modes ------\n"); err = dc1394_video_get_supported_modes(camera, &modes); DC1394_ERR_RTN(err,"Could not get list of modes"); for(j = 0; j < modes.num; j++) { print_video_mode_info(camera, modes.modes[j]); } dc1394_camera_free(camera); } } dc1394_camera_free_list (list); dc1394_free (d); return 0; }
int _setVideoMode(int mode, long *sizex, long *sizey){ if(camera==NULL) { dc1394_log_error("Camera is not initialised"); return -1; } switch(mode) { case 0 : video_mode=DC1394_VIDEO_MODE_FORMAT7_0;break; case 1 : video_mode=DC1394_VIDEO_MODE_FORMAT7_1;break; case 2 : video_mode=DC1394_VIDEO_MODE_FORMAT7_2;break; case 3 : video_mode=DC1394_VIDEO_MODE_FORMAT7_3;break; default : ; } unsigned int h_size = 160; unsigned int v_size = 120; err = dc1394_format7_get_max_image_size(camera, video_mode, &h_size, &v_size); DC1394_ERR_CLN_RTN(err,_unsetupCam(),"Could not get max image size of Format7 mode"); *sizex = h_size; *sizey = v_size; err = dc1394_video_set_mode(camera, video_mode); DC1394_ERR_CLN_RTN(err,_unsetupCam(),"Could not set Format7 mode"); err = dc1394_format7_set_roi(camera, video_mode, DC1394_COLOR_CODING_MONO16, DC1394_USE_MAX_AVAIL, // use max packet size 0, 0, // left, top h_size, v_size); // width, height DC1394_ERR_RTN(err,"Unable to set Format7 mode 0.\nEdit the example file manually to fit your camera capabilities"); return 0; }
int main(int argc, char *argv[]) { dc1394camera_t * camera; dc1394error_t err; dc1394video_frame_t * frame; dc1394_t * d; dc1394camera_list_t * list; d = dc1394_new (); /* Initialize libdc1394 */ if (!d) return 1; err=dc1394_camera_enumerate (d, &list); /* Find cameras */ DC1394_ERR_RTN(err,"Failed to enumerate cameras"); if (list->num == 0) { /* Verify that we have at least one camera */ dc1394_log_error("No cameras found"); return 1; } camera = dc1394_camera_new (d, list->ids[0].guid); /* Work with first camera */ if (!camera) { dc1394_log_error("Failed to initialize camera with guid %llx", list->ids[0].guid); return 1; } dc1394_camera_free_list (list); err=dc1394_capture_setup(camera, 4, DC1394_CAPTURE_FLAGS_DEFAULT); /* Setup capture */ err=dc1394_video_set_transmission(camera, DC1394_ON); /* Start transmission */ err=dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_WAIT, &frame);/* Capture */ DC1394_ERR_RTN(err,"Problem getting an image"); err=dc1394_capture_enqueue(camera, frame); /* Release the buffer */ err=dc1394_video_set_transmission(camera, DC1394_OFF); /* Stop transmission */ err=dc1394_capture_stop(camera); /* Stop capture */ printf("Hello World\n"); /* Hey, this is a HELLO WORLD program!! */ dc1394_camera_free (camera); /* cleanup and exit */ dc1394_free (d); return 0; }
int _setupCam(long yExpo, long yGain, long *sizex, long *sizey, long speed){ dc1394camera_list_t * list; dc1394_t *d = dc1394_new (); if (!d) return -1; dc1394speed_t iso_speed; if(speed == 400) iso_speed = DC1394_ISO_SPEED_400; else if(speed == 800) iso_speed = DC1394_ISO_SPEED_800; else return -1; //iso_speed = DC1394_ISO_SPEED_400; dc1394_log_register_handler(DC1394_LOG_WARNING, NULL, NULL); err=dc1394_camera_enumerate (d, &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 (d, list->ids[0].guid); if (!camera) { dc1394_log_error("Failed to initialize camera with guid %lld", list->ids[0].guid); return -1; } dc1394_camera_free_list (list); printf("Using camera with GUID %lld\n", camera->guid); release_iso_and_bw(); uint32_t expo = yExpo; uint32_t gain = yGain; /*----------------------------------------------------------------------- * setup capture *-----------------------------------------------------------------------*/ dc1394_video_set_operation_mode(camera, DC1394_OPERATION_MODE_1394B); DC1394_ERR_CLN_RTN(err,_unsetupCam(),"Could not set operation mode"); err=dc1394_video_set_iso_speed(camera, iso_speed); DC1394_ERR_CLN_RTN(err,_unsetupCam(),"Could not set iso speed"); _setVideoMode(0, sizex, sizey); err=dc1394_feature_set_value(camera, DC1394_FEATURE_GAIN, gain); DC1394_ERR_CLN_RTN(err,_unsetupCam(),"Could not define gain"); err=dc1394_feature_set_value(camera, DC1394_FEATURE_SHUTTER, expo); DC1394_ERR_CLN_RTN(err,_unsetupCam(),"Could not define shutter"); return 0; }
int _getBinning(uint32_t *sX, uint32_t *sY){ if(camera==NULL) { dc1394_log_error("Camera is not initialised"); return -1; } err=dc1394_format7_get_unit_size(camera, video_mode, sX, sY); DC1394_ERR_RTN(err,"Unable to set Format7 mode 0.\nEdit the example file manually to fit your camera capabilities"); return 0; }
int _getMaxRes(uint32_t *sX, uint32_t *sY){ if(camera==NULL) { dc1394_log_error("Camera is not initialised"); return -1; } err=dc1394_avt_get_MaxResolution(camera, sX, sY); DC1394_ERR_RTN(err,"Unable to set Format7 mode 0.\nEdit the example file manually to fit your camera capabilities"); return 0; }
/* * check if a feature is enabled or not */ dc1394error_t dc1394_basler_sff_feature_is_enabled (dc1394camera_t* camera, dc1394basler_sff_feature_t feature_id, dc1394bool_t *is_enabled) { const sff_feature *feature_desc = NULL; uint64_t feature_address; uint32_t data; dc1394error_t err; if (camera == NULL || is_enabled == NULL) { err = DC1394_INVALID_ARGUMENT_VALUE; DC1394_ERR_RTN(err, "camera or is_enabled is NULL"); } feature_desc = basler_sff_registry_find_by_id (feature_id); if (feature_desc == NULL) return DC1394_FAILURE; /* check if this feature can be enabled by the generic function */ if (!feature_desc->generic) { err = DC1394_FUNCTION_NOT_SUPPORTED; DC1394_ERR_RTN(err, "cannot check feature with the generic enable function"); } err = get_sff_address_from_csr_guid (camera, &(feature_desc->csr_guid), &feature_address); DC1394_ERR_RTN(err, "Cannot get SFF address from GUID"); if (feature_address == 0) return DC1394_FAILURE; err = dc1394_get_register (camera, feature_address, &data); DC1394_ERR_RTN(err, "Cannot read SFF feature CSR register"); //fprintf (stderr, "%s: address = 0x%016"PRIx64" data = 0x%08x\n", __FUNCTION__, feature_address, data); if (data & BASLER_SFF_CSR_BIT_ENABLE) { *is_enabled = DC1394_TRUE; } else { *is_enabled = DC1394_FALSE; } return DC1394_SUCCESS; }
int _startCam(){ if(camera==NULL) { dc1394_log_error("Camera is not initialised"); return -1; } err=dc1394_capture_setup(camera, 4, DC1394_CAPTURE_FLAGS_DEFAULT); // err=dc1394_capture_setup(camera, 4, DC1394_CAPTURE_FLAGS_AUTO_ISO); // err=dc1394_capture_setup(camera, 4, DC1394_CAPTURE_FLAGS_CHANNEL_ALLOC & DC1394_CAPTURE_FLAGS_BANDWIDTH_ALLOC); DC1394_ERR_CLN_RTN(err,_unsetupCam(),"Could not setup camera-\nmake sure that the video mode and framerate are\nsupported by your camera"); /*----------------------------------------------------------------------- * print allowed and used packet size *-----------------------------------------------------------------------*/ unsigned int min_bytes, max_bytes; unsigned int actual_bytes; uint64_t total_bytes = 0; err=dc1394_format7_get_packet_parameters(camera, video_mode, &min_bytes, &max_bytes); DC1394_ERR_RTN(err,"Packet para inq error"); //printf( "camera reports allowed packet size from %d - %d bytes\n", min_bytes, max_bytes); err=dc1394_format7_get_packet_size(camera, video_mode, &actual_bytes); DC1394_ERR_RTN(err,"dc1394_format7_get_packet_size error"); //printf( "camera reports actual packet size = %d bytes\n", actual_bytes); err=dc1394_format7_get_total_bytes(camera, video_mode, &total_bytes); DC1394_ERR_RTN(err,"dc1394_query_format7_total_bytes error"); //printf( "camera reports total bytes per frame = %lld bytes\n",total_bytes); /*----------------------------------------------------------------------- * have the camera start sending us data *-----------------------------------------------------------------------*/ err=dc1394_video_set_transmission(camera, DC1394_ON); DC1394_ERR_CLN_RTN(err,_unsetupCam(),"Could not start camera iso transmission"); return 0; }
bool Camera::Init() { // VideoCapture Interface cv_image_ = cvCreateImage(cvSize(640,480), IPL_DEPTH_8U, 3); // Initialize libdc1394 if ((d = dc1394_new ()) == NULL) return false; // Find cameras err = dc1394_camera_enumerate(d, &list); DC1394_ERR_RTN(err, "Failed to enumerate cameras"); // Verify that we have at least one cameraDC1394_BYTE_ORDER_YUYV if(list->num == 0) { dc1394_log_error("No cameras found"); return false; } // Work with first camera found if ((camera = dc1394_camera_new (d, list->ids[0].guid)) == NULL) { dc1394_log_error("Failed to initialize camera with guid %llx", list->ids[0].guid); return false; } // Not using the list anymore, clean it up dc1394_camera_free_list(list); // Power ON dc1394_camera_set_power(camera, DC1394_ON); // Print camera info dc1394_camera_print_info(camera, stdout); // Setup capture err = dc1394_capture_setup(camera, 4, DC1394_CAPTURE_FLAGS_DEFAULT); // Set video mode dc1394_video_set_mode(camera, DC1394_VIDEO_MODE_640x480_YUV422); // Start transmission err = dc1394_video_set_transmission(camera, DC1394_ON); // VideoCapture Interface init_ = true; return true; }
/* * Tests whether the camera supports Basler SFF */ dc1394error_t dc1394_basler_sff_is_available (dc1394camera_t* camera, dc1394bool_t *available) { uint32_t data; dc1394error_t err; const uint32_t basler_feature_id_1 = 0x0030533b; const uint32_t basler_feature_id_2 = 0x73c3f000; if (camera == NULL || available == NULL) { err = DC1394_INVALID_ARGUMENT_VALUE; DC1394_ERR_RTN(err, "camera or available is NULL"); } *available = DC1394_FALSE; err = dc1394_set_adv_control_register (camera, 0x0U, basler_feature_id_1); DC1394_ERR_RTN(err, "Could not write the first quadlet of Basler feature ID"); err = dc1394_set_adv_control_register (camera, 0x4U, basler_feature_id_2); DC1394_ERR_RTN(err, "Could not write the second quadlet of Basler feature ID"); err = dc1394_get_adv_control_register (camera, 0x0U, &data); DC1394_ERR_RTN(err, "Could not read from the ACR"); if (data != 0xffffffff) { *available = DC1394_TRUE; return DC1394_SUCCESS; } err = dc1394_get_adv_control_register (camera, 0x4U, &data); DC1394_ERR_RTN(err, "Could not read from ACR + 4"); if (data != 0xffffffff) { *available = DC1394_TRUE; return DC1394_SUCCESS; } /* SFF is not supported */ return DC1394_SUCCESS; }
/** * Tests whether the camera supports the specified SFF feature */ dc1394error_t dc1394_basler_sff_feature_is_available (dc1394camera_t* camera, dc1394basler_sff_feature_t feature_id, dc1394bool_t *available) { const sff_feature *feature_desc = NULL; uint64_t feature_address = 0; dc1394error_t err; if (camera == NULL || available == NULL) { err = DC1394_INVALID_ARGUMENT_VALUE; DC1394_ERR_RTN(err, "dc1394_basler_sff_feature_is_available(): camera or available is NULL"); } feature_desc = basler_sff_registry_find_by_id (feature_id); if (feature_desc == NULL) { err = DC1394_FAILURE; DC1394_ERR_RTN(err, "unknown feature"); } /* if this feature uses has an image chunk, only allow it if the * camera IIDC version is >= 1.30 because the _dc1394_capture_setup_basic() * does not compute the frame byte size correctly for iidc version < 1.30 */ if (feature_desc->has_chunk && camera->iidc_version < DC1394_IIDC_VERSION_1_30) { err = DC1394_FAILURE; DC1394_ERR_RTN(err, "smart features which have image chunks cannot be used with cameras with a iidc_version lower than 1.30"); } /* get address for this feature, if the address is 0x0, then this feature is not available */ err = get_sff_address_from_csr_guid (camera, &(feature_desc->csr_guid), &feature_address); DC1394_ERR_RTN(err, "Cannot get SFF address from GUID"); if (feature_address == 0) *available = DC1394_FALSE; else *available = DC1394_TRUE; return DC1394_SUCCESS; }
/* * Private functions */ dc1394error_t get_sff_address_from_csr_guid (dc1394camera_t* camera, const dc1394basler_sff_guid_t* feature_guid, uint64_t* address) { dc1394error_t err; uint32_t data; if (camera == NULL || feature_guid == NULL || address == NULL) return DC1394_FAILURE; /* write GUID like this to BASLER_ADDRESS_SFF_INQUIRY_REGISTER: * 0x10 <- D1 * 0x14 <- D3 | D2 * 0x18 <- D4[3] | D4[2] | D4[1] | D4[0] * 0x1C <- D4[7] | D4[6] | D4[5] | D4[4] */ data = feature_guid->d1; err = dc1394_set_adv_control_register (camera, BASLER_ADDRESS_SFF_INQUIRY, data); DC1394_ERR_RTN(err, "Could not write D1 to SFF inquiry register"); data = ((uint32_t)feature_guid->d3) << 16 | feature_guid->d2; err = dc1394_set_adv_control_register (camera, BASLER_ADDRESS_SFF_INQUIRY + 0x4U, data); DC1394_ERR_RTN(err, "Could not write D3 | D2 to SFF inquiry register"); data = ((uint32_t)feature_guid->d4[3] << 24) | ((uint32_t)feature_guid->d4[2] << 16) | ((uint32_t)feature_guid->d4[1] << 8) | feature_guid->d4[0]; err = dc1394_set_adv_control_register (camera, BASLER_ADDRESS_SFF_INQUIRY + 0x8U, data); DC1394_ERR_RTN(err, "Could not write D4[3..0] to SFF inquiry register"); data = ((uint32_t)feature_guid->d4[7] << 24) | ((uint32_t)feature_guid->d4[6] << 16) | ((uint32_t)feature_guid->d4[5] << 8) | feature_guid->d4[4]; err = dc1394_set_adv_control_register (camera, BASLER_ADDRESS_SFF_INQUIRY + 0xCU, data); DC1394_ERR_RTN(err, "Could not write D4[7..4] to SFF inquiry register"); /* read address */ err = dc1394_get_adv_control_register (camera, BASLER_ADDRESS_SFF_ADDRESS, &data); DC1394_ERR_RTN(err, "Could not read first quadlet of address from SFF address register"); *address = data; err = dc1394_get_adv_control_register (camera, BASLER_ADDRESS_SFF_ADDRESS + 0x4U, &data); DC1394_ERR_RTN(err, "Could not read second quadlet of address from SFF address register"); *address |= ((uint64_t)data) << 32; *address -= CONFIG_ROM_BASE; return DC1394_SUCCESS; }
/** * Finds a specific SFF chunk in the frame buffer */ dc1394error_t dc1394_basler_sff_chunk_find (dc1394basler_sff_feature_t feature_id, void** chunk_data, void* frame_buffer, uint32_t frame_size, dc1394bool_t has_crc_checksum) { dc1394basler_sff_t chunk; dc1394bool_t found = DC1394_FALSE; dc1394error_t err; err = dc1394_basler_sff_chunk_iterate_init (&chunk, frame_buffer, frame_size, has_crc_checksum); DC1394_ERR_RTN(err, "dc1394_basler_sff_chunk_find(): dc1394_basler_sff_chunk_iterate_init() failed"); while ((err = dc1394_basler_sff_chunk_iterate (&chunk)) == DC1394_SUCCESS) { if (chunk.feature_id == feature_id) { found = DC1394_TRUE; break; } } if (!found) return DC1394_FAILURE; if (chunk_data) *chunk_data = chunk.chunk_data; return DC1394_SUCCESS; }
dc1394error_t dc1394_juju_capture_stop(platform_camera_t *craw) { dc1394camera_t * camera = craw->camera; struct fw_cdev_stop_iso stop; int i; if (craw->capture_is_set == 0) return DC1394_CAPTURE_IS_NOT_SET; stop.handle = craw->iso_handle; if (ioctl(craw->iso_fd, FW_CDEV_IOC_STOP_ISO, &stop) < 0) return DC1394_IOCTL_FAILURE; munmap(craw->buffer, craw->buffer_size); close(craw->iso_fd); for (i = 0; i<craw->num_frames; i++) release_frame(craw, i); free (craw->frames); craw->frames = NULL; craw->capture_is_set = 0; if (craw->capture_iso_resource) { if (juju_iso_deallocate (craw, craw->capture_iso_resource) < 0) dc1394_log_warning ("juju: Failed to deallocate iso resources"); craw->capture_iso_resource = NULL; } // stop ISO if it was started automatically if (craw->iso_auto_started>0) { dc1394error_t err=dc1394_video_set_transmission(camera, DC1394_OFF); DC1394_ERR_RTN(err,"Could not stop ISO!"); craw->iso_auto_started=0; } return DC1394_SUCCESS; }
int main(int argc, char *argv[]) { FILE* imagefile; dc1394camera_t *camera; unsigned int width, height; dc1394video_frame_t *frame=NULL; //dc1394featureset_t features; dc1394_t * d; dc1394camera_list_t * list; dc1394error_t err; int counter = 0; d = dc1394_new (); if (!d) return 1; err=dc1394_camera_enumerate (d, &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 (d, list->ids[0].guid); if (!camera) { dc1394_log_error("Failed to initialize camera with guid %llx", list->ids[0].guid); return 1; } dc1394_camera_free_list (list); printf("Using camera with GUID %" PRIx64 "\n", camera->guid); /*----------------------------------------------------------------------- * setup capture *-----------------------------------------------------------------------*/ 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, DC1394_VIDEO_MODE_FORMAT7_7); DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not set video mode\n"); err=dc1394_video_set_framerate(camera, DC1394_FRAMERATE_7_5); DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not set framerate\n"); err=dc1394_capture_setup(camera,4, DC1394_CAPTURE_FLAGS_DEFAULT); DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not setup camera-\nmake sure that the video mode and framerate are\nsupported by your camera\n"); /*----------------------------------------------------------------------- * 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\n"); for(;;){ /*----------------------------------------------------------------------- * 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\n"); dc1394_get_image_size_from_video_mode(camera, DC1394_VIDEO_MODE_FORMAT7_7, &width, &height); cv::Mat img = cv::Mat(height, width, CV_8U, frame->image); err = dc1394_capture_enqueue(camera, frame); frame=NULL; cv::imshow("Image", img); printf("Frame %d\n", counter); counter++; cv::waitKey(10); } /*----------------------------------------------------------------------- * stop data transmission *-----------------------------------------------------------------------*/ err=dc1394_video_set_transmission(camera,DC1394_OFF); DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not stop the camera?\n"); /*----------------------------------------------------------------------- * close camera *-----------------------------------------------------------------------*/ dc1394_video_set_transmission(camera, DC1394_OFF); dc1394_capture_stop(camera); dc1394_camera_free(camera); dc1394_free (d); return 0; }
int main(int argc, char *argv[]) { // first check for the correct number of arguments if (argc != numArguments) { printf("Wrong number of arguments, exiting\n"); exit(0); } time_t theTime = time(NULL); struct tm *aTime = localtime(&theTime); int day = aTime->tm_mday; int month = aTime->tm_mon + 1; int year = aTime->tm_year + 1900; int hour = aTime->tm_hour; int min = aTime->tm_min; int sec = aTime->tm_sec; char directory[128]; sprintf(directory, "/opt/data_collection/%d_%d_%d_%d_%d_%d", year, month, day, hour, min, sec); char cmd[128]; sprintf(cmd, "mkdir %s", directory); system(cmd); double lapse=0; double fps; double ntime=0, ptime=0, tmp; int index; dc1394video_frame_t *frame; dc1394error_t err; int cam_id=0; char fileName[128]; // init 1394 d = dc1394_new(); if (!d) return 1; // find cameras dc1394camera_list_t * list; err=dc1394_camera_enumerate (d, &list); DC1394_ERR_RTN(err,"Failed to enumerate cameras"); if (list->num == 0) { fprintf(stderr,"No cameras found\n"); return 1; } else { if (list->num<cam_id+1) { fprintf(stderr,"Not enough cameras found for id\n"); return 1; } } // use selected camera camera = dc1394_camera_new (d, list->ids[cam_id].guid); if (!camera) { dc1394_log_error("Failed to initialize camera with guid %llx", list->ids[cam_id].guid); return 1; } dc1394_camera_free_list (list); // setup video format dc1394video_mode_t video_mode; if (color == 0) { printf("video mode is MONO\n"); video_mode = DC1394_VIDEO_MODE_640x480_MONO8; } else { printf("video mode is RGB8\n"); video_mode = DC1394_VIDEO_MODE_640x480_RGB8; } dc1394framerate_t framerate = DC1394_FRAMERATE_7_5; //dc1394_video_set_transmission(video_mode, DC1394_OFF); // just in case dc1394_video_set_mode(camera, video_mode); dc1394_video_set_framerate(camera, framerate); dc1394_video_set_iso_speed(camera, DC1394_ISO_SPEED_400); // setup catpure err=dc1394_capture_setup(camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT); if (err!=DC1394_SUCCESS) { fprintf(stderr,"Could not set capture!\n"); return 0; } // start image transmission dc1394_video_set_transmission(camera, DC1394_ON); // first, see what we are controlling manually vs. auto autoExposure = atoi(argv[1]); autoGain = atoi(argv[3]); autoShutter = atoi(argv[5]); autoBrightness = atoi(argv[7]); // set what we want to control to manual... if (autoExposure == 1) { dc1394_feature_set_mode(camera, DC1394_FEATURE_EXPOSURE, DC1394_FEATURE_MODE_AUTO); } else { dc1394_feature_set_mode(camera, DC1394_FEATURE_EXPOSURE, DC1394_FEATURE_MODE_MANUAL); } if (autoGain == 1) { dc1394_feature_set_mode(camera, DC1394_FEATURE_GAIN, DC1394_FEATURE_MODE_AUTO); } else { dc1394_feature_set_mode(camera, DC1394_FEATURE_GAIN, DC1394_FEATURE_MODE_MANUAL); } if (autoShutter == 1) { dc1394_feature_set_mode(camera, DC1394_FEATURE_SHUTTER, DC1394_FEATURE_MODE_AUTO); } else { dc1394_feature_set_mode(camera, DC1394_FEATURE_SHUTTER, DC1394_FEATURE_MODE_MANUAL); } if (autoBrightness == 1) { dc1394_feature_set_mode(camera, DC1394_FEATURE_BRIGHTNESS, DC1394_FEATURE_MODE_AUTO); } else { dc1394_feature_set_mode(camera, DC1394_FEATURE_BRIGHTNESS, DC1394_FEATURE_MODE_MANUAL); } // print out the current features dc1394featureset_t features; err=dc1394_feature_get_all(camera, &features); if (err!=DC1394_SUCCESS) { dc1394_log_warning("Could not get the feature set"); } else { dc1394_feature_print_all(&features, stdout); } // set the exposure level if (argc == numArguments) { exposure = atoi(argv[2]); } err=dc1394_feature_set_value(camera, DC1394_FEATURE_EXPOSURE, exposure); if (err!=DC1394_SUCCESS) { printf("Could NOT set the exposure!\n"); } else { printf("exposure set to %d\n", exposure); } // set the gain if (argc == numArguments) { gain = atoi(argv[4]); } err=dc1394_feature_set_value(camera, DC1394_FEATURE_GAIN, gain); if (err!=DC1394_SUCCESS) { printf("Could NOT set the gain!\n"); } else { printf("gain set to %d\n", gain); } // set the shutter if (argc == numArguments) { shutter = atoi(argv[6]); } err=dc1394_feature_set_value(camera, DC1394_FEATURE_SHUTTER, shutter); if (err!=DC1394_SUCCESS) { printf("Could NOT set the shutter!\n"); } else { printf("shutter set to %d\n", shutter); } // set the brightness if (argc == numArguments) { brightness = atoi(argv[8]); } err=dc1394_feature_set_value(camera, DC1394_FEATURE_BRIGHTNESS, brightness); if (err!=DC1394_SUCCESS) { printf("Could NOT set the brightness!\n"); } else { printf("shutter set to %d\n", brightness); } // print out the current features //dc1394featureset_t features; err=dc1394_feature_get_all(camera, &features); if (err!=DC1394_SUCCESS) { dc1394_log_warning("Could not get the feature set"); } else { dc1394_feature_print_all(&features, stdout); } // go ahead and log our settings to a text file in the directory we created FILE *file; char fileNameSettings[256]; sprintf(fileNameSettings, "%s/settings.txt", directory); file = fopen(fileNameSettings, "a+"); fprintf(file,"exposure=%d,%d\n", autoExposure, exposure); fprintf(file,"gain=%d,%d\n", autoGain, gain); fprintf(file,"shutter=%d,%d\n",autoShutter, shutter); fprintf(file,"brightness=%d,%d\n",autoBrightness, brightness); fclose(file); // start the main loop int quit = 0; while (!quit) { // capture image err=dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_WAIT, &frame);/* Capture */ DC1394_ERR_RTN(err,"Problem getting an image"); // return buffer dc1394_capture_enqueue(camera, frame); int t=0; if (color == 1) { t=3; } else { t=1; } IplImage *tmp = cvCreateImage(cvSize(640,480),8,t); memcpy(tmp->imageData, frame->image, 640*480*t); cvShowImage("image", tmp); char c = cvWaitKey(10); // save the image sprintf(fileName, "%s/frame%06d.bmp", directory, count); count++; cvSaveImage(fileName, tmp, 0); cvReleaseImage(&tmp); } // end loop return 0; }
dc1394error_t dc1394_windows_capture_setup(platform_camera_t * craw, uint32_t num_dma_buffers, uint32_t flags) { dc1394camera_t * camera = craw->camera; dc1394error_t err; if (flags & DC1394_CAPTURE_FLAGS_DEFAULT) { flags = DC1394_CAPTURE_FLAGS_CHANNEL_ALLOC | DC1394_CAPTURE_FLAGS_BANDWIDTH_ALLOC; } // if capture is already set, abort if (craw->capture_is_set > 0) { return DC1394_CAPTURE_IS_RUNNING; } craw->capture.flags = flags; craw->allocated_channel = -1; // if auto iso is requested, stop ISO (if necessary) if (flags & DC1394_CAPTURE_FLAGS_AUTO_ISO) { dc1394switch_t is_iso_on; dc1394_video_get_transmission(camera, &is_iso_on); if (is_iso_on == DC1394_ON) { err=dc1394_video_set_transmission(camera, DC1394_OFF); DC1394_ERR_RTN(err,"Could not stop ISO!"); } } craw->capture.frames = malloc (num_dma_buffers * sizeof (dc1394video_frame_t)); if (!craw->capture.frames) { goto fail; } err = capture_basic_setup(camera, craw->capture.frames); if (err != DC1394_SUCCESS) { goto fail; } err = windows_capture_setup (craw, num_dma_buffers, flags); if (err != DC1394_SUCCESS) { goto fail; } // if auto iso is requested, start ISO if (flags & DC1394_CAPTURE_FLAGS_AUTO_ISO) { err=dc1394_video_set_transmission(camera, DC1394_ON); DC1394_ERR_RTN(err,"Could not start ISO!"); craw->iso_auto_started = 1; } craw->capture.num_dma_buffers = num_dma_buffers; return DC1394_SUCCESS; fail: // free resources if they were allocated if (craw->allocated_channel >= 0) { if (dc1394_iso_release_channel (camera, craw->allocated_channel) != DC1394_SUCCESS) dc1394_log_warning("Warning: Could not free ISO channel"); } if (craw->allocated_bandwidth) { if (dc1394_iso_release_bandwidth (camera, craw->allocated_bandwidth) != DC1394_SUCCESS) dc1394_log_warning("Warning: Could not free bandwidth"); } craw->allocated_channel = -1; craw->allocated_bandwidth = 0; free (craw->capture.frames); craw->capture.frames = NULL; dc1394_log_error ("Error: Failed to setup DMA capture"); return DC1394_FAILURE; }
bool ieee1394capture::init(RoboCompCamera::TCamParams ¶ms_, RoboCompJointMotor::JointMotorPrx head_ , RoboCompDifferentialRobot::DifferentialRobotPrx base_ ) { params = params_; head = head_; base = base_; int32_t i; fps = (dc1394framerate_t)params.FPS;//15; res = (dc1394video_mode_t)0; switch (fps) { case 1: fps = DC1394_FRAMERATE_1_875; break; case 3: fps = DC1394_FRAMERATE_3_75; break; case 15: fps = DC1394_FRAMERATE_15; break; case 30: fps = DC1394_FRAMERATE_30; break; case 60: fps = DC1394_FRAMERATE_60; break; default: fps = DC1394_FRAMERATE_7_5; break; } switch (res) { case 1: res = DC1394_VIDEO_MODE_640x480_YUV411; device_width = 640; device_height = 480; break; case 2: res = DC1394_VIDEO_MODE_640x480_RGB8; device_width = 640; device_height = 480; break; default: res = DC1394_VIDEO_MODE_320x240_YUV422; device_width = 320; device_height = 240; break; } /// Get handle qDebug() << "ieee1394capture::init() -> Initializating first Firewire Card in the system..."; if (!(d = dc1394_new())) { qDebug() << "ieee1394capture::init() -> Fatal error: Unable to aquire a handle to the Ieee1394 device"; qDebug() << "Please check if the kernel modules `ieee1394',`raw1394' and `ohci1394' are loaded or if you have read/write access to /dev/raw1394 and to /dev/video1394-0 " ; return false; } CREATED_BUS = true; /// Create camera interfaces numCameras = 0; err = dc1394_camera_enumerate(d, &list); DC1394_ERR_RTN(err, "Failed to enumerate cameras"); if (list->num == 0) { dc1394_log_error("No cameras found"); return 1; } numCameras = 0; for (uint32_t di = 0; di < list->num; di++) { if (numCameras >= MAX_CAMERAS) break; cameras[numCameras] = dc1394_camera_new(d, list->ids[di].guid); if (!cameras[numCameras]) { dc1394_log_warning("Failed to initialize camera with guid %llx", list->ids[di].guid); continue; } printf("Camera #%d\n", numCameras); numCameras++; } dc1394_camera_free_list(list); if ( numCameras < 1 ) { qDebug() << "ieee1394capture::init() -> Fatal error: No cameras found in the bus! Called from "; cleanup(); return false; } /// Check if one camera has become the root node /* for ( int n=0; n < numCameras; n++ ) { if ( cameraNodeList[n] == numNodes ) { qDebug() << "ieee1394capture::init() -> Fatal error: Sorry, your camera is the highest numbered node of the bus, and has therefore become the root node." ; cleanup(); return false; } }*/ CREATED_CAMS = true; /// Setup cameras for capture qDebug() << "ieee1394capture::init() -> Searching cameras with requested parameters:"; printf("%s\n",params.mode.c_str()); if (params.mode == "MODE_320x240_YUV422") { res = DC1394_VIDEO_MODE_320x240_YUV422; params.width = 320; params.height = 240; } else if (params.mode == "MODE_640x480_YUV422" ) { res = DC1394_VIDEO_MODE_640x480_YUV422; params.width = 640; params.height = 480; } else if (params.mode == "MODE_640x480_RGB" ) { res = DC1394_VIDEO_MODE_640x480_RGB8; params.width = 640; params.height = 480; } else if (params.mode == "MODE_640x480_YUV411") { res = DC1394_VIDEO_MODE_640x480_YUV411; params.width = 640; params.height = 480; } else if (params.mode == "MODE_640x480_MONO") { res = DC1394_VIDEO_MODE_640x480_MONO8; params.width = 640; params.height = 480; } else if (params.mode == "MODE_640x480_MONO16") { res = DC1394_VIDEO_MODE_640x480_MONO16; params.width = 640; params.height = 480; } else if (params.mode == "MODE_516x338_YUV422") { res = DC1394_VIDEO_MODE_FORMAT7_1; params.width = 516; params.height = 338; } else qFatal("ieee1394capture::init() -> Image Mode %s not available. Aborting...", params.mode.c_str()); params.size = params.width*params.height; if (params.FPS!=15 and params.FPS!=30) { qWarning("ieee1394capture::init() -> Framerate %d not available. Aborting...", params.FPS ); cleanup(); return false; } dc1394format7modeset_t info; for (i = 0; i < numCameras; i++) { if (params.mode == "MODE_516x338_YUV422") { err = dc1394_format7_get_modeset(cameras[i], &info); for( int j=0;j<DC1394_VIDEO_MODE_FORMAT7_NUM;j++) { qDebug() << info.mode[j].present; qDebug() << info.mode[j].size_x; qDebug() << info.mode[j].size_y; qDebug() << info.mode[j].max_size_x; qDebug() << info.mode[j].max_size_y; qDebug() << info.mode[j].pos_x; qDebug() << info.mode[j].pos_y; qDebug() << info.mode[j].unit_size_x; qDebug() << info.mode[j].unit_size_y; qDebug() << info.mode[j].unit_pos_x; qDebug() << info.mode[j].unit_pos_y; qDebug() << info.mode[j].pixnum; qDebug() << info.mode[j].packet_size; /* in bytes */ qDebug() << info.mode[j].unit_packet_size; qDebug() << info.mode[j].max_packet_size; } } release_iso_and_bw(i); err = dc1394_video_set_mode(cameras[i], res); DC1394_ERR_CLN_RTN(err, cleanup(), "Could not set video mode"); err = dc1394_video_set_iso_speed(cameras[i], DC1394_ISO_SPEED_400); DC1394_ERR_CLN_RTN(err, cleanup(), "Could not set ISO speed"); //For format 7 modes only if (params.mode == "MODE_516x338_YUV422") { // uint32_t packet_size; // err=dc1394_format7_set_image_size(cameras[i], res, 514, 384); // DC1394_ERR_RTN(err,"Could not set image size"); // err=dc1394_format7_get_recommended_packet_size(cameras[i], res, &packet_size); // DC1394_ERR_RTN(err,"Could not get format 7 recommended packet size"); // err=dc1394_format7_set_roi(cameras[i], res, DC1394_COLOR_CODING_YUV422, packet_size, 0,0, 514, 384); // DC1394_ERR_RTN(err,"Could not set ROI"); qDebug() << "ya"; } err = dc1394_video_set_framerate(cameras[i], fps); DC1394_ERR_CLN_RTN(err, cleanup(), "Could not set framerate"); err = dc1394_capture_setup(cameras[i], NUM_BUFFERS, DC1394_CAPTURE_FLAGS_DEFAULT); DC1394_ERR_CLN_RTN(err, cleanup(), "Could not setup camera-\nmake sure that the video mode and framerate are\nsupported by your camera"); err = dc1394_video_set_transmission(cameras[i], DC1394_ON); DC1394_ERR_CLN_RTN(err, cleanup(), "Could not start camera iso transmission"); } fflush(stdout); qDebug() << " ieee1394capture::init() -> Iso transmission started."; ///Buffers de imagen qDebug() << "BUFFERS DE IMAGEN ------------------------------------------"; for ( int i=0; i < numCameras; i++ ) { AimgBuffer[i] = ( Ipp8u * ) ippsMalloc_8u ( params.size * 9 ); BimgBuffer[i] = ( Ipp8u * ) ippsMalloc_8u ( params.size * 9 ); img8u_lum[i] = AimgBuffer[i]; img8u_YUV[i] = AimgBuffer[i]+params.size; localYRGBImgBufferPtr[i] = BimgBuffer[i]; qDebug() << "Reservando" << params.size * 9 <<" para localYRGBImgBufferPtr["<<i<<"]"; printf("(de %p a %p)\n", localYRGBImgBufferPtr[i], localYRGBImgBufferPtr[i]+(params.size*9-1)); } planos[0]=BimgBuffer[0]+params.size*3; planos[1]=BimgBuffer[0]+ ( params.size*4 ); planos[2]=BimgBuffer[0]+ ( params.size*5 ); //img8u_aux = BimgBuffer[0]+(params.size*6); imgSize_ipp.width=params.width; imgSize_ipp.height=params.height; return true; }
int main(int argc, char *argv[]) { double lapse=0; double fps; double ntime=0, ptime=0, tmp; int index; dc1394video_frame_t *frame; dc1394error_t err; GLuint texture; TimeLapse_t timer; int cam_id=0; if ( argc!=1 && argc!=2) { fprintf(stderr,"Usage:\n\tdc1394_iso <optional camera_ID>\n"); fprintf(stderr,"\t\tDefault values for histogram are min=%.0f, max=%.0f and %d bins\n\n",DEF_BIN_MIN, DEF_BIN_MAX, (int)DEF_BIN_N); exit(0); } signal(SIGINT, DisplayStatsAndExit); if (argc==2) cam_id=atoi(argv[1]); bin_n=DEF_BIN_N; bin_max=DEF_BIN_MAX; bin_min=DEF_BIN_MIN; bins = calloc(bin_n,sizeof(unsigned int)); // init 1394 d = dc1394_new(); if (!d) return 1; // find cameras dc1394camera_list_t * list; err=dc1394_camera_enumerate (d, &list); DC1394_ERR_RTN(err,"Failed to enumerate cameras"); if (list->num == 0) { fprintf(stderr,"No cameras found\n"); return 1; } else { if (list->num<cam_id+1) { fprintf(stderr,"Not enough cameras found for id\n"); return 1; } } // use selected camera camera = dc1394_camera_new (d, list->ids[cam_id].guid); if (!camera) { dc1394_log_error("Failed to initialize camera with guid %llx", list->ids[cam_id].guid); return 1; } dc1394_camera_free_list (list); // setup video format dc1394_video_set_transmission(camera, DC1394_OFF); // just in case dc1394_video_set_mode(camera, MODE); dc1394_video_set_iso_speed(camera, DC1394_ISO_SPEED_400); err=dc1394_format7_set_roi(camera, MODE, DC1394_COLOR_CODING_MONO8, PKT, 0, 0, SX, SY); if (err!=DC1394_SUCCESS) { fprintf(stderr,"Could not set ROI!\n"); return 0; } // setup catpure err=dc1394_capture_setup(camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT); if (err!=DC1394_SUCCESS) { fprintf(stderr,"Could not set capture!\n"); return 0; } fps_min=9e6; fps_max=0; fprintf(stderr,"\t\t\t\t------------- FPS -------------- \n"); fprintf(stderr,"Time:\t\tFrames:\t\tInst:\tMin:\tMax:\tAvg:\n"); fprintf(stderr,"Stabilizing...\r"); unsigned int stabilized=0; video=SDL_Start_GL(SX, SY); // start image transmission dc1394_video_set_transmission(camera, DC1394_ON); ResetTimer(&timer); // ----- ready to roll on all images! ----- int quit = 0; while (!quit) { // capture image err=dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_WAIT, &frame);/* Capture */ DC1394_ERR_RTN(err,"Problem getting an image"); // display image glGenTextures( 1, &texture); glBindTexture( GL_TEXTURE_2D, texture); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexImage2D( GL_TEXTURE_2D, 0, GL_LUMINANCE8, SX, SY, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, frame->image ); // return buffer dc1394_capture_enqueue(camera, frame); glBegin( GL_QUADS ); { glTexCoord2f(0, 0); glVertex3f(0, 0, 1 ); glTexCoord2f(0, 1); glVertex3f(0, SY, 1 ); glTexCoord2f(1, 1); glVertex3f(SX, SY, 1 ); glTexCoord2f(1, 0); glVertex3f(SX, 0, 1 ); } glEnd(); SDL_GL_SwapBuffers(); glDeleteTextures(1, &texture); if (ntime>0.1) // if ntime is valid... ptime=ntime; ntime=(double)frame->timestamp/1000000.0; // display the framerate nframes++; fps=1.0/(ntime-ptime+1e-20); tmp=GetTimeLapseSec(&timer); lapse+=tmp; if (lapse>STABILIZING_TIME && stabilized==0) { stabilized=1; lapse=tmp; nframes=1; fprintf(stderr," \r"); } if (stabilized>0) { if (nframes>1) { fprintf(stderr,"%9.3f\t%7d \t%-3.2f \t%3.2f \t%3.2f \t%3.3f \r", lapse, nframes, fps, fps_min, fps_max, ((float)nframes)/(lapse+1e-10)); } index=(int)((fps-bin_min)/((double)(bin_max-bin_min)/(double)bin_n)); if (index>bin_n-1) index=bin_n-1; if (index<0) index=0; bins[index]++; if (fps_min>fps) fps_min=fps; if (fps_max<fps) fps_max=fps; } SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: quit = 1; break; } } } // --------------- END OF BIG IMAGE LOOP ------------------- DisplayStatsAndExit(0); return 0; }
int main(int argc,char *argv[]) { XEvent xev; XGCValues xgcv; long background=0x010203; int i, j; dc1394_t * d; dc1394camera_list_t * list; get_options(argc,argv); /* process options */ switch(fps) { case 1: fps = DC1394_FRAMERATE_1_875; break; case 3: fps = DC1394_FRAMERATE_3_75; break; case 15: fps = DC1394_FRAMERATE_15; break; case 30: fps = DC1394_FRAMERATE_30; break; case 60: fps = DC1394_FRAMERATE_60; break; default: fps = DC1394_FRAMERATE_7_5; break; } switch(res) { case 1: res = DC1394_VIDEO_MODE_640x480_YUV411; device_width=640; device_height=480; format=XV_YUY2; break; case 2: res = DC1394_VIDEO_MODE_640x480_RGB8; device_width=640; device_height=480; format=XV_YUY2; break; case 3: res = DC1394_VIDEO_MODE_800x600_YUV422; device_width=800; device_height=600; format=XV_UYVY; break; default: res = DC1394_VIDEO_MODE_320x240_YUV422; device_width=320; device_height=240; format=XV_UYVY; break; } dc1394error_t err; d = dc1394_new (); if (!d) return 1; err=dc1394_camera_enumerate (d, &list); DC1394_ERR_RTN(err,"Failed to enumerate cameras"); if (list->num == 0) { dc1394_log_error("No cameras found"); return 1; } j = 0; for (i = 0; i < list->num; i++) { if (j >= MAX_CAMERAS) break; cameras[j] = dc1394_camera_new (d, list->ids[i].guid); if (!cameras[j]) { dc1394_log_warning("Failed to initialize camera with guid %llx", list->ids[i].guid); continue; } j++; } numCameras = j; dc1394_camera_free_list (list); if (numCameras == 0) { dc1394_log_error("No cameras found"); exit (1); } /* setup cameras for capture */ for (i = 0; i < numCameras; i++) { //err=dc1394_video_set_iso_speed(cameras[i], DC1394_ISO_SPEED_400); //dc1394_video_set_operation_mode(my_camera_ptr[i], DC1394_OPERATION_MODE_1394B); err= dc1394_video_set_operation_mode(cameras[i], DC1394_OPERATION_MODE_1394B); err=dc1394_video_set_iso_speed(cameras[i], DC1394_ISO_SPEED_800); DC1394_ERR_CLN_RTN(err,cleanup(),"Could not set ISO speed"); err=dc1394_video_set_mode(cameras[i], res); DC1394_ERR_CLN_RTN(err,cleanup(),"Could not set video mode"); err=dc1394_video_set_framerate(cameras[i], fps); DC1394_ERR_CLN_RTN(err,cleanup(),"Could not set framerate"); err=dc1394_capture_setup(cameras[i],NUM_BUFFERS, DC1394_CAPTURE_FLAGS_DEFAULT); DC1394_ERR_CLN_RTN(err,cleanup(),"Could not setup camera-\nmake sure that the video mode and framerate are\nsupported by your camera"); err=dc1394_video_set_transmission(cameras[i], DC1394_ON); DC1394_ERR_CLN_RTN(err,cleanup(),"Could not start camera iso transmission"); // Camera settings err = dc1394_feature_set_value(cameras[i],DC1394_FEATURE_SHUTTER,1400); err = dc1394_feature_set_value(cameras[i],DC1394_FEATURE_BRIGHTNESS,800); err = dc1394_feature_set_value(cameras[i],DC1394_FEATURE_EXPOSURE,150); err = dc1394_feature_whitebalance_set_value(cameras[i],500,400); } fflush(stdout); if (numCameras < 1) { perror("no cameras found :(\n"); cleanup(); exit(-1); } switch(format){ case XV_YV12: set_frame_length(device_width*device_height*3/2, numCameras); break; case XV_YUY2: case XV_UYVY: set_frame_length(device_width*device_height*2, numCameras); break; default: dc1394_log_error("Unknown format set (internal error)"); exit(255); } /* make the window */ display=XOpenDisplay(getenv("DISPLAY")); if(display==NULL) { dc1394_log_error("Could not open display \"%s\"",getenv("DISPLAY")); cleanup(); exit(-1); } QueryXv(); if ( adaptor < 0 ) { cleanup(); exit(-1); } width=device_width; height=device_height * numCameras; //width=device_width * numCameras; //height=device_height; window=XCreateSimpleWindow(display,DefaultRootWindow(display),0,0,width,height,0, WhitePixel(display,DefaultScreen(display)), background); XSelectInput(display,window,StructureNotifyMask|KeyPressMask); XMapWindow(display,window); connection=ConnectionNumber(display); gc=XCreateGC(display,window,0,&xgcv); /* main event loop */ while(1){ for (i = 0; i < numCameras; i++) { if (dc1394_capture_dequeue(cameras[i], DC1394_CAPTURE_POLICY_WAIT, &frames[i])!=DC1394_SUCCESS) dc1394_log_error("Failed to capture from camera %d", i); } display_frames(); XFlush(display); while(XPending(display)>0){ XNextEvent(display,&xev); switch(xev.type){ case ConfigureNotify: width=xev.xconfigure.width; height=xev.xconfigure.height; display_frames(); break; case KeyPress: switch(XKeycodeToKeysym(display,xev.xkey.keycode,0)){ case XK_q: case XK_Q: cleanup(); exit(0); break; case XK_comma: case XK_less: //width=width/2; //height=height/2; width--; XResizeWindow(display,window,width,height); display_frames(); break; case XK_period: case XK_greater: //width=2*width; //height=2*height; width++; XResizeWindow(display,window,width,height); display_frames(); break; case XK_0: freeze = !freeze; break; case XK_1: fps = DC1394_FRAMERATE_1_875; for (i = 0; i < numCameras; i++) dc1394_video_set_framerate(cameras[i], fps); break; case XK_2: fps = DC1394_FRAMERATE_3_75; for (i = 0; i < numCameras; i++) dc1394_video_set_framerate(cameras[i], fps); break; case XK_4: fps = DC1394_FRAMERATE_15; for (i = 0; i < numCameras; i++) dc1394_video_set_framerate(cameras[i], fps); break; case XK_5: fps = DC1394_FRAMERATE_30; for (i = 0; i < numCameras; i++) dc1394_video_set_framerate(cameras[i], fps); break; case XK_3: fps = DC1394_FRAMERATE_7_5; for (i = 0; i < numCameras; i++) dc1394_video_set_framerate(cameras[i], fps); break; } break; } } /* XPending */ for (i = 0; i < numCameras; i++) { if (frames[i]) dc1394_capture_enqueue (cameras[i], frames[i]); } } /* while not interrupted */ exit(0); }
int main(int argc, char *argv[]) { dc1394_t * d; dc1394camera_list_t * list; dc1394camera_t *camera; dc1394error_t err; d = dc1394_new (); if (!d) return 1; int cam=0; int num=-1; int chan; unsigned int val; do { err=dc1394_camera_enumerate (d, &list); DC1394_ERR_RTN(err,"Failed to enumerate cameras"); if (list->num == 0) { dc1394_log_error("No cameras found"); return 1; } else { num=list->num; } printf("Camera %d\n",cam); camera = dc1394_camera_new (d, list->ids[cam].guid); if (!camera) { dc1394_log_error("Failed to initialize camera with guid %llx", list->ids[cam].guid); return 1; } dc1394_camera_free_list (list); printf("Using camera with GUID %"PRIx64"\n", camera->guid); printf ("Reseting bus...\n"); if (dc1394_reset_bus (camera) != DC1394_SUCCESS) printf ("Warning: reset reported error\n"); dc1394_capture_stop(camera); dc1394_iso_release_all(camera); if ( dc1394_video_get_bandwidth_usage(camera,&val) == DC1394_SUCCESS && dc1394_iso_release_bandwidth(camera,val) == DC1394_SUCCESS) { printf("Freed %d Bandwidth\n",val); } if ( dc1394_video_get_iso_channel(camera,&val) == DC1394_SUCCESS && dc1394_iso_release_channel(camera,val) == DC1394_SUCCESS) { printf("Freed ISO %d\n",val); } dc1394_video_set_transmission(camera, DC1394_OFF); //dc1394_iso_release_bandwidth(camera, 10000000); /*dc1394_iso_release_bandwidth(camera, INT_MAX); for(chan=0;chan<64;chan++) { dc1394_iso_release_channel(camera,chan); }*/ dc1394_camera_free (camera); cam++; } while(cam<num); dc1394_free (d); return 0; }
dc1394error_t dc1394_juju_capture_setup(platform_camera_t *craw, uint32_t num_dma_buffers, uint32_t flags) { struct fw_cdev_create_iso_context create; struct fw_cdev_start_iso start_iso; dc1394error_t err; dc1394video_frame_t proto; int i, j, retval; dc1394camera_t * camera = craw->camera; if (flags & DC1394_CAPTURE_FLAGS_DEFAULT) flags = DC1394_CAPTURE_FLAGS_CHANNEL_ALLOC | DC1394_CAPTURE_FLAGS_BANDWIDTH_ALLOC; craw->flags = flags; // if capture is already set, abort if (craw->capture_is_set>0) return DC1394_CAPTURE_IS_RUNNING; // if auto iso is requested, stop ISO (if necessary) if (flags & DC1394_CAPTURE_FLAGS_AUTO_ISO) { dc1394switch_t is_iso_on; dc1394_video_get_transmission(camera, &is_iso_on); if (is_iso_on == DC1394_ON) { err=dc1394_video_set_transmission(camera, DC1394_OFF); DC1394_ERR_RTN(err,"Could not stop ISO!"); } } if (capture_basic_setup(camera, &proto) != DC1394_SUCCESS) { dc1394_log_error("basic setup failed"); return DC1394_FAILURE; } if (flags & (DC1394_CAPTURE_FLAGS_CHANNEL_ALLOC | DC1394_CAPTURE_FLAGS_BANDWIDTH_ALLOC)) { uint64_t channels_allowed = 0; unsigned int bandwidth_units = 0; int channel; if (flags & DC1394_CAPTURE_FLAGS_CHANNEL_ALLOC) channels_allowed = 0xffff; if (flags & DC1394_CAPTURE_FLAGS_BANDWIDTH_ALLOC) dc1394_video_get_bandwidth_usage (camera, &bandwidth_units); err = juju_iso_allocate (craw, channels_allowed, bandwidth_units, &craw->capture_iso_resource); if (err == DC1394_SUCCESS) { channel = craw->capture_iso_resource->channel; } else if (err == DC1394_FUNCTION_NOT_SUPPORTED) { channel = craw->node_id & 0x3f; dc1394_log_warning ("iso allocation not available in this kernel, " "using channel %d...", channel); } else { dc1394_log_error ("juju: Failed to allocate iso resources"); return err; } if (dc1394_video_set_iso_channel (camera, channel) != DC1394_SUCCESS) return DC1394_NO_ISO_CHANNEL; } if (dc1394_video_get_iso_channel (camera, &craw->iso_channel) != DC1394_SUCCESS) return DC1394_FAILURE; dc1394_log_debug ("juju: Receiving from iso channel %d", craw->iso_channel); craw->iso_fd = open(craw->filename, O_RDWR); if (craw->iso_fd < 0) { dc1394_log_error("error opening file: %s", strerror (errno)); return DC1394_FAILURE; } create.type = FW_CDEV_ISO_CONTEXT_RECEIVE; create.header_size = craw->header_size; create.channel = craw->iso_channel; create.speed = SCODE_400; err = DC1394_IOCTL_FAILURE; if (ioctl(craw->iso_fd, FW_CDEV_IOC_CREATE_ISO_CONTEXT, &create) < 0) { dc1394_log_error("failed to create iso context"); goto error_fd; } craw->iso_handle = create.handle; craw->num_frames = num_dma_buffers; craw->current = -1; craw->buffer_size = proto.total_bytes * num_dma_buffers; craw->buffer = mmap(NULL, craw->buffer_size, PROT_READ | PROT_WRITE , MAP_SHARED, craw->iso_fd, 0); err = DC1394_IOCTL_FAILURE; if (craw->buffer == MAP_FAILED) goto error_fd; err = DC1394_MEMORY_ALLOCATION_FAILURE; craw->frames = malloc (num_dma_buffers * sizeof *craw->frames); if (craw->frames == NULL) goto error_mmap; for (i = 0; i < num_dma_buffers; i++) { err = init_frame(craw, i, &proto); if (err != DC1394_SUCCESS) { dc1394_log_error("error initing frames"); break; } } if (err != DC1394_SUCCESS) { for (j = 0; j < i; j++) release_frame(craw, j); goto error_mmap; } for (i = 0; i < num_dma_buffers; i++) { err = queue_frame(craw, i); if (err != DC1394_SUCCESS) { dc1394_log_error("error queuing"); goto error_frames; } } // starting from here we use the ISO channel so we set the flag in // the camera struct: craw->capture_is_set = 1; start_iso.cycle = -1; start_iso.tags = FW_CDEV_ISO_CONTEXT_MATCH_ALL_TAGS; start_iso.sync = 1; start_iso.handle = craw->iso_handle; retval = ioctl(craw->iso_fd, FW_CDEV_IOC_START_ISO, &start_iso); err = DC1394_IOCTL_FAILURE; if (retval < 0) { dc1394_log_error("error starting iso"); goto error_frames; } // if auto iso is requested, start ISO if (flags & DC1394_CAPTURE_FLAGS_AUTO_ISO) { err=dc1394_video_set_transmission(camera, DC1394_ON); DC1394_ERR_RTN(err,"Could not start ISO!"); craw->iso_auto_started=1; } return DC1394_SUCCESS; error_frames: for (i = 0; i < num_dma_buffers; i++) release_frame(craw, i); error_mmap: munmap(craw->buffer, craw->buffer_size); error_fd: close(craw->iso_fd); return err; }
int main (int argc, char **argv) { char* ntry = (char*)""; if (argc > 1) { ntry = argv[1]; } double fps = FPS; double target_dur = 1.0/fps; double tol = 1.0e-3; double total_dur = 0.0; dc1394_t * d = dc1394_new(); if (!d) { return 1; } dc1394camera_list_t * list; dc1394error_t err = dc1394_camera_enumerate (d, &list); DC1394_ERR_RTN(err,"Failed to enumerate cameras"); if (list->num == 0) { /* Verify that we have at least one camera */ dc1394_log_error("No cameras found"); return 1; } gCamera.init(d, list->ids[0].guid); if (!gCamera.cam()) { dc1394_log_error("Failed to initialize camera with guid %ld", list->ids[0].guid); dc1394_camera_free_list (list); return 1; } dc1394_camera_free_list (list); /*----------------------------------------------------------------------- * have the camera start sending us data *-----------------------------------------------------------------------*/ err = gCamera.start_transmission(); DC1394_ERR_CLN_RTN(err,cleanup_and_exit(gCamera),"Could not start camera iso transmission"); /*----------------------------------------------------------------------- * capture one frame *-----------------------------------------------------------------------*/ uint32_t width = 0; uint32_t height = 0; gCamera.get_image_size(&width, &height); cv::Mat mapping = cv::getRotationMatrix2D(cv::Point2f(width/2.0, height/2.0), 180.0, 1.0); #ifdef USE_SDL static char *var = (char*)"SDL_VIDEO_WINDOW_POS=\"1280,480\""; int ret = putenv(var); if (SDL_Init(SDL_INIT_VIDEO) != 0) { std::cerr << "DC1394: Unable to initialize SDL: " << SDL_GetError() << std::endl; return 1; } atexit(SDL_Quit); SDL_Surface *screen; screen = SDL_SetVideoMode(width, height, 24, SDL_HWSURFACE); if (screen == NULL) { std::cerr << "DC1394: Unable to set SDL video mode:" << SDL_GetError() << std::endl; } SDL_Event event; #endif #ifndef LICKOMETER pthread_t save_thread, acq_thread; pthread_create( &save_thread, NULL, &thread_save_image, NULL); #endif pthread_t save_thread, acq_thread; pthread_create( &acq_thread, NULL, &thread_acq_image, NULL); timespec t_sleep, t_rem; t_sleep.tv_sec = 0; t_sleep.tv_nsec = 1000; #ifndef STANDALONE int s; if ((s = socket(SOCKTYPE, SOCK_STREAM, 0)) < 0) { perror("DC1394: client: socket"); cleanup_and_exit(gCamera); return 1; } /* * Create the address we will be connecting to. */ #ifndef INET sockaddr_un sa; sa.sun_family = AF_UNIX; std::ostringstream tmpfn; tmpfn << "fwsocket" << ntry; std::cout << "DC1394: socket name " << tmpfn.str() << std::endl; int nameLen = strlen(tmpfn.str().c_str()); if (nameLen >= (int) sizeof(sa.sun_path) -1) { /* too long? */ cleanup_and_exit(gCamera); return 1; } sa.sun_path[0] = '\0'; /* abstract namespace */ strcpy(sa.sun_path+1, tmpfn.str().c_str()); int len = 1 + nameLen + offsetof(struct sockaddr_un, sun_path); #else sockaddr_in sa; bzero((char *) &sa, sizeof(sa)); sa.sin_family = AF_INET; hostent *server = gethostbyname("128.40.156.129"); bcopy((char *)server->h_addr, (char *)&sa.sin_addr.s_addr, server->h_length); sa.sin_port = htons(35000); int len = sizeof(sa); #endif /* * Try to connect to the address. For this to * succeed, the server must already have bound * this address, and must have issued a listen() * request. * * The third argument indicates the "length" of * the structure, not just the length of the * socket name. */ std::cout << "DC1394: Waiting for connection... " << std::flush; while (true) { // wait for connection: if (connect(s, (sockaddr*)&sa, len) < 0) { nanosleep(&t_sleep, &t_rem); } else { break; } } std::cout << "done" << std::endl; bool connected = false; std::vector<char> data(BUFSIZE); int nrec = recv(s, &data[0], data.size(), 0); std::string datastr(data.begin(), data.end()); if (nrec<=0) { std::cerr << "DC1394: Didn't receive start message; exiting now" << std::endl; cleanup_and_exit(gCamera); close(s); return 1; } connected = true; std::string ready = "ready"; while (send(s, ready.c_str(), ready.size(), 0) < 0) { perror("DC1394: client: send"); } int flags = 0; if (-1 == (flags = fcntl(s, F_GETFL, 0))) flags = 0; if (fcntl(s, F_SETFL, flags | O_NONBLOCK)==-1) { perror("DC1394: client: unblock"); } #endif /* pthread_mutex_lock( &camera_mutex ); gCamera.wait_for_trigger(); pthread_mutex_unlock( &camera_mutex ); Wait for acq_frame_buffer to fill instead */ int ncount = 0; cv::Mat im(cv::Size(width, height), CV_8UC1); cv::Mat thresh = cv::Mat::ones(cv::Size(width, height), CV_8UC1); cv::Mat prevs(cv::Size(width, height), CV_8UC1); cv::Mat gray(cv::Size(width, height), CV_8UC1); // wait for image: int nframes = get_image(im, mapping, false, -1, "", ncount); std::cout << "DC1394: Waiting for first image to arrive... " << std::flush; int nwait = 0; while (!nframes) { nanosleep(&t_sleep, &t_rem); std::cout << "." << std::flush; nframes = get_image(im, mapping, false, -1, "", ncount); nwait++; #ifdef STANDALONE if (nwait > 1000) { #else if (nwait > 100000) { #endif std::cout << "Time out, stopping now\n"; cleanup_and_exit(gCamera); } } timespec time0; clock_gettime(CLOCK_REALTIME, &time0); std::cout << "DC1394: image arrived: " << IplImage(im).depth << " bits, " << IplImage(im).nChannels << " channels, " << IplImage(im).widthStep << " step width" << std::endl; #ifdef USE_SDL SDL_Surface *surface = SDL_CreateRGBSurfaceFrom((void*)im.data, im.cols, im.rows, IplImage(im).depth*IplImage(im).nChannels, IplImage(im).widthStep, 0xffffff, 0xffffff, 0xffffff, 0); screen = SDL_GetVideoSurface(); if(SDL_BlitSurface(surface, NULL, screen, NULL) == 0) SDL_UpdateRect(screen, 0, 0, 0, 0); #else cv::namedWindow("DC1394", CV_WINDOW_AUTOSIZE); cvMoveWindow("DC1394", 1280, 480); cv::imshow("DC1394", im); #endif timespec time1 = time0; timespec time2 = time0; timespec time3 = time0; timespec time4 = time0; timespec t_disconnect = time0; timespec t_notrigger = time0; #ifdef STANDALONE int s = -1; #endif std::string fn = ""; #ifdef LICKOMETER std::string fn_lick = ""; FILE* fp_lick = NULL; #endif int key = 0; int nloop = 0; while (true) { clock_gettime( CLOCK_REALTIME, &time1); #ifndef STANDALONE std::vector<char> data(BUFSIZE); int nrec = recv(s, &data[0], data.size(), 0); std::string datastr(data.begin(), data.end()); #endif nframes += get_image(im, mapping, false, s, fn, ncount); #ifndef STANDALONE // no update from blender in a long time, terminate process if (datastr.find("1")==std::string::npos) { if (connected) { t_disconnect = time1; connected = false; } else { if (tdiff(time1, t_disconnect) > TIMEOUT) { std::cout << "DC1394: Received termination signal" << std::endl; close(s); pthread_cancel(acq_thread); pthread_cancel(save_thread); return 0; } } } else { connected = true; } /* Explicit termination */ if (datastr.find("quit")!=std::string::npos) { std::cout << "DC1394: Game over signal." << std::endl; std::string sclose = "close"; while (send(s, sclose.c_str(), sclose.size(), 0) < 0) { perror("DC1394: client: send"); } close(s); pthread_cancel(acq_thread); pthread_cancel(save_thread); return 0; } // Stop recording if (datastr.find("stop") != std::string::npos && fn != "") { fn = ""; #ifdef LICKOMETER fn_lick = ""; if (fp_lick) { fclose(fp_lick); fp_lick = NULL; } #endif std::cout << "DC1394: Stopping video" << std::endl; connected = true; ncount = 0; } // Start recording if (datastr.find("avi") != std::string::npos && datastr.find("stop") == std::string::npos && fn == "") { std::size_t startpos = datastr.find("begin")+5; std::size_t endpos = datastr.find("end") - datastr.find("begin") - 5; fn = datastr.substr(startpos, endpos); fn = std::string(trunk) + "data/" + fn; #ifdef LICKOMETER fn_lick = fn + "_lick"; fp_lick = fopen(fn_lick.c_str(), "wb"); std::cout << "DC1394: Recording lick detection, writing to " << fn_lick << std::endl; #else boost::filesystem::path path(fn); boost::filesystem::path writepath(path); // Test whether dir exists: if (!boost::filesystem::exists(writepath)) { std::cout << "DC1394: Creating directory " << writepath << std::endl; boost::filesystem::create_directories(writepath); } fn += "/"; /* check save frame buffer */ std::size_t nfb = save_frame_buffer.size(); if (nfb) std::cerr << "DC1394: Frame buffer isn't empty!" << std::endl; std::cout << "DC1394: Starting video, writing to " << fn << std::endl; connected = true; ncount = 0; #endif } #endif // #nstandalone #ifdef USE_SDL if (SDL_PollEvent(&event)) { #ifdef STANDALONE /* Any of these event types will end the program */ if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN || event.type == SDL_KEYUP) { std::cout << std::endl; std::cout << std::endl << "DC1394: Total number of frames was " << nframes << std::endl; std::cout << std::endl << "DC1394: Frame buffer: " << acq_frame_buffer.size() << " frames left" << std::endl; close(s); pthread_cancel(acq_thread); pthread_cancel(save_thread); return 0; } #endif // STANDALONE } surface->pixels = (void*)im.data; // SDL_CreateRGBSurfaceFrom((void*)IplImage(im).imageData, // IplImage(im).width, // IplImage(im).height, // IplImage(im).depth*IplImage(im).nChannels, // IplImage(im).widthStep, // 1, 1, 1, 0); screen = SDL_GetVideoSurface(); if(SDL_BlitSurface(surface, NULL, screen, NULL) == 0) SDL_UpdateRect(screen, 0, 0, 0, 0); #else // not SDL key = cv::waitKey(2); cv::imshow("DC1394", im); if (key == 1114155 || key == 65579 || key==43 /*+*/) { uint32_t gain = 0; err = dc1394_feature_get_value(gCamera.cam(), DC1394_FEATURE_GAIN, &gain); DC1394_ERR_CLN_RTN(err,cleanup_and_exit(gCamera),"Can't get gain"); if (gain < gCamera.get_maxgain()-10) { gain += 10; pthread_mutex_lock( &camera_mutex ); err = dc1394_feature_set_value(gCamera.cam(), DC1394_FEATURE_GAIN, gain); pthread_mutex_unlock( &camera_mutex ); std::cout << "DC1394: New gain value: " << gain << std::endl; DC1394_ERR_CLN_RTN(err,cleanup_and_exit(gCamera),"Can't set gain"); } } if (key == 1114207 || key == 45 /*-*/) { uint32_t gain = 0; err = dc1394_feature_get_value(gCamera.cam(), DC1394_FEATURE_GAIN, &gain); DC1394_ERR_CLN_RTN(err,cleanup_and_exit(gCamera),"Can't get gain"); if (gain > gCamera.get_mingain()+10) { gain -= 10; pthread_mutex_lock( &camera_mutex ); err = dc1394_feature_set_value(gCamera.cam(), DC1394_FEATURE_GAIN, gain); pthread_mutex_unlock( &camera_mutex ); DC1394_ERR_CLN_RTN(err,cleanup_and_exit(gCamera),"Can't set gain"); } } #endif // not SDL #ifdef LICKOMETER /* IS THIS ALL YOU NEED THEN? Lick detection */ /* Not required because the captured image is already gray cv::Mat gray = bgr2gray(im); */ gray = thresholding(im, LICK_FRAME_THRESHOLD); if (nloop != 0) { cv::absdiff(prevs, gray, thresh); double pixel_sum_thresh = cv::sum(thresh)[0]; double pixel_sum_gray = cv::sum(gray)[0]; if (pixel_sum_thresh > LICK_SUM_THRESHOLD) { std::cout << "DC1394: Lick" << std::endl; } if (fp_lick != NULL) { fwrite(&pixel_sum_thresh, sizeof(pixel_sum_thresh), 1, fp_lick); fwrite(&pixel_sum_gray, sizeof(pixel_sum_gray), 1, fp_lick); } } prevs = gray.clone(); nloop++; #endif #ifdef STANDALONE if (key == 1048689 || key == 113 /*q*/) { std::cout << "DC1394: Mean frame rate was " << nframes/total_dur << " fps" << std::endl; pthread_cancel(acq_thread); pthread_cancel(save_thread); return 0; } if (key == 1048691 /*s*/) { fn = ""; std::cout << "DC1394: Stopping video" << std::endl; ncount = 0; } if (key == 1048690 /*r*/) { fn = trunk + std::string("tmp/"); std::cout << "DC1394: Starting video, writing to " << fn << std::endl; ncount = 0; } #endif // #standalone clock_gettime( CLOCK_REALTIME, &time2); double loop_dur = tdiff(time2, time3); clock_gettime( CLOCK_REALTIME, &time3); double meanfps = 0; total_dur = tdiff(time3, time0); if (total_dur > 0) meanfps = nframes / total_dur; double currentfps = ret / loop_dur; std::cout << "DC1394: Current fps: " << std::setprecision(7) << currentfps << " Average fps: " << std::setprecision(7) << meanfps << "\r" << std::flush; #ifdef STANDALONE // std::cout << capture_dur << "\t" << target_dur << "\t" << rem << "\t" << loop_dur << std::endl; #endif } if (d) { dc1394_free(d); } #ifndef STANDALONE close(s); #endif return 0; }
int main(int argc, char *argv[]) { // parse configuration file // get input arguments OpenCvStereoConfig stereo_config; string config_file = ""; ConciseArgs parser(argc, argv); parser.add(config_file, "c", "config", "Configuration file containing camera GUIDs, etc.", true); parser.add(left_camera_mode, "l", "left-camera", "Calibrate just the left camera."); parser.add(right_camera_mode, "r", "right-camera", "Calibrate just the right camera."); parser.add(force_brightness, "b", "brightness", "set brightness to this level"); parser.add(force_exposure, "e", "exposure", "set exposure to this level"); parser.parse(); // parse the config file if (ParseConfigFile(config_file, &stereo_config) != true) { fprintf(stderr, "Failed to parse configuration file, quitting.\n"); return -1; } if (left_camera_mode || right_camera_mode) { stereo_mode = false; } uint64 guid = stereo_config.guidLeft; uint64 guid2 = stereo_config.guidRight; dc1394_t *d; dc1394camera_t *camera; dc1394error_t err; Mat frame_array_left[MAX_FRAMES]; Mat frame_array_right[MAX_FRAMES]; int numFrames = 0; // ----- cam 2 ----- dc1394_t *d2; dc1394camera_t *camera2; dc1394error_t err2; d = dc1394_new (); if (!d) g_critical("Could not create dc1394 context"); d2 = dc1394_new (); if (!d2) g_critical("Could not create dc1394 context for camera 2"); camera = dc1394_camera_new (d, guid); if (!camera) g_critical("Could not create dc1394 camera"); camera2 = dc1394_camera_new (d2, guid2); if (!camera2) g_critical("Could not create dc1394 camera for camera 2"); // setup err = setup_gray_capture(camera, DC1394_VIDEO_MODE_FORMAT7_1); DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera), "Could not setup camera"); err2 = setup_gray_capture(camera2, DC1394_VIDEO_MODE_FORMAT7_1); DC1394_ERR_CLN_RTN(err2, cleanup_and_exit(camera2), "Could not setup camera number 2"); // enable auto-exposure // turn on the auto exposure feature err = dc1394_feature_set_power(camera, DC1394_FEATURE_EXPOSURE, DC1394_ON); DC1394_ERR_RTN(err,"Could not turn on the exposure feature"); err = dc1394_feature_set_mode(camera, DC1394_FEATURE_EXPOSURE, DC1394_FEATURE_MODE_ONE_PUSH_AUTO); DC1394_ERR_RTN(err,"Could not turn on Auto-exposure"); // enable auto-exposure // turn on the auto exposure feature err = dc1394_feature_set_power(camera2, DC1394_FEATURE_EXPOSURE, DC1394_ON); DC1394_ERR_RTN(err,"Could not turn on the exposure feature for cam2"); err = dc1394_feature_set_mode(camera2, DC1394_FEATURE_EXPOSURE, DC1394_FEATURE_MODE_ONE_PUSH_AUTO); DC1394_ERR_RTN(err,"Could not turn on Auto-exposure for cam2"); // enable camera err = dc1394_video_set_transmission(camera, DC1394_ON); DC1394_ERR_CLN_RTN(err, cleanup_and_exit(camera), "Could not start camera iso transmission"); err2 = dc1394_video_set_transmission(camera2, DC1394_ON); DC1394_ERR_CLN_RTN(err2, cleanup_and_exit(camera2), "Could not start camera iso transmission for camera number 2"); if (left_camera_mode || stereo_mode) { InitBrightnessSettings(camera, camera2); MatchBrightnessSettings(camera, camera2, true, force_brightness, force_exposure); } else { // use the right camera as the master for brightness // since we're calibrating that one InitBrightnessSettings(camera2, camera); MatchBrightnessSettings(camera2, camera, true); } // make opencv windows if (left_camera_mode || stereo_mode) { namedWindow("Input Left", CV_WINDOW_AUTOSIZE); moveWindow("Input Left", 100, 100); } if (right_camera_mode || stereo_mode) { namedWindow("Input Right", CV_WINDOW_AUTOSIZE); moveWindow("Input Right", 478, 100); } CvSize size; Mat cornersL, cornersR; int i; while (numFrames < MAX_FRAMES) { Mat chessL, chessR; // each loop dump a bunch of frames to clear the buffer MatchBrightnessSettings(camera, camera2, true, force_brightness, force_exposure); for (i=0;i<10;i++) { if (left_camera_mode || stereo_mode) { chessL = GetFrameFormat7(camera); } if (right_camera_mode || stereo_mode) { chessR = GetFrameFormat7(camera2); } } // copy the images for drawing/display size = chessL.size(); Mat chessLc; chessLc.create(size, CV_32FC3); Mat chessRc; chessRc.create(size, CV_32FC3); // attempt checkerboard matching bool foundPattern = true; // set to true so we can do an OR // later if we're only using one // camera if (left_camera_mode || stereo_mode) { foundPattern = findChessboardCorners(chessL, Size(CHESS_X, CHESS_Y), cornersL); } if (right_camera_mode || stereo_mode) { foundPattern = foundPattern & findChessboardCorners(chessR, Size(CHESS_X, CHESS_Y), cornersR); } if (left_camera_mode || stereo_mode) { cvtColor( chessL, chessLc, CV_GRAY2BGR ); drawChessboardCorners(chessLc, Size(CHESS_X, CHESS_Y), cornersL, foundPattern); imshow("Input Left", chessLc); } if (right_camera_mode || stereo_mode) { cvtColor(chessR, chessRc, CV_GRAY2BGR); drawChessboardCorners(chessRc, Size(CHESS_X, CHESS_Y), cornersR, foundPattern); imshow("Input Right", chessRc); } // key codes: // page up: 654365 // page down: 65366 // b: 98 char key = waitKey(); //printf("%d\n", (int)key); if (key == 98) { break; } else if (key == 86){ if (foundPattern) { // this was a good one -- save it frame_array_left[numFrames] = chessL; frame_array_right[numFrames] = chessR; // give the user some guidence on the number // of frames they should be using if (stereo_mode) { printf("Saved frame %d / about 10\n", numFrames); } else { printf("Saved frame %d / about 20-30\n", numFrames); } numFrames ++; } else { printf("Not saving frame since did not find a checkboard.\n"); } } else if (key == 'W') { force_brightness +=20; cout << "Brightness: " << force_brightness << "\n"; } else if (key == 'w') { force_brightness -=20; cout << "Brightness: " << force_brightness << "\n"; } else if (key == 'E') { force_exposure +=20; cout << "Exposure: " << force_exposure << "\n"; } else if (key == 'e') { force_exposure -=20; cout << "Exposure: " << force_exposure << "\n"; } } printf("\n\n"); // clear out the calibration directory printf("Deleting old images...\nrm calibrationImages/*.ppm\n"); int retval = system("rm calibrationImages/*.ppm"); if (retval != 0) { printf("Warning: Deleting images may have failed.\n"); } printf("done.\n"); char filename[1000]; for (i=0;i<numFrames;i++) { if (left_camera_mode || stereo_mode) { sprintf(filename, "calibrationImages/cam1-%05d.ppm", i+1); imwrite(filename, frame_array_left[i]); } if (right_camera_mode || stereo_mode) { sprintf(filename, "calibrationImages/cam2-%05d.ppm", i+1); imwrite(filename, frame_array_right[i]); } printf("Writing frame %d\n", i); } printf("\n\n"); destroyWindow("Input Left"); destroyWindow("Input Right"); // stop data transmission err = dc1394_video_set_transmission(camera, DC1394_OFF); DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not stop the camera"); err2 = dc1394_video_set_transmission(camera2, DC1394_OFF); DC1394_ERR_CLN_RTN(err2,cleanup_and_exit(camera2),"Could not stop the camera 2"); // close camera cleanup_and_exit(camera); cleanup_and_exit(camera2); dc1394_free (d); dc1394_free (d2); return 0; }
//-------------------------------------------------------------------- int of1394VideoGrabber::initGrabber() { // moved from constructor int i; printf("Making new camera\n"); d = dc1394_new (); err=dc1394_camera_enumerate (d, &list); DC1394_ERR_RTN(err,"Failed to enumerate cameras"); if (list->num == 0) { if(err!=0 && bVerbose)printf("\n No cameras found\n "); return 1; } camera = dc1394_camera_new (d, list->ids[0].guid); if (!camera) { if(err!=0 && bVerbose)printf("\n Failed to initialize camera with guid %PRIx64", list->ids[0].guid); return 1; } dc1394_camera_free_list (list); /*----------------------------------------------------------------------- * setup capture *-----------------------------------------------------------------------*/ dc1394_video_set_mode(camera, DC1394_VIDEO_MODE_FORMAT7_0); uint32_t f, f2; dc1394_format7_get_image_size(camera, DC1394_VIDEO_MODE_FORMAT7_0, &f, &f2); err=dc1394_capture_setup(camera,4, DC1394_CAPTURE_FLAGS_DEFAULT); DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not setup camera-\nmake sure that the video mode and framerate are\nsupported by your camera\n"); /*----------------------------------------------------------------------- * 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\n"); dc1394_get_image_size_from_video_mode(camera, video_mode, &width, &height); if(err != DC1394_SUCCESS){ cout<<"get image size error: "<<err<<endl; } printf("Size from video mode = %i wide %i high\n", width, height); pixels = new unsigned char[width*height*3]; // x3 for RGB pixels2 = new unsigned char[width*height*3]; // x3 for RGB tex.allocate(width,height,GL_LUMINANCE); err = dc1394_capture_dequeue( camera, DC1394_CAPTURE_POLICY_WAIT, &frame ) ; DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not capture a frame\n"); memcpy( pixels, frame->image, width * height*3) ; dc1394_capture_enqueue( camera, frame ) ; ofSleepMillis(10); startThread(true, false); failedToInit = false; dc1394_feature_set_mode(camera, DC1394_FEATURE_EXPOSURE, DC1394_FEATURE_MODE_MANUAL); dc1394_feature_set_mode(camera, DC1394_FEATURE_BRIGHTNESS, DC1394_FEATURE_MODE_MANUAL); dc1394_feature_set_mode(camera, DC1394_FEATURE_GAMMA, DC1394_FEATURE_MODE_AUTO); dc1394_feature_set_mode(camera, DC1394_FEATURE_SHUTTER, DC1394_FEATURE_MODE_MANUAL); dc1394_feature_set_mode(camera, DC1394_FEATURE_GAIN, DC1394_FEATURE_MODE_AUTO); dc1394_feature_set_mode(camera, DC1394_FEATURE_IRIS, DC1394_FEATURE_MODE_AUTO); dc1394_feature_set_mode(camera, DC1394_FEATURE_WHITE_BALANCE, DC1394_FEATURE_MODE_AUTO); dc1394_feature_set_mode(camera, DC1394_FEATURE_TEMPERATURE, DC1394_FEATURE_MODE_AUTO); dc1394_feature_set_mode(camera, DC1394_FEATURE_WHITE_SHADING, DC1394_FEATURE_MODE_AUTO); dc1394_feature_set_mode(camera, DC1394_FEATURE_SATURATION, DC1394_FEATURE_MODE_AUTO); dc1394_feature_set_mode(camera, DC1394_FEATURE_HUE, DC1394_FEATURE_MODE_AUTO); //Print features dc1394_feature_get_all(camera, &features); dc1394_feature_print_all(&features, stdout); return 0; }
int main(int argc, const char * argv[]) { dc1394_t *d; dc1394camera_list_t *list; dc1394error_t err; dc1394camera_t *camera; dc1394format7modeset_t modeset; dc1394video_frame_t *frame; FILE* imagefile; char filename[256]; int i = 0; d = dc1394_new(); if (!d) { return 1; } err = dc1394_camera_enumerate(d, &list); DC1394_ERR_RTN(err, "Failed to enumerate cameras"); if (list->num == 0) { dc1394_log_error("No cameras found"); dc1394_free(d); return 1; } printf("Detected %d cameras\n", list->num); // Assume that Ladybug 5 is detected as camera #0 camera = dc1394_camera_new(d, list->ids[0].guid); if (!camera) { dc1394_log_error("Failed to initialize camera with guid %llx", list->ids[0].guid); dc1394_free(d); } dc1394_camera_free_list(list); printf("Using camera %s %s\n", camera->vendor, camera->model); // Report camera info err = dc1394_camera_print_info(camera, stdout); DC1394_ERR_RTN(err, "Could not print camera info"); // Setup video mode, etc... err = dc1394_video_set_operation_mode(camera, DC1394_OPERATION_MODE_1394B); DC1394_ERR_RTN(err, "Could not set B mode"); err = dc1394_video_set_iso_speed(camera, DC1394_ISO_SPEED_MAX); DC1394_ERR_RTN(err, "Could not set max speed"); err = dc1394_video_set_mode(camera, DC1394_VIDEO_MODE_FORMAT7_0); DC1394_ERR_RTN(err, "Could not set DC1394_VIDEO_MODE_FORMAT7_0"); // Get format7 mode info err = dc1394_format7_get_modeset(camera, &modeset); DC1394_ERR_RTN(err, "Could not get format 7 mode info\n"); print_format7_info(&modeset); // Set format 7 roi err = dc1394_format7_set_roi(camera, DC1394_VIDEO_MODE_FORMAT7_0, modeset.mode[0].color_coding, modeset.mode[0].max_packet_size, modeset.mode[0].pos_x, modeset.mode[0].pos_y, modeset.mode[0].max_size_x, modeset.mode[0].max_size_y); DC1394_ERR_RTN(err, "Could not set max ROI"); // Set capture err = dc1394_capture_setup(camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT); DC1394_ERR_RTN(err, "Could not setup capture"); err = dc1394_video_set_transmission(camera, DC1394_ON); DC1394_ERR_RTN(err, "Could not start transmission"); while (i < NFRAMES) { // Capture image printf("Capturing image %d\n", i); err = dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_WAIT, &frame); DC1394_ERR_RTN(err, "Could not dequeue a frame"); // Do something with the image print_frame_info(frame); // Save the image sprintf(filename, "%05d.pgm",i); imagefile = fopen(filename, "wb"); if ( imagefile == NULL ) { printf("Could not save image\n"); continue; } fprintf(imagefile, "P5\n%u %u 255\n", frame->size[0], frame->size[1]); fwrite(frame->image, 1, frame->image_bytes, imagefile); fclose(imagefile); printf("Saved image %s\n", filename); err = dc1394_capture_enqueue(camera, frame); DC1394_ERR_RTN(err, "Could enqueue a frame"); i++; } dc1394_camera_free(camera); dc1394_free(d); return 0; }
int main(int argc, char *argv[]) { fitsfile *fptr; long fpixel=1, nelements, naxes[2]; dc1394camera_t *camera; int grab_n_frames; struct timeval start_time, end_time; int i, j, status; unsigned int max_height, max_width; uint64_t total_bytes = 0; unsigned int width, height; dc1394video_frame_t *frame=NULL; dc1394_t * d; dc1394camera_list_t * list; dc1394error_t err; char *filename; unsigned char *buffer; float *average; grab_n_frames = atoi(argv[1]); filename = argv[2]; status = 0; width = 320; height = 240; naxes[0] = width; naxes[1] = height; nelements = naxes[0]*naxes[1]; stderr = freopen("grab_cube.log", "w", stderr); d = dc1394_new(); if (!d) return 1; err = dc1394_camera_enumerate(d, &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 (d, list->ids[0].guid); if (!camera) { dc1394_log_error("Failed to initialize camera with guid %"PRIx64, list->ids[0].guid); return 1; } dc1394_camera_free_list(list); printf("Using camera with GUID %"PRIx64"\n", camera->guid); /*----------------------------------------------------------------------- * setup capture for format 7 *-----------------------------------------------------------------------*/ // err=dc1394_video_set_operation_mode(camera, DC1394_OPERATION_MODE_1394B); // DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot operate at 1394B"); // libdc1394 doesn't work well with firewire800 yet so set to legacy 400 mode dc1394_video_set_iso_speed(camera, DC1394_ISO_SPEED_400); // configure camera for format7 err = dc1394_video_set_mode(camera, DC1394_VIDEO_MODE_FORMAT7_1); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot choose format7_0"); err = dc1394_format7_get_max_image_size (camera, DC1394_VIDEO_MODE_FORMAT7_1, &max_width, &max_height); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot get max image size for format7_0"); err = dc1394_format7_set_roi (camera, DC1394_VIDEO_MODE_FORMAT7_1, DC1394_COLOR_CODING_MONO8, // not sure why RAW8/16 don't work DC1394_USE_MAX_AVAIL, 0, 0, // left, top width, height); // width, height DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set roi"); // set the frame rate to absolute value in frames/sec err = dc1394_feature_set_mode(camera, DC1394_FEATURE_FRAME_RATE, DC1394_FEATURE_MODE_MANUAL); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set framerate to manual"); err = dc1394_feature_set_absolute_control(camera, DC1394_FEATURE_FRAME_RATE, DC1394_TRUE); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set framerate to absolute mode"); err = dc1394_feature_set_absolute_value(camera, DC1394_FEATURE_FRAME_RATE, 100.0); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set framerate"); // set the shutter speed to absolute value in seconds err = dc1394_feature_set_mode(camera, DC1394_FEATURE_SHUTTER, DC1394_FEATURE_MODE_MANUAL); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set shutter to manual"); err = dc1394_feature_set_absolute_control(camera, DC1394_FEATURE_SHUTTER, DC1394_TRUE); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set shutter to absolute mode"); err = dc1394_feature_set_absolute_value(camera, DC1394_FEATURE_SHUTTER, 1.0e-2); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set shutter"); // set gain manually. use relative value here in range 48 to 730. err = dc1394_feature_set_mode(camera, DC1394_FEATURE_GAIN, DC1394_FEATURE_MODE_MANUAL); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set gain to manual"); err = dc1394_feature_set_value(camera, DC1394_FEATURE_GAIN, 200); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set gain"); // set brightness manually. use relative value in range 0 to 1023. err = dc1394_feature_set_mode(camera, DC1394_FEATURE_BRIGHTNESS, DC1394_FEATURE_MODE_MANUAL); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set brightness to manual"); err = dc1394_feature_set_value(camera, DC1394_FEATURE_BRIGHTNESS, 50); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set brightness"); err = dc1394_format7_get_total_bytes (camera, DC1394_VIDEO_MODE_FORMAT7_1, &total_bytes); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot get total bytes"); // err = dc1394_feature_set_value (camera, DC1394_FEATURE_GAIN, 24); //DC1394_ERR_CLN_RTN(err, dc1394_camera_free(camera), "Error setting gain"); err = dc1394_capture_setup(camera, 16, DC1394_CAPTURE_FLAGS_DEFAULT); DC1394_ERR_CLN_RTN(err, dc1394_camera_free(camera), "Error capturing"); /*----------------------------------------------------------------------- * have the camera start sending us data *-----------------------------------------------------------------------*/ err = dc1394_video_set_transmission(camera, DC1394_ON); if (err != DC1394_SUCCESS) { dc1394_log_error("unable to start camera iso transmission"); dc1394_capture_stop(camera); dc1394_camera_free(camera); exit(1); } /* allocate the buffers */ if (!(buffer = malloc(nelements*sizeof(char)))) { printf("Couldn't Allocate Image Buffer\n"); exit(-1); } if (!(average = calloc(nelements, sizeof(float)))) { printf("Couldn't Allocate Average Image Buffer\n"); exit(-1); } // set up FITS image and capture fits_create_file(&fptr, filename, &status); dc1394_get_image_size_from_video_mode(camera, DC1394_VIDEO_MODE_FORMAT7_1, &width, &height); fits_create_img(fptr, FLOAT_IMG, 2, naxes, &status); /*----------------------------------------------------------------------- * capture frames and measure the time for this operation *-----------------------------------------------------------------------*/ gettimeofday(&start_time, NULL); printf("Start capture:\n"); for (i=0; i<grab_n_frames; ++i) { /*----------------------------------------------------------------------- * capture one frame *-----------------------------------------------------------------------*/ err = dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_WAIT, &frame); if (err != DC1394_SUCCESS) { dc1394_log_error("unable to capture"); dc1394_capture_stop(camera); dc1394_camera_free(camera); exit(1); } memcpy(buffer, frame->image, nelements*sizeof(char)); // release buffer dc1394_capture_enqueue(camera,frame); for (j=0; j<nelements; j++) { average[j] += (1.0/grab_n_frames)*(buffer[j]); } } gettimeofday(&end_time, NULL); printf("End capture.\n"); /*----------------------------------------------------------------------- * stop data transmission *-----------------------------------------------------------------------*/ err = dc1394_video_set_transmission(camera, DC1394_OFF); DC1394_ERR_RTN(err,"couldn't stop the camera?"); /*----------------------------------------------------------------------- * save FITS image to disk *-----------------------------------------------------------------------*/ fits_write_img(fptr, TFLOAT, fpixel, naxes[0]*naxes[1], average, &status); fits_close_file(fptr, &status); fits_report_error(stderr, status); free(buffer); free(average); printf("wrote: %s\n", filename); printf("Readout is %d bits/pixel.\n", frame->data_depth); /*----------------------------------------------------------------------- * close camera, cleanup *-----------------------------------------------------------------------*/ dc1394_capture_stop(camera); dc1394_video_set_transmission(camera, DC1394_OFF); dc1394_camera_free(camera); dc1394_free(d); return 0; }
int main (int argc, char **argv) { dc1394camera_t *camera = NULL; uint64_t guid = 0x0LL; dc1394_t * d; dc1394camera_list_t * list; dc1394error_t err; d = dc1394_new (); if (!d) return 1; err=dc1394_camera_enumerate (d, &list); DC1394_ERR_RTN(err,"Failed to enumerate cameras"); if (list->num == 0) { dc1394_log_error("No cameras found"); return 1; } /* parse arguments */ if (argc == 2) { if (!strcmp (argv[1], "--list-cameras")) list_cameras (d, list); else print_usage(); dc1394_camera_free_list (list); dc1394_free (d); return 0; } else if (argc == 3) { if (!strcmp (argv[1], "--guid")) { if (sscanf (argv[2], "0x%"PRIx64, &guid) == 1) { } else { dc1394_camera_free_list (list); dc1394_free (d); return print_usage(); } } } else if (argc != 1) { dc1394_camera_free_list (list); dc1394_free (d); return print_usage(); } if (guid == 0x0LL) { printf ("I: found %d camera%s, working with camera 0\n", list->num, list->num == 1 ? "" : "s"); camera = dc1394_camera_new (d, list->ids[0].guid); } else { camera = dc1394_camera_new (d, guid); if (camera == NULL) { dc1394_log_error("no camera with guid 0x%"PRIx64" found", guid); return 1; } printf ("I: found camera with guid 0x%"PRIx64"\n", guid); } dc1394_camera_print_info (camera, stdout); printf ("\nSFF feature info:\n"); dc1394_basler_sff_feature_print_all (camera, stdout); dc1394_camera_free (camera); dc1394_camera_free_list (list); dc1394_free (d); return 0; }
int main(int argc, char *argv[]) { fitsfile *fptr; long fpixel=1, nelements, naxes[3]; dc1394camera_t *camera; int grab_n_frames; struct timeval start_time, end_time; time_t start_sec, end_sec; suseconds_t start_usec, end_usec; float elapsed_time, fps; int i, status; unsigned int min_bytes, max_bytes, max_height, max_width; unsigned int actual_bytes; uint64_t total_bytes = 0; unsigned int width, height; dc1394video_frame_t *frame=NULL; dc1394_t * d; dc1394camera_list_t * list; dc1394error_t err; char *filename; grab_n_frames = atoi(argv[1]); filename = argv[2]; width = 320; height = 240; naxes[0] = width; naxes[1] = height; naxes[2] = grab_n_frames; nelements = naxes[0]*naxes[1]*naxes[2]; stderr = freopen("grab_cube.log", "w", stderr); d = dc1394_new (); if (!d) return 1; err=dc1394_camera_enumerate (d, &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 (d, list->ids[0].guid); if (!camera) { dc1394_log_error("Failed to initialize camera with guid %"PRIx64, list->ids[0].guid); return 1; } dc1394_camera_free_list (list); printf("Using camera with GUID %"PRIx64"\n", camera->guid); /*----------------------------------------------------------------------- * setup capture for format 7 *-----------------------------------------------------------------------*/ // err=dc1394_video_set_operation_mode(camera, DC1394_OPERATION_MODE_1394B); // DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot operate at 1394B"); // libdc1394 doesn't work well with firewire800 yet so set to legacy 400 mode dc1394_video_set_iso_speed(camera, DC1394_ISO_SPEED_400); // configure camera for format7 err = dc1394_video_set_mode(camera, DC1394_VIDEO_MODE_FORMAT7_1); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot choose format7_0"); printf ("I: video mode is format7_0\n"); err = dc1394_format7_get_max_image_size (camera, DC1394_VIDEO_MODE_FORMAT7_1, &max_width, &max_height); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot get max image size for format7_0"); printf ("I: max image size is: height = %d, width = %d\n", max_height, max_width); printf ("I: current image size is: height = %d, width = %d\n", height, width); err = dc1394_format7_set_roi (camera, DC1394_VIDEO_MODE_FORMAT7_1, DC1394_COLOR_CODING_MONO16, // not sure why RAW8/16 don't work DC1394_USE_MAX_AVAIL, 0, 0, // left, top width, height); // width, height DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set roi"); printf ("I: roi is (0, 0) - (%d, %d)\n", width, height); // set the frame rate to absolute value in frames/sec err = dc1394_feature_set_mode(camera, DC1394_FEATURE_FRAME_RATE, DC1394_FEATURE_MODE_MANUAL); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set framerate to manual"); err = dc1394_feature_set_absolute_control(camera, DC1394_FEATURE_FRAME_RATE, DC1394_TRUE); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set framerate to absolute mode"); err = dc1394_feature_set_absolute_value(camera, DC1394_FEATURE_FRAME_RATE, 330.0); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set framerate"); printf("I: framerate is %f fps\n", 330.0); // set the shutter speed to absolute value in seconds err = dc1394_feature_set_mode(camera, DC1394_FEATURE_SHUTTER, DC1394_FEATURE_MODE_MANUAL); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set shutter to manual"); err = dc1394_feature_set_absolute_control(camera, DC1394_FEATURE_SHUTTER, DC1394_TRUE); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set shutter to absolute mode"); err = dc1394_feature_set_absolute_value(camera, DC1394_FEATURE_SHUTTER, 3.0e-3); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set shutter"); printf("I: exptime is %f s\n", 3.0e-3); // set gain manually. use relative value here in range 48 to 730. err = dc1394_feature_set_mode(camera, DC1394_FEATURE_GAIN, DC1394_FEATURE_MODE_MANUAL); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set gain to manual"); err = dc1394_feature_set_value(camera, DC1394_FEATURE_GAIN, 400); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set gain"); printf ("I: gain is %d\n", 400); // set brightness manually. use relative value in range 0 to 1023. err = dc1394_feature_set_mode(camera, DC1394_FEATURE_BRIGHTNESS, DC1394_FEATURE_MODE_MANUAL); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set brightness to manual"); err = dc1394_feature_set_value(camera, DC1394_FEATURE_BRIGHTNESS, 100); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set brightness"); printf ("I: brightness is %d\n", 100); err = dc1394_format7_get_total_bytes (camera, DC1394_VIDEO_MODE_FORMAT7_1, &total_bytes); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot get total bytes"); printf ("I: total bytes is %"PRIu64" before SFF enabled\n", total_bytes); err=dc1394_capture_setup(camera, 16, DC1394_CAPTURE_FLAGS_DEFAULT); DC1394_ERR_CLN_RTN(err, dc1394_camera_free(camera), "Error capturing"); /*----------------------------------------------------------------------- * print allowed and used packet size *-----------------------------------------------------------------------*/ err=dc1394_format7_get_packet_parameters(camera, DC1394_VIDEO_MODE_FORMAT7_1, &min_bytes, &max_bytes); DC1394_ERR_RTN(err,"Packet para inq error"); printf( "camera reports allowed packet size from %d - %d bytes\n", min_bytes, max_bytes); err=dc1394_format7_get_packet_size(camera, DC1394_VIDEO_MODE_FORMAT7_1, &actual_bytes); DC1394_ERR_RTN(err,"dc1394_format7_get_packet_size error"); printf( "camera reports actual packet size = %d bytes\n", actual_bytes); err=dc1394_format7_get_total_bytes(camera, DC1394_VIDEO_MODE_FORMAT7_1, &total_bytes); DC1394_ERR_RTN(err,"dc1394_query_format7_total_bytes error"); printf( "camera reports total bytes per frame = %"PRId64" bytes\n", total_bytes); /*----------------------------------------------------------------------- * have the camera start sending us data *-----------------------------------------------------------------------*/ err=dc1394_video_set_transmission(camera,DC1394_ON); if (err!=DC1394_SUCCESS) { dc1394_log_error("unable to start camera iso transmission"); dc1394_capture_stop(camera); dc1394_camera_free(camera); exit(1); } // set up FITS image and capture fits_create_file(&fptr, filename, &status); dc1394_get_image_size_from_video_mode(camera, DC1394_VIDEO_MODE_FORMAT7_1, &width, &height); fits_create_img(fptr, USHORT_IMG, 3, naxes, &status); /*----------------------------------------------------------------------- * capture frames and measure the time for this operation *-----------------------------------------------------------------------*/ gettimeofday(&start_time, NULL); printf("Start capture:\n"); for( i = 0; i < grab_n_frames; ++i) { /*----------------------------------------------------------------------- * capture one frame *-----------------------------------------------------------------------*/ err=dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_WAIT, &frame); if (err!=DC1394_SUCCESS) { dc1394_log_error("unable to capture"); dc1394_capture_stop(camera); dc1394_camera_free(camera); exit(1); } // attempt to preallocate image array and write to memory before dumping to disk. // turns out to be slow due to large size of images. cfitsio buffering is far // more efficient. //memcpy(im_buffer+2*i*naxes[0]*naxes[1], //frame->image-1, //naxes[0]*naxes[1]*sizeof(short)); // just writing each frame to the FITS file goes pretty fast fits_write_img(fptr, TUSHORT, fpixel+i*naxes[0]*naxes[1], naxes[0]*naxes[1], frame->image-1, &status); // release buffer dc1394_capture_enqueue(camera,frame); } gettimeofday(&end_time, NULL); printf("End capture.\n"); /*----------------------------------------------------------------------- * stop data transmission *-----------------------------------------------------------------------*/ start_sec = start_time.tv_sec; start_usec = start_time.tv_usec; end_sec = end_time.tv_sec; end_usec = end_time.tv_usec; elapsed_time = (float)((end_sec + 1.0e-6*end_usec) - (start_sec + 1.0e-6*start_usec)); fps = grab_n_frames/elapsed_time; printf("Elapsed time = %g seconds.\n", elapsed_time); printf("Framerate = %g fps.\n", fps); err=dc1394_video_set_transmission(camera,DC1394_OFF); DC1394_ERR_RTN(err,"couldn't stop the camera?"); /*----------------------------------------------------------------------- * save FITS image to disk *-----------------------------------------------------------------------*/ //fits_write_img(fptr, TUSHORT, fpixel, naxes[0]*naxes[1]*naxes[2], im_buffer, &status); fits_close_file(fptr, &status); fits_report_error(stderr, status); //free(im_buffer); printf("wrote: %s\n", filename); printf("Image is %d bits/pixel.\n", frame->data_depth); /*----------------------------------------------------------------------- * close camera, cleanup *-----------------------------------------------------------------------*/ dc1394_capture_stop(camera); dc1394_video_set_transmission(camera, DC1394_OFF); dc1394_camera_free(camera); dc1394_free (d); return 0; }
int main(int argc, char *argv[]) { // assumes your camera can output 640x480 with 8-bit monochrome video_mode = DC1394_VIDEO_MODE_640x480_MONO8; d = dc1394_new(); if (!d) return 1; err=dc1394_camera_enumerate (d, &list); DC1394_ERR_RTN(err,"Failed to enumerate cameras"); if (list->num == 0) { dc1394_log_error("No cameras found"); return 1; } // use two counters so tht cameras array does not contain gaps in the case of errors j = 0; for (i = 0; i < list->num; i++) { if (j >= MAX_CAMERAS) break; cameras[j] = dc1394_camera_new (d, list->ids[i].guid); if (!cameras[j]) { dc1394_log_warning("Failed to initialize camera with guid %llx", list->ids[i].guid); continue; } j++; } numCameras = j; dc1394_camera_free_list (list); if (numCameras == 0) { dc1394_log_error("No cameras found"); exit (1); } // setup cameras for capture for (i = 0; i < numCameras; i++) { err=dc1394_video_set_iso_speed(cameras[i], DC1394_ISO_SPEED_800); DC1394_ERR_CLN_RTN(err,cleanup(),"Could not set ISO speed"); err=dc1394_video_set_mode(cameras[i], video_mode); DC1394_ERR_CLN_RTN(err,cleanup(),"Could not set video mode"); err=dc1394_capture_setup(cameras[i],NUM_BUFFERS, DC1394_CAPTURE_FLAGS_DEFAULT); DC1394_ERR_CLN_RTN(err,cleanup(),"Could not setup camera-\nmake sure that the video mode and framerate are\nsupported by your camera"); err=dc1394_get_image_size_from_video_mode(cameras[i], video_mode, &video_mode_width, &video_mode_height); DC1394_ERR_CLN_RTN(err,cleanup(),"Could not query video mode width and height"); err=dc1394_video_set_one_shot(cameras[i], DC1394_ON); DC1394_ERR_CLN_RTN(err,cleanup(),"Could not use one shot mode"); } fflush(stdout); if (numCameras < 1) { perror("no cameras found :(\n"); cleanup(); exit(-1); } for (i = 0; i < numCameras; i++) { if (dc1394_capture_dequeue(cameras[i], DC1394_CAPTURE_POLICY_WAIT, &frames[i])!=DC1394_SUCCESS) dc1394_log_error("Failed to capture from camera %d", i); // save image as '[GUID].pgm' char filename[256]; sprintf(filename, "%" PRIu64 "%s",list->ids[i].guid,IMAGE_FILE_EXTENSION); imagefile=fopen(filename, "w"); if( imagefile == NULL) { dc1394_log_error("Can't create %s", filename); } // adding the pgm file header fprintf(imagefile,"P5\n%u %u 255\n", video_mode_width, video_mode_height); // writing to the file fwrite((const char *)frames[i]->image, 1, \ video_mode_width * video_mode_height, imagefile); fclose(imagefile); } // exit cleanly cleanup(); return(0); }