adjustable image2led mode for grabbers (#341)

* implement most points for a adjustable image2leds mapping

* implement new adjustable led mapping type
This commit is contained in:
redPanther
2016-12-19 23:59:50 +01:00
committed by GitHub
parent 53924c4fca
commit c5e0299c55
21 changed files with 258 additions and 47 deletions

View File

@@ -93,7 +93,7 @@ public:
///
unsigned getLedCount() const;
QSize getLedGridSize() const { return _ledGridSize; }
QSize getLedGridSize() const { return _ledGridSize; };
///
/// Returns the current priority
@@ -273,6 +273,9 @@ public slots:
/// @param timeout The timeout of the effect (after the timout, the effect will be cleared)
int setEffect(const QString & effectName, const QJsonObject & args, int priority, int timeout = -1, QString pythonScript = "");
/// sets the methode how image is maped to leds
void setLedMappingType(int mappingType);
public:
static Hyperion *_hyperion;
@@ -312,6 +315,7 @@ signals:
void componentStateChanged(const hyperion::Components component, bool enabled);
void imageToLedsMappingChanged(int mappingType);
void emitImage(int priority, const Image<ColorRgb> & image, const int timeout_ms);
private slots:

View File

@@ -1,6 +1,7 @@
#pragma once
#include <QString>
// Utils includes
#include <utils/Image.h>
@@ -18,9 +19,12 @@
/// performed in two steps. First the average color per led-region is computed. Second a
/// color-tranform is applied based on a gamma-correction.
///
class ImageProcessor
class ImageProcessor : public QObject
{
Q_OBJECT
public:
~ImageProcessor();
///
@@ -38,12 +42,23 @@ public:
///
void setSize(const unsigned width, const unsigned height);
/// Enable or disable the black border detector
void enableBlackBorderDetector(bool enable);
/// Returns starte of black border detector
bool blackBorderDetectorEnabled();
/// Returns starte of black border detector
int ledMappingType();
static int mappingTypeToInt(QString mappingType);
public slots:
/// Enable or disable the black border detector
void enableBlackBorderDetector(bool enable);
/// Enable or disable the black border detector
void setLedMappingType(int mapType);
public:
///
/// Processes the image to a list of led colors. This will update the size of the buffer-image
/// if required and call the image-to-leds mapping to determine the mean color per led.
@@ -62,7 +77,12 @@ public:
verifyBorder(image);
// Create a result vector and call the 'in place' functionl
std::vector<ColorRgb> colors = _imageToLeds->getMeanLedColor(image);
std::vector<ColorRgb> colors;
switch (_mappingType)
{
case 1: colors = _imageToLeds->getUniLedColor(image); break;
default: colors = _imageToLeds->getMeanLedColor(image);
}
// return the computed colors
return colors;
@@ -84,7 +104,12 @@ public:
verifyBorder(image);
// Determine the mean-colors of each led (using the existing mapping)
_imageToLeds->getMeanLedColor(image, ledColors);
switch (_mappingType)
{
case 1: _imageToLeds->getUniLedColor(image, ledColors); break;
default: _imageToLeds->getMeanLedColor(image, ledColors);
}
}
///
@@ -153,6 +178,7 @@ private:
}
private:
Logger * _log;
/// The Led-string specification
const LedString _ledString;
@@ -161,4 +187,7 @@ private:
/// The mapping of image-pixels to leds
hyperion::ImageToLedsMap* _imageToLeds;
/// Type of image 2 led mapping
int _mappingType;
};

View File

@@ -32,7 +32,7 @@ public:
/// @param[in] ledString The led configuration
/// @param[in] blackborderConfig Contains the blackborder configuration
///
void init(const LedString& ledString, const QJsonObject &blackborderConfig);
void init(const LedString& ledString, const QJsonObject &blackborderConfig, int mappingType);
///
/// Creates a new ImageProcessor. The onwership of the processor is transferred to the caller.
@@ -45,6 +45,9 @@ private:
/// The Led-string specification
LedString _ledString;
// Reference to the blackborder json configuration values
/// Reference to the blackborder json configuration values
QJsonObject _blackborderConfig;
// image 2 led mapping type
int _mappingType;
};

