static void gst_aravis_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec) { GstAravis *gst_aravis = GST_ARAVIS (object); switch (prop_id) { case PROP_CAMERA_NAME: g_free (gst_aravis->camera_name); /* check if we are currently active prevent setting camera and other values to something not representing the active camera */ if (gst_aravis->stream == NULL) { gst_aravis->camera_name = g_strdup (g_value_get_string (value)); gst_aravis_init_camera (gst_aravis); } GST_LOG_OBJECT (gst_aravis, "Set camera name to %s", gst_aravis->camera_name); break; case PROP_GAIN: gst_aravis->gain = g_value_get_double (value); if (gst_aravis->camera != NULL) arv_camera_set_gain (gst_aravis->camera, gst_aravis->gain); break; case PROP_GAIN_AUTO: gst_aravis->gain_auto = g_value_get_boolean (value); if (gst_aravis->camera != NULL) arv_camera_set_gain_auto (gst_aravis->camera, gst_aravis->gain_auto); break; case PROP_EXPOSURE: gst_aravis->exposure_time_us = g_value_get_double (value); if (gst_aravis->camera != NULL) arv_camera_set_exposure_time (gst_aravis->camera, gst_aravis->exposure_time_us); break; case PROP_EXPOSURE_AUTO: gst_aravis->exposure_auto = g_value_get_boolean (value); if (gst_aravis->camera != NULL) arv_camera_set_exposure_time_auto (gst_aravis->camera, gst_aravis->exposure_auto); break; case PROP_OFFSET_X: gst_aravis->offset_x = g_value_get_int (value); break; case PROP_OFFSET_Y: gst_aravis->offset_y = g_value_get_int (value); break; case PROP_H_BINNING: gst_aravis->h_binning = g_value_get_int (value); break; case PROP_V_BINNING: gst_aravis->v_binning = g_value_get_int (value); break; case PROP_PACKET_RESEND: gst_aravis->packet_resend = g_value_get_boolean (value); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; } }
CamGigE::CamGigE(int id): Cam(id) { mWidth = XSIZE; mHeight = YSIZE; xshift = 0; yshift = 0; exposure = 2*1000; gain = 300; isCapturing = false; framerate = 15.0f; mId = id; g_type_init (); camera = arv_camera_new(NULL); if(camera == NULL) { throw "No camera found"; } arv_camera_set_region(camera, xshift, yshift, mWidth, mHeight); arv_camera_set_exposure_time(camera, exposure); arv_camera_set_gain(camera, gain); arv_camera_set_pixel_format(camera, ARV_PIXEL_FORMAT_MONO_8); //arv_camera_set_pixel_format(camera, ARV_PIXEL_FORMAT_BAYER_BG_8); width = (int)mWidth; height = (int)mHeight; arv_camera_get_region (camera, &xshift, &yshift, &width, &height); payload = arv_camera_get_payload (camera); stream = arv_camera_create_stream (camera, NULL, NULL); if(stream == NULL) { throw "Cannot create stream"; } g_object_set(stream, "packet-timeout", (unsigned) 20 * 1000, "frame-retention", (unsigned) 100 * 1000, NULL); for(int i = 0; i < 4; i++) arv_stream_push_buffer(stream, arv_buffer_new(payload, NULL)); arv_camera_set_acquisition_mode(camera, ARV_ACQUISITION_MODE_CONTINUOUS); arv_camera_set_frame_rate(camera, framerate); rawdata = new unsigned char[width*height]; }
bool CvCaptureCAM_Aravis::setProperty( int property_id, double value ) { switch ( property_id ) { case CV_CAP_PROP_EXPOSURE: if(exposureAvailable) { /* exposure time in seconds, like 1/100 s */ value *= 1e6; // -> from s to us arv_camera_set_exposure_time(camera, exposure = BETWEEN(value, exposureMin, exposureMax)); break; } else return false; case CV_CAP_PROP_FPS: if(fpsAvailable) { arv_camera_set_frame_rate(camera, fps = BETWEEN(value, fpsMin, fpsMax)); break; } else return false; case CV_CAP_PROP_GAIN: if(gainAvailable) { arv_camera_set_gain(camera, gain = BETWEEN(value, gainMin, gainMax)); break; } else return false; case CV_CAP_PROP_FOURCC: { ArvPixelFormat newFormat = pixelFormat; switch((int)value) { case MODE_GRAY8: newFormat = ARV_PIXEL_FORMAT_MONO_8; break; case MODE_GRAY12: newFormat = ARV_PIXEL_FORMAT_MONO_12; break; } if(newFormat != pixelFormat) { stopCapture(); arv_camera_set_pixel_format(camera, pixelFormat = newFormat); startCapture(); } } break; default: return false; } return true; }
static gboolean gst_aravis_set_caps (GstBaseSrc *src, GstCaps *caps) { GstAravis* gst_aravis = GST_ARAVIS(src); GstStructure *structure; ArvPixelFormat pixel_format; int height, width; int bpp, depth; const GValue *frame_rate; const char *caps_string; unsigned int i; guint32 fourcc; GST_LOG_OBJECT (gst_aravis, "Requested caps = %" GST_PTR_FORMAT, caps); arv_camera_stop_acquisition (gst_aravis->camera); if (gst_aravis->stream != NULL) g_object_unref (gst_aravis->stream); structure = gst_caps_get_structure (caps, 0); gst_structure_get_int (structure, "width", &width); gst_structure_get_int (structure, "height", &height); frame_rate = gst_structure_get_value (structure, "framerate"); gst_structure_get_int (structure, "bpp", &bpp); gst_structure_get_int (structure, "depth", &depth); if (gst_structure_get_field_type (structure, "format") == G_TYPE_STRING) { const char *string; string = gst_structure_get_string (structure, "format"); fourcc = GST_STR_FOURCC (string); } else if (gst_structure_get_field_type (structure, "format") == GST_TYPE_FOURCC) { gst_structure_get_fourcc (structure, "format", &fourcc); } else fourcc = 0; pixel_format = arv_pixel_format_from_gst_0_10_caps (gst_structure_get_name (structure), bpp, depth, fourcc); arv_camera_set_region (gst_aravis->camera, gst_aravis->offset_x, gst_aravis->offset_y, width, height); arv_camera_set_binning (gst_aravis->camera, gst_aravis->h_binning, gst_aravis->v_binning); arv_camera_set_pixel_format (gst_aravis->camera, pixel_format); if (frame_rate != NULL) { double dbl_frame_rate; dbl_frame_rate = (double) gst_value_get_fraction_numerator (frame_rate) / (double) gst_value_get_fraction_denominator (frame_rate); GST_DEBUG_OBJECT (gst_aravis, "Frame rate = %g Hz", dbl_frame_rate); arv_camera_set_frame_rate (gst_aravis->camera, dbl_frame_rate); if (dbl_frame_rate > 0.0) gst_aravis->buffer_timeout_us = MAX (GST_ARAVIS_BUFFER_TIMEOUT_DEFAULT, 3e6 / dbl_frame_rate); else gst_aravis->buffer_timeout_us = GST_ARAVIS_BUFFER_TIMEOUT_DEFAULT; } else gst_aravis->buffer_timeout_us = GST_ARAVIS_BUFFER_TIMEOUT_DEFAULT; GST_DEBUG_OBJECT (gst_aravis, "Buffer timeout = %" G_GUINT64_FORMAT " µs", gst_aravis->buffer_timeout_us); GST_DEBUG_OBJECT (gst_aravis, "Actual frame rate = %g Hz", arv_camera_get_frame_rate (gst_aravis->camera)); if(gst_aravis->gain_auto) { arv_camera_set_gain_auto (gst_aravis->camera, ARV_AUTO_CONTINUOUS); GST_DEBUG_OBJECT (gst_aravis, "Auto Gain = continuous"); } else { if (gst_aravis->gain >= 0) { GST_DEBUG_OBJECT (gst_aravis, "Gain = %g", gst_aravis->gain); arv_camera_set_gain_auto (gst_aravis->camera, ARV_AUTO_OFF); arv_camera_set_gain (gst_aravis->camera, gst_aravis->gain); } GST_DEBUG_OBJECT (gst_aravis, "Actual gain = %g", arv_camera_get_gain (gst_aravis->camera)); } if(gst_aravis->exposure_auto) { arv_camera_set_exposure_time_auto (gst_aravis->camera, ARV_AUTO_CONTINUOUS); GST_DEBUG_OBJECT (gst_aravis, "Auto Exposure = continuous"); } else { if (gst_aravis->exposure_time_us > 0.0) { GST_DEBUG_OBJECT (gst_aravis, "Exposure = %g µs", gst_aravis->exposure_time_us); arv_camera_set_exposure_time_auto (gst_aravis->camera, ARV_AUTO_OFF); arv_camera_set_exposure_time (gst_aravis->camera, gst_aravis->exposure_time_us); } GST_DEBUG_OBJECT (gst_aravis, "Actual exposure = %g µs", arv_camera_get_exposure_time (gst_aravis->camera)); } if (gst_aravis->fixed_caps != NULL) gst_caps_unref (gst_aravis->fixed_caps); caps_string = arv_pixel_format_to_gst_0_10_caps_string (pixel_format); if (caps_string != NULL) { GstStructure *structure; GstCaps *caps; caps = gst_caps_new_empty (); structure = gst_structure_from_string (caps_string, NULL); gst_structure_set (structure, "width", G_TYPE_INT, width, "height", G_TYPE_INT, height, NULL); if (frame_rate != NULL) gst_structure_set_value (structure, "framerate", frame_rate); gst_caps_append_structure (caps, structure); gst_aravis->fixed_caps = caps; } else gst_aravis->fixed_caps = NULL; gst_aravis->payload = arv_camera_get_payload (gst_aravis->camera); gst_aravis->stream = arv_camera_create_stream (gst_aravis->camera, NULL, NULL); if (ARV_IS_GV_STREAM (gst_aravis->stream) && gst_aravis->packet_resend) g_object_set (gst_aravis->stream, "packet-resend", ARV_GV_STREAM_PACKET_RESEND_ALWAYS, NULL); else g_object_set (gst_aravis->stream, "packet-resend", ARV_GV_STREAM_PACKET_RESEND_NEVER, NULL); for (i = 0; i < GST_ARAVIS_N_BUFFERS; i++) arv_stream_push_buffer (gst_aravis->stream, arv_buffer_new (gst_aravis->payload, NULL)); GST_LOG_OBJECT (gst_aravis, "Start acquisition"); arv_camera_start_acquisition (gst_aravis->camera); gst_aravis->timestamp_offset = 0; gst_aravis->last_timestamp = 0; return TRUE; }
bool CvCaptureCAM_Aravis::setProperty( int property_id, double value ) { switch(property_id) { case CV_CAP_PROP_AUTO_EXPOSURE: if(exposureAvailable || gainAvailable) { if( (controlExposure = (bool)(int)value) ) { exposure = exposureAvailable ? arv_camera_get_exposure_time(camera) : 0; gain = gainAvailable ? arv_camera_get_gain(camera) : 0; } } break; case CV_CAP_PROP_BRIGHTNESS: exposureCompensation = CLIP(value, -3., 3.); break; case CV_CAP_PROP_EXPOSURE: if(exposureAvailable) { /* exposure time in seconds, like 1/100 s */ value *= 1e6; // -> from s to us arv_camera_set_exposure_time(camera, exposure = CLIP(value, exposureMin, exposureMax)); break; } else return false; case CV_CAP_PROP_FPS: if(fpsAvailable) { arv_camera_set_frame_rate(camera, fps = CLIP(value, fpsMin, fpsMax)); break; } else return false; case CV_CAP_PROP_GAIN: if(gainAvailable) { if ( (autoGain = (-1 == value) ) ) break; arv_camera_set_gain(camera, gain = CLIP(value, gainMin, gainMax)); break; } else return false; case CV_CAP_PROP_FOURCC: { ArvPixelFormat newFormat = pixelFormat; switch((int)value) { case MODE_GREY: case MODE_Y800: newFormat = ARV_PIXEL_FORMAT_MONO_8; targetGrey = 128; break; case MODE_Y12: newFormat = ARV_PIXEL_FORMAT_MONO_12; targetGrey = 2048; break; case MODE_Y16: newFormat = ARV_PIXEL_FORMAT_MONO_16; targetGrey = 32768; break; case MODE_GRBG: newFormat = ARV_PIXEL_FORMAT_BAYER_GR_8; targetGrey = 128; break; } if(newFormat != pixelFormat) { stopCapture(); arv_camera_set_pixel_format(camera, pixelFormat = newFormat); startCapture(); } } break; case CV_CAP_PROP_BUFFERSIZE: { int x = (int)value; if((x > 0) && (x != num_buffers)) { stopCapture(); num_buffers = x; startCapture(); } } break; default: return false; } return true; }
void CvCaptureCAM_Aravis::autoExposureControl(IplImage* image) { // Software control of exposure parameters utilizing // automatic change of exposure time & gain // Priority is set as follows: // - to increase brightness, first increase time then gain // - to decrease brightness, first decrease gain then time cv::Mat m = cv::cvarrToMat(image); // calc mean value for luminance or green channel double brightness = cv::mean(m)[image->nChannels > 1 ? 1 : 0]; if(brightness < 1) brightness = 1; // mid point - 100 % means no change static const double dmid = 100; // distance from optimal value as a percentage double d = (targetGrey * dmid) / brightness; if(d >= dmid) d = ( d + (dmid * 2) ) / 3; prevFrameID = frameID; midGrey = brightness; double maxe = 1e6 / fps; double ne = CLIP( ( exposure * d ) / ( dmid * pow(sqrt(2), -2 * exposureCompensation) ), exposureMin, maxe); // if change of value requires intervention if(std::fabs(d-dmid) > 5) { double ev, ng = 0; if(gainAvailable && autoGain) { ev = log( d / dmid ) / log(2); ng = CLIP( gain + ev + exposureCompensation, gainMin, gainMax); if( ng < gain ) { // priority 1 - reduce gain arv_camera_set_gain(camera, (gain = ng)); return; } } if(exposureAvailable) { // priority 2 - control of exposure time if(std::fabs(exposure - ne) > 2) { // we have not yet reach the max-e level arv_camera_set_exposure_time(camera, (exposure = ne) ); return; } } if(gainAvailable && autoGain) { if(exposureAvailable) { // exposure at maximum - increase gain if possible if(ng > gain && ng < gainMax && ne >= maxe) { arv_camera_set_gain(camera, (gain = ng)); return; } } else { // priority 3 - increase gain arv_camera_set_gain(camera, (gain = ng)); return; } } } // if gain can be reduced - do it if(gainAvailable && autoGain && exposureAvailable) { if(gain > gainMin && exposure < maxe) { exposure = CLIP( ne * 1.05, exposureMin, maxe); arv_camera_set_exposure_time(camera, exposure ); } } }
static gboolean gst_aravis_set_caps (GstBaseSrc *src, GstCaps *caps) { GstAravis* gst_aravis = GST_ARAVIS(src); GstStructure *structure; ArvPixelFormat pixel_format; int height, width; int bpp, depth; const GValue *frame_rate; const char *caps_string; unsigned int i; guint32 fourcc; GST_LOG_OBJECT (gst_aravis, "Requested caps = %" GST_PTR_FORMAT, caps); arv_camera_stop_acquisition (gst_aravis->camera); if (gst_aravis->stream != NULL) g_object_unref (gst_aravis->stream); structure = gst_caps_get_structure (caps, 0); gst_structure_get_int (structure, "width", &width); gst_structure_get_int (structure, "height", &height); frame_rate = gst_structure_get_value (structure, "framerate"); gst_structure_get_fourcc (structure, "format", &fourcc); gst_structure_get_int (structure, "bpp", &bpp); gst_structure_get_int (structure, "depth", &depth); pixel_format = arv_pixel_format_from_gst_caps (gst_structure_get_name (structure), bpp, depth, fourcc); arv_camera_set_region (gst_aravis->camera, 0, 0, width, height); arv_camera_set_binning (gst_aravis->camera, gst_aravis->h_binning, gst_aravis->v_binning); arv_camera_set_pixel_format (gst_aravis->camera, pixel_format); if (frame_rate != NULL) { double dbl_frame_rate; dbl_frame_rate = (double) gst_value_get_fraction_numerator (frame_rate) / (double) gst_value_get_fraction_denominator (frame_rate); GST_DEBUG_OBJECT (gst_aravis, "Frame rate = %g Hz", dbl_frame_rate); arv_camera_set_frame_rate (gst_aravis->camera, dbl_frame_rate); if (dbl_frame_rate > 0.0) gst_aravis->buffer_timeout_us = MAX (GST_ARAVIS_BUFFER_TIMEOUT_DEFAULT, 3e6 / dbl_frame_rate); else gst_aravis->buffer_timeout_us = GST_ARAVIS_BUFFER_TIMEOUT_DEFAULT; } else gst_aravis->buffer_timeout_us = GST_ARAVIS_BUFFER_TIMEOUT_DEFAULT; GST_DEBUG_OBJECT (gst_aravis, "Buffer timeout = %Ld µs", gst_aravis->buffer_timeout_us); GST_DEBUG_OBJECT (gst_aravis, "Actual frame rate = %g Hz", arv_camera_get_frame_rate (gst_aravis->camera)); GST_DEBUG_OBJECT (gst_aravis, "Gain = %d", gst_aravis->gain); arv_camera_set_gain (gst_aravis->camera, gst_aravis->gain); GST_DEBUG_OBJECT (gst_aravis, "Actual gain = %d", arv_camera_get_gain (gst_aravis->camera)); GST_DEBUG_OBJECT (gst_aravis, "Exposure = %g µs", gst_aravis->exposure_time_us); arv_camera_set_exposure_time (gst_aravis->camera, gst_aravis->exposure_time_us); GST_DEBUG_OBJECT (gst_aravis, "Actual exposure = %g µs", arv_camera_get_exposure_time (gst_aravis->camera)); if (gst_aravis->fixed_caps != NULL) gst_caps_unref (gst_aravis->fixed_caps); caps_string = arv_pixel_format_to_gst_caps_string (pixel_format); if (caps_string != NULL) { GstStructure *structure; GstCaps *caps; caps = gst_caps_new_empty (); structure = gst_structure_from_string (caps_string, NULL); gst_structure_set (structure, "width", G_TYPE_INT, width, "height", G_TYPE_INT, height, NULL); if (frame_rate != NULL) gst_structure_set_value (structure, "framerate", frame_rate); gst_caps_append_structure (caps, structure); gst_aravis->fixed_caps = caps; } else gst_aravis->fixed_caps = NULL; gst_aravis->payload = arv_camera_get_payload (gst_aravis->camera); gst_aravis->stream = arv_camera_create_stream (gst_aravis->camera, NULL, NULL); for (i = 0; i < GST_ARAVIS_N_BUFFERS; i++) arv_stream_push_buffer (gst_aravis->stream, arv_buffer_new (gst_aravis->payload, NULL)); GST_LOG_OBJECT (gst_aravis, "Start acquisition"); arv_camera_start_acquisition (gst_aravis->camera); gst_aravis->timestamp_offset = 0; gst_aravis->last_timestamp = 0; return TRUE; }