make v4l signal detection switchable (#415)

This commit is contained in:
redPanther
2017-03-15 20:33:11 +01:00
committed by GitHub
parent bae3808957
commit a20e073bbd
12 changed files with 106 additions and 55 deletions

View File

@@ -47,6 +47,12 @@ V4L2Grabber::V4L2Grabber(const QString & device
, _frameDecimation(std::max(1, frameDecimation))
, _noSignalCounterThreshold(50)
, _noSignalThresholdColor(ColorRgb{0,0,0})
, _signalDetectionEnabled(true)
, _noSignalDetected(false)
, _x_frac_min(0.25)
, _y_frac_min(0.25)
, _x_frac_max(0.75)
, _y_frac_max(0.75)
, _currentFrame(0)
, _noSignalCounter(0)
, _streamNotifier(nullptr)
@@ -54,11 +60,6 @@ V4L2Grabber::V4L2Grabber(const QString & device
, _log(Logger::getInstance("V4L2:"+device))
, _initialized(false)
, _deviceAutoDiscoverEnabled(false)
, _noSignalDetected(false)
, _x_frac_min(0.25)
, _y_frac_min(0.25)
, _x_frac_max(0.75)
, _y_frac_max(0.75)
{
_imageResampler.setHorizontalPixelDecimation(std::max(1, horizontalPixelDecimation));
@@ -809,50 +810,57 @@ void V4L2Grabber::process_image(const uint8_t * data)
Image<ColorRgb> image(0, 0);
_imageResampler.processImage(data, _width, _height, _lineLength, _pixelFormat, image);
// check signal (only in center of the resulting image, because some grabbers have noise values along the borders)
bool noSignal = true;
// top left
unsigned xOffset = image.width() * _x_frac_min;
unsigned yOffset = image.height() * _y_frac_min;
// bottom right
unsigned xMax = image.width() * _x_frac_max;
unsigned yMax = image.height() * _y_frac_max;
for (unsigned x = xOffset; noSignal && x < xMax; ++x)
if (_signalDetectionEnabled)
{
for (unsigned y = yOffset; noSignal && y < yMax; ++y)
// check signal (only in center of the resulting image, because some grabbers have noise values along the borders)
bool noSignal = true;
// top left
unsigned xOffset = image.width() * _x_frac_min;
unsigned yOffset = image.height() * _y_frac_min;
// bottom right
unsigned xMax = image.width() * _x_frac_max;
unsigned yMax = image.height() * _y_frac_max;
for (unsigned x = xOffset; noSignal && x < xMax; ++x)
{
noSignal &= (ColorRgb&)image(x, y) <= _noSignalThresholdColor;
for (unsigned y = yOffset; noSignal && y < yMax; ++y)
{
noSignal &= (ColorRgb&)image(x, y) <= _noSignalThresholdColor;
}
}
}
if (noSignal)
{
++_noSignalCounter;
if (noSignal)
{
++_noSignalCounter;
}
else
{
if (_noSignalCounter >= _noSignalCounterThreshold)
{
_noSignalDetected = true;
Info(_log, "Signal detected");
}
_noSignalCounter = 0;
}
if ( _noSignalCounter < _noSignalCounterThreshold)
{
emit newFrame(image);
}
else if (_noSignalCounter == _noSignalCounterThreshold)
{
_noSignalDetected = false;
Info(_log, "Signal lost");
}
}
else
{
if (_noSignalCounter >= _noSignalCounterThreshold)
{
_noSignalDetected = true;
Info(_log, "Signal detected");
}
_noSignalCounter = 0;
}
if (_noSignalCounter < _noSignalCounterThreshold)
{
emit newFrame(image);
}
else if (_noSignalCounter == _noSignalCounterThreshold)
{
_noSignalDetected = false;
Info(_log, "Signal lost");
}
}
int V4L2Grabber::xioctl(int request, void *arg)
@@ -877,3 +885,13 @@ void V4L2Grabber::throw_errno_exception(const QString & error)
{
throw std::runtime_error(QString(error + " error code " + QString::number(errno) + ", " + strerror(errno)).toStdString());
}
void V4L2Grabber::setSignalDetectionEnable(bool enable)
{
_signalDetectionEnabled = enable;
}
bool V4L2Grabber::getSignalDetectionEnabled()
{
return _signalDetectionEnabled;
}

View File

@@ -122,3 +122,13 @@ void V4L2Wrapper::action()
{
checkSources();
}
void V4L2Wrapper::setSignalDetectionEnable(bool enable)
{
_grabber.setSignalDetectionEnable(enable);
}
bool V4L2Wrapper::getSignalDetectionEnable()
{
return _grabber.getSignalDetectionEnabled();
}