From 5e3cb497fa350da20aa6dde035cade5f67d323c1 Mon Sep 17 00:00:00 2001 From: johan Date: Tue, 4 Mar 2014 22:04:15 +0100 Subject: [PATCH] Add support to set the threshold for each RGB channel separately Former-commit-id: 5edb206bb2657e78f711f67625fd5f6164d8296c --- include/grabber/V4L2Grabber.h | 12 +++++++-- include/grabber/V4L2Wrapper.h | 3 +++ include/utils/ColorRgb.h | 7 +++++ libsrc/grabber/v4l2/V4L2Grabber.cpp | 40 ++++++++++++++++++++++++++--- libsrc/grabber/v4l2/V4L2Wrapper.cpp | 26 ++++++++++++------- src/hyperion-v4l2/ImageHandler.cpp | 26 +++---------------- src/hyperion-v4l2/ImageHandler.h | 11 +------- src/hyperion-v4l2/hyperion-v4l2.cpp | 12 ++++++++- src/hyperiond/hyperiond.cpp | 3 +++ 9 files changed, 92 insertions(+), 48 deletions(-) diff --git a/include/grabber/V4L2Grabber.h b/include/grabber/V4L2Grabber.h index c2f61f16..ff600108 100644 --- a/include/grabber/V4L2Grabber.h +++ b/include/grabber/V4L2Grabber.h @@ -24,8 +24,7 @@ class V4L2Grabber : public QObject Q_OBJECT public: - V4L2Grabber( - const std::string & device, + V4L2Grabber(const std::string & device, int input, VideoStandard videoStandard, int width, @@ -43,6 +42,11 @@ public slots: void set3D(VideoMode mode); + void setSignalThreshold(double redSignalThreshold, + double greenSignalThreshold, + double blueSignalThreshold, + int noSignalCounterThreshold); + void start(); void stop(); @@ -110,10 +114,14 @@ private: int _frameDecimation; int _horizontalPixelDecimation; int _verticalPixelDecimation; + int _noSignalCounterThreshold; + + ColorRgb _noSignalThresholdColor; VideoMode _mode3D; int _currentFrame; + int _noSignalCounter; QSocketNotifier * _streamNotifier; }; diff --git a/include/grabber/V4L2Wrapper.h b/include/grabber/V4L2Wrapper.h index ad6ca247..99159186 100644 --- a/include/grabber/V4L2Wrapper.h +++ b/include/grabber/V4L2Wrapper.h @@ -19,6 +19,9 @@ public: int height, int frameDecimation, int pixelDecimation, + double redSignalThreshold, + double greenSignalThreshold, + double blueSignalThreshold, Hyperion * hyperion, int hyperionPriority); virtual ~V4L2Wrapper(); diff --git a/include/utils/ColorRgb.h b/include/utils/ColorRgb.h index c578d69f..ea544fe0 100644 --- a/include/utils/ColorRgb.h +++ b/include/utils/ColorRgb.h @@ -48,3 +48,10 @@ inline std::ostream& operator<<(std::ostream& os, const ColorRgb& color) os << "{" << unsigned(color.red) << "," << unsigned(color.green) << "," << unsigned(color.blue) << "}"; return os; } + + +/// Compare operator to check if a color is 'smaller' than another color +inline bool operator<(const ColorRgb & lhs, const ColorRgb & rhs) +{ + return (lhs.red < rhs.red) && (lhs.green < rhs.green) && (lhs.blue < rhs.blue); +} diff --git a/libsrc/grabber/v4l2/V4L2Grabber.cpp b/libsrc/grabber/v4l2/V4L2Grabber.cpp index dc0e47c6..2e4bf67d 100644 --- a/libsrc/grabber/v4l2/V4L2Grabber.cpp +++ b/libsrc/grabber/v4l2/V4L2Grabber.cpp @@ -36,8 +36,7 @@ static void yuv2rgb(uint8_t y, uint8_t u, uint8_t v, uint8_t & r, uint8_t & g, u } -V4L2Grabber::V4L2Grabber( - const std::string & device, +V4L2Grabber::V4L2Grabber(const std::string & device, int input, VideoStandard videoStandard, int width, @@ -59,8 +58,11 @@ V4L2Grabber::V4L2Grabber( _frameDecimation(std::max(1, frameDecimation)), _horizontalPixelDecimation(std::max(1, horizontalPixelDecimation)), _verticalPixelDecimation(std::max(1, verticalPixelDecimation)), + _noSignalCounterThreshold(50), + _noSignalThresholdColor(ColorRgb{0,0,0}), _mode3D(VIDEO_2D), _currentFrame(0), + _noSignalCounter(0), _streamNotifier(nullptr) { open_device(); @@ -91,6 +93,14 @@ void V4L2Grabber::set3D(VideoMode mode) _mode3D = mode; } +void V4L2Grabber::setSignalThreshold(double redSignalThreshold, double greenSignalThreshold, double blueSignalThreshold, int noSignalCounterThreshold) +{ + _noSignalThresholdColor.red = uint8_t(255*redSignalThreshold); + _noSignalThresholdColor.green = uint8_t(255*greenSignalThreshold); + _noSignalThresholdColor.blue = uint8_t(255*blueSignalThreshold); + _noSignalCounterThreshold = std::max(1, noSignalCounterThreshold); +} + void V4L2Grabber::start() { _streamNotifier->setEnabled(true); @@ -658,6 +668,8 @@ void V4L2Grabber::process_image(const uint8_t * data) int outputHeight = (height - _cropTop - _cropBottom + _verticalPixelDecimation/2) / _verticalPixelDecimation; Image image(outputWidth, outputHeight); + bool noSignal = true; + for (int ySource = _cropTop + _verticalPixelDecimation/2, yDest = 0; ySource < height - _cropBottom; ySource += _verticalPixelDecimation, ++yDest) { for (int xSource = _cropLeft + _horizontalPixelDecimation/2, xDest = 0; xSource < width - _cropRight; xSource += _horizontalPixelDecimation, ++xDest) @@ -683,10 +695,32 @@ void V4L2Grabber::process_image(const uint8_t * data) ColorRgb & rgb = image(xDest, yDest); yuv2rgb(y, u, v, rgb.red, rgb.green, rgb.blue); + noSignal &= rgb < _noSignalThresholdColor; } } - emit newFrame(image); + if (noSignal) + { + ++_noSignalCounter; + } + else + { + if (_noSignalCounter >= _noSignalCounterThreshold) + { + std::cout << "V4L2 Grabber: " << "Signal detected" << std::endl; + } + + _noSignalCounter = 0; + } + + if (_noSignalCounter < _noSignalCounterThreshold) + { + emit newFrame(image); + } + else if (_noSignalCounter == _noSignalCounterThreshold) + { + std::cout << "V4L2 Grabber: " << "Signal lost" << std::endl; + } } int V4L2Grabber::xioctl(int request, void *arg) diff --git a/libsrc/grabber/v4l2/V4L2Wrapper.cpp b/libsrc/grabber/v4l2/V4L2Wrapper.cpp index f859c05e..9a8f450d 100644 --- a/libsrc/grabber/v4l2/V4L2Wrapper.cpp +++ b/libsrc/grabber/v4l2/V4L2Wrapper.cpp @@ -11,22 +11,32 @@ V4L2Wrapper::V4L2Wrapper(const std::string &device, int height, int frameDecimation, int pixelDecimation, + double redSignalThreshold, + double greenSignalThreshold, + double blueSignalThreshold, Hyperion *hyperion, int hyperionPriority) : _timeout_ms(1000), _priority(hyperionPriority), _grabber(device, - input, - videoStandard, - width, - height, - frameDecimation, - pixelDecimation, - pixelDecimation), + input, + videoStandard, + width, + height, + frameDecimation, + pixelDecimation, + pixelDecimation), _processor(ImageProcessorFactory::getInstance().newImageProcessor()), _hyperion(hyperion), _ledColors(hyperion->getLedCount(), ColorRgb{0,0,0}) { + // set the signal detection threshold of the grabber + _grabber.setSignalThreshold( + redSignalThreshold, + greenSignalThreshold, + blueSignalThreshold, + 50); + // register the image type qRegisterMetaType>("Image"); qRegisterMetaType>("std::vector"); @@ -71,8 +81,6 @@ void V4L2Wrapper::set3D(VideoMode mode) void V4L2Wrapper::newFrame(const Image &image) { - // TODO: add a signal detector - // process the new image _processor->process(image, _ledColors); diff --git a/src/hyperion-v4l2/ImageHandler.cpp b/src/hyperion-v4l2/ImageHandler.cpp index afe7419c..19a1da80 100644 --- a/src/hyperion-v4l2/ImageHandler.cpp +++ b/src/hyperion-v4l2/ImageHandler.cpp @@ -1,11 +1,9 @@ // hyperion-v4l2 includes #include "ImageHandler.h" -ImageHandler::ImageHandler(const std::string & address, int priority, double signalThreshold, bool skipProtoReply) : +ImageHandler::ImageHandler(const std::string & address, int priority, bool skipProtoReply) : _priority(priority), - _connection(address), - _signalThreshold(signalThreshold), - _signalProcessor(100, 50, 0, uint8_t(std::min(255, std::max(0, int(255*signalThreshold))))) + _connection(address) { _connection.setSkipReply(skipProtoReply); } @@ -16,23 +14,5 @@ ImageHandler::~ImageHandler() void ImageHandler::receiveImage(const Image & image) { - // check if we should do signal detection - if (_signalThreshold < 0) - { - _connection.setImage(image, _priority, 1000); - } - else - { - if (_signalProcessor.process(image)) - { - std::cout << "Signal state = " << (_signalProcessor.getCurrentBorder().unknown ? "off" : "on") << std::endl; - } - - // consider an unknown border as no signal - // send the image to Hyperion if we have a signal - if (!_signalProcessor.getCurrentBorder().unknown) - { - _connection.setImage(image, _priority, 1000); - } - } + _connection.setImage(image, _priority, 1000); } diff --git a/src/hyperion-v4l2/ImageHandler.h b/src/hyperion-v4l2/ImageHandler.h index 5658c750..8730989d 100644 --- a/src/hyperion-v4l2/ImageHandler.h +++ b/src/hyperion-v4l2/ImageHandler.h @@ -5,9 +5,6 @@ #include #include -// blackborder includes -#include - // hyperion v4l2 includes #include "ProtoConnection.h" @@ -17,7 +14,7 @@ class ImageHandler : public QObject Q_OBJECT public: - ImageHandler(const std::string & address, int priority, double signalThreshold, bool skipProtoReply); + ImageHandler(const std::string & address, int priority, bool skipProtoReply); virtual ~ImageHandler(); public slots: @@ -31,10 +28,4 @@ private: /// Hyperion proto connection object ProtoConnection _connection; - - /// Threshold used for signal detection - double _signalThreshold; - - /// Blackborder detector which is used as a signal detector (unknown border = no signal) - hyperion::BlackBorderProcessor _signalProcessor; }; diff --git a/src/hyperion-v4l2/hyperion-v4l2.cpp b/src/hyperion-v4l2/hyperion-v4l2.cpp index 04d5d5b4..611abe3f 100644 --- a/src/hyperion-v4l2/hyperion-v4l2.cpp +++ b/src/hyperion-v4l2/hyperion-v4l2.cpp @@ -63,6 +63,9 @@ int main(int argc, char** argv) IntParameter & argFrameDecimation = parameters.add ('f', "frame-decimator", "Decimation factor for the video frames [default=1]"); SwitchParameter<> & argScreenshot = parameters.add> (0x0, "screenshot", "Take a single screenshot, save it to file and quit"); DoubleParameter & argSignalThreshold = parameters.add ('t', "signal-threshold", "The signal threshold for detecting the presence of a signal. Value should be between 0.0 and 1.0."); + DoubleParameter & argRedSignalThreshold = parameters.add (0x0, "red-threshold", "The red signal threshold. Value should be between 0.0 and 1.0. (overrides --signal-threshold)"); + DoubleParameter & argGreenSignalThreshold = parameters.add (0x0, "green-threshold", "The green signal threshold. Value should be between 0.0 and 1.0. (overrides --signal-threshold)"); + DoubleParameter & argBlueSignalThreshold = parameters.add (0x0, "blue-threshold", "The blue signal threshold. Value should be between 0.0 and 1.0. (overrides --signal-threshold)"); SwitchParameter<> & arg3DSBS = parameters.add> (0x0, "3DSBS", "Interpret the incoming video stream as 3D side-by-side"); SwitchParameter<> & arg3DTAB = parameters.add> (0x0, "3DTAB", "Interpret the incoming video stream as 3D top-and-bottom"); StringParameter & argAddress = parameters.add ('a', "address", "Set the address of the hyperion server [default: 127.0.0.1:19445]"); @@ -110,6 +113,13 @@ int main(int argc, char** argv) std::max(1, argSizeDecimation.getValue()), std::max(1, argSizeDecimation.getValue())); + // set signal detection + grabber.setSignalThreshold( + std::min(1.0, std::max(0.0, argRedSignalThreshold.isSet() ? argRedSignalThreshold.getValue() : argSignalThreshold.getValue())), + std::min(1.0, std::max(0.0, argGreenSignalThreshold.isSet() ? argGreenSignalThreshold.getValue() : argSignalThreshold.getValue())), + std::min(1.0, std::max(0.0, argBlueSignalThreshold.isSet() ? argBlueSignalThreshold.getValue() : argSignalThreshold.getValue())), + 50); + // set cropping values grabber.setCropping( std::max(0, argCropLeft.getValue()), @@ -138,7 +148,7 @@ int main(int argc, char** argv) } else { - ImageHandler handler(argAddress.getValue(), argPriority.getValue(), argSignalThreshold.getValue(), argSkipReply.isSet()); + ImageHandler handler(argAddress.getValue(), argPriority.getValue(), argSkipReply.isSet()); QObject::connect(&grabber, SIGNAL(newFrame(Image)), &handler, SLOT(receiveImage(Image))); grabber.start(); QCoreApplication::exec(); diff --git a/src/hyperiond/hyperiond.cpp b/src/hyperiond/hyperiond.cpp index 71b01c0b..d31e9f07 100644 --- a/src/hyperiond/hyperiond.cpp +++ b/src/hyperiond/hyperiond.cpp @@ -184,6 +184,9 @@ int main(int argc, char** argv) grabberConfig.get("height", -1).asInt(), grabberConfig.get("frameDecimation", 2).asInt(), grabberConfig.get("sizeDecimation", 8).asInt(), + grabberConfig.get("redSignalThreshold", 0.0).asDouble(), + grabberConfig.get("greenSignalThreshold", 0.0).asDouble(), + grabberConfig.get("blueSignalThreshold", 0.0).asDouble(), &hyperion, grabberConfig.get("priority", 800).asInt()); v4l2Grabber->set3D(parse3DMode(grabberConfig.get("mode", "2D").asString()));