View File

@@ -70,7 +70,7 @@ namespace hyperion
template <typename Pixel_T>
std::vector<ColorRgb> getMeanLedColor(const Image<Pixel_T> & image) const
{
std::vector<ColorRgb> colors(mColorsMap.size(), ColorRgb{0,0,0});
std::vector<ColorRgb> colors(_colorsMap.size(), ColorRgb{0,0,0});
getMeanLedColor(image, colors);
return colors;
}
@@ -86,16 +86,50 @@ namespace hyperion
void getMeanLedColor(const Image<Pixel_T> & image, std::vector<ColorRgb> & ledColors) const
{
// Sanity check for the number of leds
assert(mColorsMap.size() == ledColors.size());
assert(_colorsMap.size() == ledColors.size());
// Iterate each led and compute the mean
auto led = ledColors.begin();
for (auto ledColors = mColorsMap.begin(); ledColors != mColorsMap.end(); ++ledColors, ++led)
for (auto ledColors = _colorsMap.begin(); ledColors != _colorsMap.end(); ++ledColors, ++led)
{
const ColorRgb color = calcMeanColor(image, *ledColors);
*led = color;
}
}
///
/// Determines the mean-color for each led using the mapping the image given
/// at construction.
///
/// @param[in] image The image from which to extract the led colors
///
/// @return ledColors The vector containing the output
///
template <typename Pixel_T>
std::vector<ColorRgb> getUniLedColor(const Image<Pixel_T> & image) const
{
std::vector<ColorRgb> colors(_colorsMap.size(), ColorRgb{0,0,0});
getUniLedColor(image, colors);
return colors;
}
///
/// Determines the mean color for each led using the mapping the image given
/// at construction.
///
/// @param[in] image The image from which to extract the led colors
/// @param[out] ledColors The vector containing the output
///
template <typename Pixel_T>
void getUniLedColor(const Image<Pixel_T> & image, std::vector<ColorRgb> & ledColors) const
{
// Sanity check for the number of leds
assert(_colorsMap.size() == ledColors.size());
// calculate uni color
const ColorRgb color = calcMeanColor(image);
std::fill(ledColors.begin(),ledColors.end(), color);
}
private:
/// The width of the indexed image
@@ -108,7 +142,7 @@ namespace hyperion
const unsigned _verticalBorder;
/// The absolute indices into the image for each led
std::vector<std::vector<unsigned>> mColorsMap;
std::vector<std::vector<unsigned>> _colorsMap;
///
/// Calculates the 'mean color' of the given list. This is the mean over each color-channel
@@ -147,6 +181,40 @@ namespace hyperion
// Return the computed color
return {avgRed, avgGreen, avgBlue};
}
///
/// Calculates the 'mean color' over the given image. This is the mean over each color-channel
/// (red, green, blue)
///
/// @param[in] image The image a section from which an average color must be computed
///
/// @return The mean of the given list of colors (or black when empty)
///
template <typename Pixel_T>
ColorRgb calcMeanColor(const Image<Pixel_T> & image) const
{
// Accumulate the sum of each seperate color channel
uint_fast16_t cummRed = 0;
uint_fast16_t cummGreen = 0;
uint_fast16_t cummBlue = 0;
const unsigned imageSize = image.width() * image.height();
for (unsigned idx=0; idx<imageSize; idx++)
{
const Pixel_T& pixel = image.memptr()[idx];
cummRed += pixel.red;
cummGreen += pixel.green;
cummBlue += pixel.blue;
}
// Compute the average of each color channel
const uint8_t avgRed = uint8_t(cummRed/imageSize);
const uint8_t avgGreen = uint8_t(cummGreen/imageSize);
const uint8_t avgBlue = uint8_t(cummBlue/imageSize);
// Return the computed color
return {avgRed, avgGreen, avgBlue};
}
};
} // end namespace hyperion