2016-10-08 03:55:49 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Intel License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2000, Intel Corporation, all rights reserved.
|
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of Intel Corporation may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
// The code has been contributed by Arkadiusz Raj on 2016 Oct
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "precomp.hpp"
|
|
|
|
|
|
|
|
#ifdef HAVE_ARAVIS_API
|
|
|
|
|
|
|
|
#include <arv.h>
|
|
|
|
|
2016-10-17 00:14:58 +08:00
|
|
|
//
|
|
|
|
// This file provides wrapper for using Aravis SDK library to access GigE Vision cameras.
|
2016-10-23 00:07:55 +08:00
|
|
|
// Aravis library (version 0.4 or 0.6) shall be installed else this code will not be included in build.
|
|
|
|
//
|
2016-10-17 00:14:58 +08:00
|
|
|
// To include this module invoke cmake with -DWITH_ARAVIS=ON
|
|
|
|
//
|
|
|
|
// Please obvserve, that jumbo frames are required when high fps & 16bit data is selected.
|
|
|
|
// (camera, switches/routers and the computer this software is running on)
|
|
|
|
//
|
|
|
|
// Basic usage: VideoCapture cap(CAP_ARAVIS + <camera id>);
|
|
|
|
//
|
|
|
|
// Supported properties:
|
2016-10-23 00:07:55 +08:00
|
|
|
// read/write
|
2016-10-17 00:14:58 +08:00
|
|
|
// CAP_PROP_AUTO_EXPOSURE(0|1)
|
|
|
|
// CAP_PROP_EXPOSURE(t), t in seconds
|
2017-05-13 04:46:46 +08:00
|
|
|
// CAP_PROP_BRIGHTNESS (ev), exposure compensation in EV for auto exposure algorithm
|
2016-10-17 00:14:58 +08:00
|
|
|
// CAP_PROP_GAIN(g), g >=0 or -1 for automatic control if CAP_PROP_AUTO_EXPOSURE is true
|
|
|
|
// CAP_PROP_FPS(f)
|
|
|
|
// CAP_PROP_FOURCC(type)
|
2016-10-23 00:07:55 +08:00
|
|
|
// CAP_PROP_BUFFERSIZE(n)
|
|
|
|
// read only:
|
|
|
|
// CAP_PROP_POS_MSEC
|
|
|
|
// CAP_PROP_FRAME_WIDTH
|
|
|
|
// CAP_PROP_FRAME_HEIGHT
|
2016-10-17 00:14:58 +08:00
|
|
|
//
|
2016-10-09 22:58:30 +08:00
|
|
|
// Supported types of data:
|
2016-10-17 00:14:58 +08:00
|
|
|
// video/x-raw, fourcc:'GREY' -> 8bit, 1 channel
|
|
|
|
// video/x-raw, fourcc:'Y800' -> 8bit, 1 channel
|
2016-10-09 22:58:30 +08:00
|
|
|
// video/x-raw, fourcc:'Y12 ' -> 12bit, 1 channel
|
2017-05-13 04:46:46 +08:00
|
|
|
// video/x-raw, fourcc:'Y16 ' -> 16bit, 1 channel
|
2016-10-17 00:14:58 +08:00
|
|
|
//
|
2016-10-09 22:58:30 +08:00
|
|
|
|
2016-10-17 00:14:58 +08:00
|
|
|
#define MODE_GREY CV_FOURCC_MACRO('G','R','E','Y')
|
|
|
|
#define MODE_Y800 CV_FOURCC_MACRO('Y','8','0','0')
|
|
|
|
#define MODE_Y12 CV_FOURCC_MACRO('Y','1','2',' ')
|
2017-05-13 04:46:46 +08:00
|
|
|
#define MODE_Y16 CV_FOURCC_MACRO('Y','1','6',' ')
|
2016-10-09 22:58:30 +08:00
|
|
|
|
2016-10-23 00:07:55 +08:00
|
|
|
#define CLIP(a,b,c) (cv::max(cv::min((a),(c)),(b)))
|
2016-10-08 19:01:01 +08:00
|
|
|
|
2016-10-08 03:55:49 +08:00
|
|
|
/********************* Capturing video from camera via Aravis *********************/
|
|
|
|
|
|
|
|
class CvCaptureCAM_Aravis : public CvCapture
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CvCaptureCAM_Aravis();
|
|
|
|
virtual ~CvCaptureCAM_Aravis()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool open(int);
|
|
|
|
virtual void close();
|
|
|
|
virtual double getProperty(int) const;
|
|
|
|
virtual bool setProperty(int, double);
|
|
|
|
virtual bool grabFrame();
|
|
|
|
virtual IplImage* retrieveFrame(int);
|
|
|
|
virtual int getCaptureDomain()
|
|
|
|
{
|
|
|
|
return CV_CAP_ARAVIS;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool create(int);
|
2016-10-09 22:58:30 +08:00
|
|
|
bool init_buffers();
|
2016-10-08 03:55:49 +08:00
|
|
|
|
|
|
|
void stopCapture();
|
|
|
|
bool startCapture();
|
|
|
|
|
|
|
|
bool getDeviceNameById(int id, std::string &device);
|
|
|
|
|
2016-10-17 00:14:58 +08:00
|
|
|
void autoExposureControl(IplImage*);
|
|
|
|
|
2016-10-08 03:55:49 +08:00
|
|
|
ArvCamera *camera; // Camera to control.
|
|
|
|
ArvStream *stream; // Object for video stream reception.
|
|
|
|
void *framebuffer; //
|
|
|
|
|
|
|
|
unsigned int payload; // Width x height x Pixel width.
|
|
|
|
|
|
|
|
int widthMin; // Camera sensor minium width.
|
|
|
|
int widthMax; // Camera sensor maximum width.
|
|
|
|
int heightMin; // Camera sensor minium height.
|
|
|
|
int heightMax; // Camera sensor maximum height.
|
2016-10-09 22:58:30 +08:00
|
|
|
bool fpsAvailable;
|
2016-10-08 03:55:49 +08:00
|
|
|
double fpsMin; // Camera minium fps.
|
|
|
|
double fpsMax; // Camera maximum fps.
|
2016-10-09 22:58:30 +08:00
|
|
|
bool gainAvailable;
|
2016-10-08 03:55:49 +08:00
|
|
|
double gainMin; // Camera minimum gain.
|
|
|
|
double gainMax; // Camera maximum gain.
|
2016-10-09 22:58:30 +08:00
|
|
|
bool exposureAvailable;
|
2016-10-08 03:55:49 +08:00
|
|
|
double exposureMin; // Camera's minimum exposure time.
|
|
|
|
double exposureMax; // Camera's maximum exposure time.
|
2016-10-17 00:14:58 +08:00
|
|
|
|
|
|
|
bool controlExposure; // Flag if automatic exposure shall be done by this SW
|
2017-05-13 04:46:46 +08:00
|
|
|
double exposureCompensation;
|
2016-10-17 00:14:58 +08:00
|
|
|
bool autoGain;
|
|
|
|
double targetGrey; // Target grey value (mid grey))
|
|
|
|
|
2016-10-09 22:58:30 +08:00
|
|
|
gint64 *pixelFormats;
|
|
|
|
guint pixelFormatsCnt;
|
|
|
|
|
2016-10-08 03:55:49 +08:00
|
|
|
|
|
|
|
int num_buffers; // number of payload transmission buffers
|
2016-10-08 19:01:01 +08:00
|
|
|
|
2016-10-23 00:07:55 +08:00
|
|
|
ArvPixelFormat pixelFormat; // pixel format
|
|
|
|
|
|
|
|
int xoffset; // current frame region x offset
|
|
|
|
int yoffset; // current frame region y offset
|
|
|
|
int width; // current frame width of frame
|
|
|
|
int height; // current frame height of image
|
|
|
|
|
|
|
|
double fps; // current value of fps
|
2016-10-09 22:58:30 +08:00
|
|
|
double exposure; // current value of exposure time
|
|
|
|
double gain; // current value of gain
|
2016-10-23 00:07:55 +08:00
|
|
|
double midGrey; // current value of mid grey (brightness)
|
|
|
|
|
|
|
|
unsigned frameID; // current frame id
|
|
|
|
unsigned prevFrameID;
|
2016-10-08 03:55:49 +08:00
|
|
|
|
2016-10-09 22:58:30 +08:00
|
|
|
IplImage *frame; // local frame copy
|
2016-10-08 03:55:49 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CvCaptureCAM_Aravis::CvCaptureCAM_Aravis()
|
|
|
|
{
|
|
|
|
camera = NULL;
|
|
|
|
stream = NULL;
|
|
|
|
framebuffer = NULL;
|
|
|
|
|
|
|
|
payload = 0;
|
|
|
|
|
|
|
|
widthMin = widthMax = heightMin = heightMax = 0;
|
2016-10-23 00:07:55 +08:00
|
|
|
xoffset = yoffset = width = height = 0;
|
2016-10-08 03:55:49 +08:00
|
|
|
fpsMin = fpsMax = gainMin = gainMax = exposureMin = exposureMax = 0;
|
2016-10-17 00:14:58 +08:00
|
|
|
controlExposure = false;
|
2017-05-13 04:46:46 +08:00
|
|
|
exposureCompensation = 0;
|
2016-10-17 00:14:58 +08:00
|
|
|
targetGrey = 0;
|
2016-10-23 00:07:55 +08:00
|
|
|
frameID = prevFrameID = 0;
|
2016-10-08 03:55:49 +08:00
|
|
|
|
2017-05-13 04:46:46 +08:00
|
|
|
num_buffers = 10;
|
2016-10-08 03:55:49 +08:00
|
|
|
frame = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CvCaptureCAM_Aravis::close()
|
|
|
|
{
|
2016-11-21 09:22:43 +08:00
|
|
|
if(camera) {
|
2016-10-17 00:14:58 +08:00
|
|
|
stopCapture();
|
2016-10-23 00:07:55 +08:00
|
|
|
|
2016-11-21 09:22:43 +08:00
|
|
|
g_object_unref(camera);
|
|
|
|
camera = NULL;
|
|
|
|
}
|
2016-10-08 03:55:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CvCaptureCAM_Aravis::getDeviceNameById(int id, std::string &device)
|
|
|
|
{
|
|
|
|
arv_update_device_list();
|
|
|
|
|
2016-10-23 00:07:55 +08:00
|
|
|
if((id >= 0) && (id < (int)arv_get_n_devices())) {
|
2016-10-17 00:14:58 +08:00
|
|
|
device = arv_get_device_id(id);
|
|
|
|
return true;
|
2016-10-08 03:55:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CvCaptureCAM_Aravis::create( int index )
|
|
|
|
{
|
|
|
|
std::string deviceName;
|
|
|
|
if(!getDeviceNameById(index, deviceName))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return NULL != (camera = arv_camera_new(deviceName.c_str()));
|
|
|
|
}
|
|
|
|
|
2016-10-09 22:58:30 +08:00
|
|
|
bool CvCaptureCAM_Aravis::init_buffers()
|
2016-10-08 03:55:49 +08:00
|
|
|
{
|
2016-10-17 00:14:58 +08:00
|
|
|
if(stream) {
|
2016-10-09 22:58:30 +08:00
|
|
|
g_object_unref(stream);
|
|
|
|
stream = NULL;
|
|
|
|
}
|
2016-10-08 03:55:49 +08:00
|
|
|
if( (stream = arv_camera_create_stream(camera, NULL, NULL)) ) {
|
|
|
|
g_object_set(stream,
|
|
|
|
"socket-buffer", ARV_GV_STREAM_SOCKET_BUFFER_AUTO,
|
|
|
|
"socket-buffer-size", 0, NULL);
|
|
|
|
g_object_set(stream,
|
|
|
|
"packet-resend", ARV_GV_STREAM_PACKET_RESEND_NEVER, NULL);
|
|
|
|
g_object_set(stream,
|
2016-10-08 19:01:01 +08:00
|
|
|
"packet-timeout", (unsigned) 40000,
|
2016-10-08 03:55:49 +08:00
|
|
|
"frame-retention", (unsigned) 200000, NULL);
|
|
|
|
|
2016-10-09 22:58:30 +08:00
|
|
|
payload = arv_camera_get_payload (camera);
|
|
|
|
|
2016-10-08 03:55:49 +08:00
|
|
|
for (int i = 0; i < num_buffers; i++)
|
|
|
|
arv_stream_push_buffer(stream, arv_buffer_new(payload, NULL));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CvCaptureCAM_Aravis::open( int index )
|
|
|
|
{
|
2016-10-17 00:14:58 +08:00
|
|
|
if(create(index)) {
|
2016-10-09 22:58:30 +08:00
|
|
|
// fetch properties bounds
|
|
|
|
pixelFormats = arv_camera_get_available_pixel_formats(camera, &pixelFormatsCnt);
|
|
|
|
|
2016-10-08 03:55:49 +08:00
|
|
|
arv_camera_get_width_bounds(camera, &widthMin, &widthMax);
|
|
|
|
arv_camera_get_height_bounds(camera, &heightMin, &heightMax);
|
2016-10-09 22:58:30 +08:00
|
|
|
arv_camera_set_region(camera, 0, 0, widthMax, heightMax);
|
2016-10-08 03:55:49 +08:00
|
|
|
|
2016-10-09 22:58:30 +08:00
|
|
|
if( (fpsAvailable = arv_camera_is_frame_rate_available(camera)) )
|
|
|
|
arv_camera_get_frame_rate_bounds(camera, &fpsMin, &fpsMax);
|
|
|
|
if( (gainAvailable = arv_camera_is_gain_available(camera)) )
|
|
|
|
arv_camera_get_gain_bounds (camera, &gainMin, &gainMax);
|
|
|
|
if( (exposureAvailable = arv_camera_is_exposure_time_available(camera)) )
|
|
|
|
arv_camera_get_exposure_time_bounds (camera, &exposureMin, &exposureMax);
|
2016-10-08 03:55:49 +08:00
|
|
|
|
2016-10-09 22:58:30 +08:00
|
|
|
// get initial values
|
|
|
|
pixelFormat = arv_camera_get_pixel_format(camera);
|
2016-10-17 00:14:58 +08:00
|
|
|
exposure = exposureAvailable ? arv_camera_get_exposure_time(camera) : 0;
|
|
|
|
gain = gainAvailable ? arv_camera_get_gain(camera) : 0;
|
2016-10-10 02:40:00 +08:00
|
|
|
fps = arv_camera_get_frame_rate(camera);
|
2016-10-08 03:55:49 +08:00
|
|
|
|
|
|
|
return startCapture();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CvCaptureCAM_Aravis::grabFrame()
|
|
|
|
{
|
2016-10-17 00:14:58 +08:00
|
|
|
// remove content of previous frame
|
|
|
|
framebuffer = NULL;
|
2016-10-09 22:58:30 +08:00
|
|
|
|
2016-10-17 00:14:58 +08:00
|
|
|
if(stream) {
|
|
|
|
ArvBuffer *arv_buffer = NULL;
|
|
|
|
int max_tries = 10;
|
|
|
|
int tries = 0;
|
|
|
|
for(; tries < max_tries; tries ++) {
|
|
|
|
arv_buffer = arv_stream_timeout_pop_buffer (stream, 200000);
|
|
|
|
if (arv_buffer != NULL && arv_buffer_get_status (arv_buffer) != ARV_BUFFER_STATUS_SUCCESS) {
|
|
|
|
arv_stream_push_buffer (stream, arv_buffer);
|
|
|
|
} else break;
|
|
|
|
}
|
2016-10-23 00:07:55 +08:00
|
|
|
if(arv_buffer != NULL && tries < max_tries) {
|
2016-10-17 00:14:58 +08:00
|
|
|
size_t buffer_size;
|
|
|
|
framebuffer = (void*)arv_buffer_get_data (arv_buffer, &buffer_size);
|
2016-10-09 22:58:30 +08:00
|
|
|
|
2016-10-23 00:07:55 +08:00
|
|
|
// retieve image size properites
|
|
|
|
arv_buffer_get_image_region (arv_buffer, &xoffset, &yoffset, &width, &height);
|
|
|
|
|
|
|
|
// retieve image ID set by camera
|
|
|
|
frameID = arv_buffer_get_frame_id(arv_buffer);
|
2016-10-09 22:58:30 +08:00
|
|
|
|
2016-10-17 00:14:58 +08:00
|
|
|
arv_stream_push_buffer(stream, arv_buffer);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2016-10-08 03:55:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
IplImage* CvCaptureCAM_Aravis::retrieveFrame(int)
|
|
|
|
{
|
|
|
|
if(framebuffer) {
|
2016-10-09 22:58:30 +08:00
|
|
|
int depth = 0, channels = 0;
|
|
|
|
switch(pixelFormat) {
|
|
|
|
case ARV_PIXEL_FORMAT_MONO_8:
|
|
|
|
depth = IPL_DEPTH_8U;
|
|
|
|
channels = 1;
|
|
|
|
break;
|
|
|
|
case ARV_PIXEL_FORMAT_MONO_12:
|
2017-05-13 04:46:46 +08:00
|
|
|
case ARV_PIXEL_FORMAT_MONO_16:
|
2016-10-09 22:58:30 +08:00
|
|
|
depth = IPL_DEPTH_16U;
|
|
|
|
channels = 1;
|
|
|
|
break;
|
2016-10-08 03:55:49 +08:00
|
|
|
}
|
2016-10-09 22:58:30 +08:00
|
|
|
if(depth && channels) {
|
|
|
|
IplImage src;
|
|
|
|
cvInitImageHeader( &src, cvSize( width, height ), depth, channels, IPL_ORIGIN_TL, 4 );
|
|
|
|
|
|
|
|
cvSetData( &src, framebuffer, src.widthStep );
|
|
|
|
if( !frame ||
|
|
|
|
frame->width != src.width ||
|
|
|
|
frame->height != src.height ||
|
|
|
|
frame->depth != src.depth ||
|
|
|
|
frame->nChannels != src.nChannels) {
|
|
|
|
|
|
|
|
cvReleaseImage( &frame );
|
|
|
|
frame = cvCreateImage( cvGetSize(&src), src.depth, channels );
|
|
|
|
}
|
|
|
|
cvCopy(&src, frame);
|
2016-10-08 03:55:49 +08:00
|
|
|
|
2017-05-13 04:46:46 +08:00
|
|
|
if(controlExposure && ((frameID - prevFrameID) >= 3)) {
|
|
|
|
// control exposure every third frame
|
2016-10-17 00:14:58 +08:00
|
|
|
// i.e. skip frame taken with previous exposure setup
|
|
|
|
autoExposureControl(frame);
|
|
|
|
}
|
|
|
|
|
2016-10-09 22:58:30 +08:00
|
|
|
return frame;
|
|
|
|
}
|
2016-10-08 03:55:49 +08:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-10-17 00:14:58 +08:00
|
|
|
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;
|
2016-10-23 00:07:55 +08:00
|
|
|
if(d >= dmid) d = ( d + (dmid * 2) ) / 3;
|
|
|
|
|
|
|
|
prevFrameID = frameID;
|
|
|
|
midGrey = brightness;
|
|
|
|
|
|
|
|
double maxe = 1e6 / fps;
|
2017-05-13 04:46:46 +08:00
|
|
|
double ne = CLIP( ( exposure * d ) / ( dmid * pow(sqrt(2), -2 * exposureCompensation) ), exposureMin, maxe);
|
2016-10-17 00:14:58 +08:00
|
|
|
|
|
|
|
// if change of value requires intervention
|
2017-05-13 04:46:46 +08:00
|
|
|
if(std::fabs(d-dmid) > 5) {
|
2016-10-23 00:07:55 +08:00
|
|
|
double ev, ng = 0;
|
2016-10-17 00:14:58 +08:00
|
|
|
|
|
|
|
if(gainAvailable && autoGain) {
|
2016-10-23 00:07:55 +08:00
|
|
|
ev = log( d / dmid ) / log(2);
|
2017-05-13 04:46:46 +08:00
|
|
|
ng = CLIP( gain + ev + exposureCompensation, gainMin, gainMax);
|
2016-10-17 00:14:58 +08:00
|
|
|
|
|
|
|
if( ng < gain ) {
|
|
|
|
// piority 1 - reduce gain
|
|
|
|
arv_camera_set_gain(camera, (gain = ng));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(exposureAvailable) {
|
2017-05-13 04:46:46 +08:00
|
|
|
// priority 2 - control of exposure time
|
|
|
|
if(std::fabs(exposure - ne) > 2) {
|
|
|
|
// we have not yet reach the max-e level
|
2016-10-17 00:14:58 +08:00
|
|
|
arv_camera_set_exposure_time(camera, (exposure = ne) );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(gainAvailable && autoGain) {
|
|
|
|
if(exposureAvailable) {
|
|
|
|
// exposure at maximum - increase gain if possible
|
2016-10-23 00:07:55 +08:00
|
|
|
if(ng > gain && ng < gainMax && ne >= maxe) {
|
|
|
|
arv_camera_set_gain(camera, (gain = ng));
|
2016-10-17 00:14:58 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2016-10-23 00:07:55 +08:00
|
|
|
// priority 3 - increase gain
|
2016-10-17 00:14:58 +08:00
|
|
|
arv_camera_set_gain(camera, (gain = ng));
|
2016-10-23 00:07:55 +08:00
|
|
|
return;
|
2016-10-17 00:14:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-23 00:07:55 +08:00
|
|
|
|
|
|
|
// 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 );
|
|
|
|
}
|
|
|
|
}
|
2016-10-17 00:14:58 +08:00
|
|
|
}
|
|
|
|
|
2016-10-08 03:55:49 +08:00
|
|
|
double CvCaptureCAM_Aravis::getProperty( int property_id ) const
|
|
|
|
{
|
2016-10-17 00:14:58 +08:00
|
|
|
switch(property_id) {
|
2016-10-23 00:07:55 +08:00
|
|
|
case CV_CAP_PROP_POS_MSEC:
|
|
|
|
return (double)frameID/fps;
|
|
|
|
|
|
|
|
case CV_CAP_PROP_FRAME_WIDTH:
|
|
|
|
return width;
|
|
|
|
|
|
|
|
case CV_CAP_PROP_FRAME_HEIGHT:
|
|
|
|
return height;
|
|
|
|
|
2016-10-17 00:14:58 +08:00
|
|
|
case CV_CAP_PROP_AUTO_EXPOSURE:
|
|
|
|
return (controlExposure ? 1 : 0);
|
|
|
|
|
2017-05-13 04:46:46 +08:00
|
|
|
case CV_CAP_PROP_BRIGHTNESS:
|
|
|
|
return exposureCompensation;
|
|
|
|
|
2016-10-08 03:55:49 +08:00
|
|
|
case CV_CAP_PROP_EXPOSURE:
|
2016-10-09 22:58:30 +08:00
|
|
|
if(exposureAvailable) {
|
|
|
|
/* exposure time in seconds, like 1/100 s */
|
|
|
|
return arv_camera_get_exposure_time(camera) / 1e6;
|
|
|
|
}
|
|
|
|
break;
|
2016-10-08 03:55:49 +08:00
|
|
|
|
|
|
|
case CV_CAP_PROP_FPS:
|
2016-10-09 22:58:30 +08:00
|
|
|
if(fpsAvailable) {
|
|
|
|
return arv_camera_get_frame_rate(camera);
|
|
|
|
}
|
|
|
|
break;
|
2016-10-08 03:55:49 +08:00
|
|
|
|
|
|
|
case CV_CAP_PROP_GAIN:
|
2016-10-09 22:58:30 +08:00
|
|
|
if(gainAvailable) {
|
|
|
|
return arv_camera_get_gain(camera);
|
|
|
|
}
|
|
|
|
break;
|
2016-10-08 19:01:01 +08:00
|
|
|
|
2016-10-09 22:58:30 +08:00
|
|
|
case CV_CAP_PROP_FOURCC:
|
2016-10-08 19:01:01 +08:00
|
|
|
{
|
|
|
|
ArvPixelFormat currFormat = arv_camera_get_pixel_format(camera);
|
|
|
|
switch( currFormat ) {
|
|
|
|
case ARV_PIXEL_FORMAT_MONO_8:
|
2016-10-17 00:14:58 +08:00
|
|
|
return MODE_Y800;
|
2016-10-08 19:01:01 +08:00
|
|
|
case ARV_PIXEL_FORMAT_MONO_12:
|
2016-10-17 00:14:58 +08:00
|
|
|
return MODE_Y12;
|
2017-05-13 04:46:46 +08:00
|
|
|
case ARV_PIXEL_FORMAT_MONO_16:
|
|
|
|
return MODE_Y16;
|
2016-10-08 19:01:01 +08:00
|
|
|
}
|
|
|
|
}
|
2016-10-23 00:07:55 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CV_CAP_PROP_BUFFERSIZE:
|
|
|
|
if(stream) {
|
|
|
|
int in, out;
|
|
|
|
arv_stream_get_n_buffers(stream, &in, &out);
|
|
|
|
// return number of available buffers in Aravis output queue
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
break;
|
2016-10-08 03:55:49 +08:00
|
|
|
}
|
|
|
|
return -1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CvCaptureCAM_Aravis::setProperty( int property_id, double value )
|
|
|
|
{
|
2016-10-17 00:14:58 +08:00
|
|
|
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;
|
2017-05-13 04:46:46 +08:00
|
|
|
case CV_CAP_PROP_BRIGHTNESS:
|
|
|
|
exposureCompensation = CLIP(value, -3., 3.);
|
|
|
|
break;
|
2016-10-17 00:14:58 +08:00
|
|
|
|
2016-10-08 03:55:49 +08:00
|
|
|
case CV_CAP_PROP_EXPOSURE:
|
2016-10-09 22:58:30 +08:00
|
|
|
if(exposureAvailable) {
|
|
|
|
/* exposure time in seconds, like 1/100 s */
|
2016-10-08 19:01:01 +08:00
|
|
|
value *= 1e6; // -> from s to us
|
2016-10-17 00:14:58 +08:00
|
|
|
|
2016-10-23 00:07:55 +08:00
|
|
|
arv_camera_set_exposure_time(camera, exposure = CLIP(value, exposureMin, exposureMax));
|
2016-10-09 22:58:30 +08:00
|
|
|
break;
|
|
|
|
} else return false;
|
2016-10-08 03:55:49 +08:00
|
|
|
|
|
|
|
case CV_CAP_PROP_FPS:
|
2016-10-09 22:58:30 +08:00
|
|
|
if(fpsAvailable) {
|
2016-10-23 00:07:55 +08:00
|
|
|
arv_camera_set_frame_rate(camera, fps = CLIP(value, fpsMin, fpsMax));
|
2016-10-09 22:58:30 +08:00
|
|
|
break;
|
|
|
|
} else return false;
|
2016-10-08 03:55:49 +08:00
|
|
|
|
|
|
|
case CV_CAP_PROP_GAIN:
|
2016-10-09 22:58:30 +08:00
|
|
|
if(gainAvailable) {
|
2016-10-17 00:14:58 +08:00
|
|
|
if ( (autoGain = (-1 == value) ) )
|
|
|
|
break;
|
|
|
|
|
2016-10-23 00:07:55 +08:00
|
|
|
arv_camera_set_gain(camera, gain = CLIP(value, gainMin, gainMax));
|
2016-10-09 22:58:30 +08:00
|
|
|
break;
|
|
|
|
} else return false;
|
2016-10-08 19:01:01 +08:00
|
|
|
|
2016-10-09 22:58:30 +08:00
|
|
|
case CV_CAP_PROP_FOURCC:
|
2016-10-08 19:01:01 +08:00
|
|
|
{
|
|
|
|
ArvPixelFormat newFormat = pixelFormat;
|
|
|
|
switch((int)value) {
|
2016-10-17 00:14:58 +08:00
|
|
|
case MODE_GREY:
|
|
|
|
case MODE_Y800:
|
2016-10-08 19:01:01 +08:00
|
|
|
newFormat = ARV_PIXEL_FORMAT_MONO_8;
|
2016-10-17 00:14:58 +08:00
|
|
|
targetGrey = 128;
|
2016-10-08 19:01:01 +08:00
|
|
|
break;
|
2016-10-17 00:14:58 +08:00
|
|
|
case MODE_Y12:
|
2016-10-08 19:01:01 +08:00
|
|
|
newFormat = ARV_PIXEL_FORMAT_MONO_12;
|
2016-10-17 00:14:58 +08:00
|
|
|
targetGrey = 2048;
|
2016-10-08 19:01:01 +08:00
|
|
|
break;
|
2017-05-13 04:46:46 +08:00
|
|
|
case MODE_Y16:
|
|
|
|
newFormat = ARV_PIXEL_FORMAT_MONO_16;
|
|
|
|
targetGrey = 32768;
|
|
|
|
break;
|
2016-10-08 19:01:01 +08:00
|
|
|
}
|
|
|
|
if(newFormat != pixelFormat) {
|
2016-10-09 22:58:30 +08:00
|
|
|
stopCapture();
|
2016-10-08 19:01:01 +08:00
|
|
|
arv_camera_set_pixel_format(camera, pixelFormat = newFormat);
|
2016-10-09 22:58:30 +08:00
|
|
|
startCapture();
|
2016-10-08 19:01:01 +08:00
|
|
|
}
|
|
|
|
}
|
2016-10-08 03:55:49 +08:00
|
|
|
break;
|
2016-10-08 19:01:01 +08:00
|
|
|
|
2016-10-23 00:07:55 +08:00
|
|
|
case CV_CAP_PROP_BUFFERSIZE:
|
|
|
|
{
|
|
|
|
int x = (int)value;
|
|
|
|
if((x > 0) && (x != num_buffers)) {
|
|
|
|
stopCapture();
|
|
|
|
num_buffers = x;
|
|
|
|
startCapture();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
2016-10-08 19:01:01 +08:00
|
|
|
default:
|
|
|
|
return false;
|
2016-10-08 03:55:49 +08:00
|
|
|
}
|
2016-10-08 19:01:01 +08:00
|
|
|
|
|
|
|
return true;
|
2016-10-08 03:55:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CvCaptureCAM_Aravis::stopCapture()
|
|
|
|
{
|
2016-10-10 02:40:00 +08:00
|
|
|
arv_camera_stop_acquisition(camera);
|
2016-10-08 03:55:49 +08:00
|
|
|
|
2016-11-21 09:22:43 +08:00
|
|
|
if(stream) {
|
|
|
|
g_object_unref(stream);
|
|
|
|
stream = NULL;
|
|
|
|
}
|
2016-10-08 03:55:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CvCaptureCAM_Aravis::startCapture()
|
|
|
|
{
|
2016-10-17 00:14:58 +08:00
|
|
|
if(init_buffers() ) {
|
2016-10-09 22:58:30 +08:00
|
|
|
arv_camera_set_acquisition_mode(camera, ARV_ACQUISITION_MODE_CONTINUOUS);
|
|
|
|
arv_camera_start_acquisition(camera);
|
2016-10-08 03:55:49 +08:00
|
|
|
|
2016-10-09 22:58:30 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2016-10-08 03:55:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CvCapture* cvCreateCameraCapture_Aravis( int index )
|
|
|
|
{
|
|
|
|
CvCaptureCAM_Aravis* capture = new CvCaptureCAM_Aravis;
|
|
|
|
|
2016-10-17 00:14:58 +08:00
|
|
|
if(capture->open(index)) {
|
2016-10-08 03:55:49 +08:00
|
|
|
return capture;
|
2016-10-17 00:14:58 +08:00
|
|
|
}
|
2016-10-08 03:55:49 +08:00
|
|
|
|
|
|
|
delete capture;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|