Skip to content
This repository was archived by the owner on May 6, 2021. It is now read-only.

Commit 5edb206

Browse files
committed
Add support to set the threshold for each RGB channel separately
1 parent 858ca23 commit 5edb206

File tree

9 files changed

+92
-48
lines changed

9 files changed

+92
-48
lines changed

include/grabber/V4L2Grabber.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ class V4L2Grabber : public QObject
2424
Q_OBJECT
2525

2626
public:
27-
V4L2Grabber(
28-
const std::string & device,
27+
V4L2Grabber(const std::string & device,
2928
int input,
3029
VideoStandard videoStandard,
3130
int width,
@@ -43,6 +42,11 @@ public slots:
4342

4443
void set3D(VideoMode mode);
4544

45+
void setSignalThreshold(double redSignalThreshold,
46+
double greenSignalThreshold,
47+
double blueSignalThreshold,
48+
int noSignalCounterThreshold);
49+
4650
void start();
4751

4852
void stop();
@@ -110,10 +114,14 @@ private slots:
110114
int _frameDecimation;
111115
int _horizontalPixelDecimation;
112116
int _verticalPixelDecimation;
117+
int _noSignalCounterThreshold;
118+
119+
ColorRgb _noSignalThresholdColor;
113120

114121
VideoMode _mode3D;
115122

116123
int _currentFrame;
124+
int _noSignalCounter;
117125

118126
QSocketNotifier * _streamNotifier;
119127
};

include/grabber/V4L2Wrapper.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ class V4L2Wrapper : public QObject
1919
int height,
2020
int frameDecimation,
2121
int pixelDecimation,
22+
double redSignalThreshold,
23+
double greenSignalThreshold,
24+
double blueSignalThreshold,
2225
Hyperion * hyperion,
2326
int hyperionPriority);
2427
virtual ~V4L2Wrapper();

include/utils/ColorRgb.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,10 @@ inline std::ostream& operator<<(std::ostream& os, const ColorRgb& color)
4848
os << "{" << unsigned(color.red) << "," << unsigned(color.green) << "," << unsigned(color.blue) << "}";
4949
return os;
5050
}
51+
52+
53+
/// Compare operator to check if a color is 'smaller' than another color
54+
inline bool operator<(const ColorRgb & lhs, const ColorRgb & rhs)
55+
{
56+
return (lhs.red < rhs.red) && (lhs.green < rhs.green) && (lhs.blue < rhs.blue);
57+
}

libsrc/grabber/v4l2/V4L2Grabber.cpp

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ static void yuv2rgb(uint8_t y, uint8_t u, uint8_t v, uint8_t & r, uint8_t & g, u
3636
}
3737

3838

39-
V4L2Grabber::V4L2Grabber(
40-
const std::string & device,
39+
V4L2Grabber::V4L2Grabber(const std::string & device,
4140
int input,
4241
VideoStandard videoStandard,
4342
int width,
@@ -59,8 +58,11 @@ V4L2Grabber::V4L2Grabber(
5958
_frameDecimation(std::max(1, frameDecimation)),
6059
_horizontalPixelDecimation(std::max(1, horizontalPixelDecimation)),
6160
_verticalPixelDecimation(std::max(1, verticalPixelDecimation)),
61+
_noSignalCounterThreshold(50),
62+
_noSignalThresholdColor(ColorRgb{0,0,0}),
6263
_mode3D(VIDEO_2D),
6364
_currentFrame(0),
65+
_noSignalCounter(0),
6466
_streamNotifier(nullptr)
6567
{
6668
open_device();
@@ -91,6 +93,14 @@ void V4L2Grabber::set3D(VideoMode mode)
9193
_mode3D = mode;
9294
}
9395

96+
void V4L2Grabber::setSignalThreshold(double redSignalThreshold, double greenSignalThreshold, double blueSignalThreshold, int noSignalCounterThreshold)
97+
{
98+
_noSignalThresholdColor.red = uint8_t(255*redSignalThreshold);
99+
_noSignalThresholdColor.green = uint8_t(255*greenSignalThreshold);
100+
_noSignalThresholdColor.blue = uint8_t(255*blueSignalThreshold);
101+
_noSignalCounterThreshold = std::max(1, noSignalCounterThreshold);
102+
}
103+
94104
void V4L2Grabber::start()
95105
{
96106
_streamNotifier->setEnabled(true);
@@ -658,6 +668,8 @@ void V4L2Grabber::process_image(const uint8_t * data)
658668
int outputHeight = (height - _cropTop - _cropBottom + _verticalPixelDecimation/2) / _verticalPixelDecimation;
659669
Image<ColorRgb> image(outputWidth, outputHeight);
660670

671+
bool noSignal = true;
672+
661673
for (int ySource = _cropTop + _verticalPixelDecimation/2, yDest = 0; ySource < height - _cropBottom; ySource += _verticalPixelDecimation, ++yDest)
662674
{
663675
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)
683695

684696
ColorRgb & rgb = image(xDest, yDest);
685697
yuv2rgb(y, u, v, rgb.red, rgb.green, rgb.blue);
698+
noSignal &= rgb < _noSignalThresholdColor;
699+
}
700+
}
701+
702+
if (noSignal)
703+
{
704+
++_noSignalCounter;
705+
}
706+
else
707+
{
708+
if (_noSignalCounter >= _noSignalCounterThreshold)
709+
{
710+
std::cout << "V4L2 Grabber: " << "Signal detected" << std::endl;
686711
}
712+
713+
_noSignalCounter = 0;
687714
}
688715

689-
emit newFrame(image);
716+
if (_noSignalCounter < _noSignalCounterThreshold)
717+
{
718+
emit newFrame(image);
719+
}
720+
else if (_noSignalCounter == _noSignalCounterThreshold)
721+
{
722+
std::cout << "V4L2 Grabber: " << "Signal lost" << std::endl;
723+
}
690724
}
691725

692726
int V4L2Grabber::xioctl(int request, void *arg)

libsrc/grabber/v4l2/V4L2Wrapper.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,32 @@ V4L2Wrapper::V4L2Wrapper(const std::string &device,
1111
int height,
1212
int frameDecimation,
1313
int pixelDecimation,
14+
double redSignalThreshold,
15+
double greenSignalThreshold,
16+
double blueSignalThreshold,
1417
Hyperion *hyperion,
1518
int hyperionPriority) :
1619
_timeout_ms(1000),
1720
_priority(hyperionPriority),
1821
_grabber(device,
19-
input,
20-
videoStandard,
21-
width,
22-
height,
23-
frameDecimation,
24-
pixelDecimation,
25-
pixelDecimation),
22+
input,
23+
videoStandard,
24+
width,
25+
height,
26+
frameDecimation,
27+
pixelDecimation,
28+
pixelDecimation),
2629
_processor(ImageProcessorFactory::getInstance().newImageProcessor()),
2730
_hyperion(hyperion),
2831
_ledColors(hyperion->getLedCount(), ColorRgb{0,0,0})
2932
{
33+
// set the signal detection threshold of the grabber
34+
_grabber.setSignalThreshold(
35+
redSignalThreshold,
36+
greenSignalThreshold,
37+
blueSignalThreshold,
38+
50);
39+
3040
// register the image type
3141
qRegisterMetaType<Image<ColorRgb>>("Image<ColorRgb>");
3242
qRegisterMetaType<std::vector<ColorRgb>>("std::vector<ColorRgb>");
@@ -71,8 +81,6 @@ void V4L2Wrapper::set3D(VideoMode mode)
7181

7282
void V4L2Wrapper::newFrame(const Image<ColorRgb> &image)
7383
{
74-
// TODO: add a signal detector
75-
7684
// process the new image
7785
_processor->process(image, _ledColors);
7886

src/hyperion-v4l2/ImageHandler.cpp

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
// hyperion-v4l2 includes
22
#include "ImageHandler.h"
33

4-
ImageHandler::ImageHandler(const std::string & address, int priority, double signalThreshold, bool skipProtoReply) :
4+
ImageHandler::ImageHandler(const std::string & address, int priority, bool skipProtoReply) :
55
_priority(priority),
6-
_connection(address),
7-
_signalThreshold(signalThreshold),
8-
_signalProcessor(100, 50, 0, uint8_t(std::min(255, std::max(0, int(255*signalThreshold)))))
6+
_connection(address)
97
{
108
_connection.setSkipReply(skipProtoReply);
119
}
@@ -16,23 +14,5 @@ ImageHandler::~ImageHandler()
1614

1715
void ImageHandler::receiveImage(const Image<ColorRgb> & image)
1816
{
19-
// check if we should do signal detection
20-
if (_signalThreshold < 0)
21-
{
22-
_connection.setImage(image, _priority, 1000);
23-
}
24-
else
25-
{
26-
if (_signalProcessor.process(image))
27-
{
28-
std::cout << "Signal state = " << (_signalProcessor.getCurrentBorder().unknown ? "off" : "on") << std::endl;
29-
}
30-
31-
// consider an unknown border as no signal
32-
// send the image to Hyperion if we have a signal
33-
if (!_signalProcessor.getCurrentBorder().unknown)
34-
{
35-
_connection.setImage(image, _priority, 1000);
36-
}
37-
}
17+
_connection.setImage(image, _priority, 1000);
3818
}

src/hyperion-v4l2/ImageHandler.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
#include <utils/Image.h>
66
#include <utils/ColorRgb.h>
77

8-
// blackborder includes
9-
#include <blackborder/BlackBorderProcessor.h>
10-
118
// hyperion v4l2 includes
129
#include "ProtoConnection.h"
1310

@@ -17,7 +14,7 @@ class ImageHandler : public QObject
1714
Q_OBJECT
1815

1916
public:
20-
ImageHandler(const std::string & address, int priority, double signalThreshold, bool skipProtoReply);
17+
ImageHandler(const std::string & address, int priority, bool skipProtoReply);
2118
virtual ~ImageHandler();
2219

2320
public slots:
@@ -31,10 +28,4 @@ public slots:
3128

3229
/// Hyperion proto connection object
3330
ProtoConnection _connection;
34-
35-
/// Threshold used for signal detection
36-
double _signalThreshold;
37-
38-
/// Blackborder detector which is used as a signal detector (unknown border = no signal)
39-
hyperion::BlackBorderProcessor _signalProcessor;
4031
};

src/hyperion-v4l2/hyperion-v4l2.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ int main(int argc, char** argv)
6363
IntParameter & argFrameDecimation = parameters.add<IntParameter> ('f', "frame-decimator", "Decimation factor for the video frames [default=1]");
6464
SwitchParameter<> & argScreenshot = parameters.add<SwitchParameter<>> (0x0, "screenshot", "Take a single screenshot, save it to file and quit");
6565
DoubleParameter & argSignalThreshold = parameters.add<DoubleParameter> ('t', "signal-threshold", "The signal threshold for detecting the presence of a signal. Value should be between 0.0 and 1.0.");
66+
DoubleParameter & argRedSignalThreshold = parameters.add<DoubleParameter> (0x0, "red-threshold", "The red signal threshold. Value should be between 0.0 and 1.0. (overrides --signal-threshold)");
67+
DoubleParameter & argGreenSignalThreshold = parameters.add<DoubleParameter> (0x0, "green-threshold", "The green signal threshold. Value should be between 0.0 and 1.0. (overrides --signal-threshold)");
68+
DoubleParameter & argBlueSignalThreshold = parameters.add<DoubleParameter> (0x0, "blue-threshold", "The blue signal threshold. Value should be between 0.0 and 1.0. (overrides --signal-threshold)");
6669
SwitchParameter<> & arg3DSBS = parameters.add<SwitchParameter<>> (0x0, "3DSBS", "Interpret the incoming video stream as 3D side-by-side");
6770
SwitchParameter<> & arg3DTAB = parameters.add<SwitchParameter<>> (0x0, "3DTAB", "Interpret the incoming video stream as 3D top-and-bottom");
6871
StringParameter & argAddress = parameters.add<StringParameter> ('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)
110113
std::max(1, argSizeDecimation.getValue()),
111114
std::max(1, argSizeDecimation.getValue()));
112115

116+
// set signal detection
117+
grabber.setSignalThreshold(
118+
std::min(1.0, std::max(0.0, argRedSignalThreshold.isSet() ? argRedSignalThreshold.getValue() : argSignalThreshold.getValue())),
119+
std::min(1.0, std::max(0.0, argGreenSignalThreshold.isSet() ? argGreenSignalThreshold.getValue() : argSignalThreshold.getValue())),
120+
std::min(1.0, std::max(0.0, argBlueSignalThreshold.isSet() ? argBlueSignalThreshold.getValue() : argSignalThreshold.getValue())),
121+
50);
122+
113123
// set cropping values
114124
grabber.setCropping(
115125
std::max(0, argCropLeft.getValue()),
@@ -138,7 +148,7 @@ int main(int argc, char** argv)
138148
}
139149
else
140150
{
141-
ImageHandler handler(argAddress.getValue(), argPriority.getValue(), argSignalThreshold.getValue(), argSkipReply.isSet());
151+
ImageHandler handler(argAddress.getValue(), argPriority.getValue(), argSkipReply.isSet());
142152
QObject::connect(&grabber, SIGNAL(newFrame(Image<ColorRgb>)), &handler, SLOT(receiveImage(Image<ColorRgb>)));
143153
grabber.start();
144154
QCoreApplication::exec();

src/hyperiond/hyperiond.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ int main(int argc, char** argv)
184184
grabberConfig.get("height", -1).asInt(),
185185
grabberConfig.get("frameDecimation", 2).asInt(),
186186
grabberConfig.get("sizeDecimation", 8).asInt(),
187+
grabberConfig.get("redSignalThreshold", 0.0).asDouble(),
188+
grabberConfig.get("greenSignalThreshold", 0.0).asDouble(),
189+
grabberConfig.get("blueSignalThreshold", 0.0).asDouble(),
187190
&hyperion,
188191
grabberConfig.get("priority", 800).asInt());
189192
v4l2Grabber->set3D(parse3DMode(grabberConfig.get("mode", "2D").asString()));

0 commit comments

Comments
 (0)