Dominant Color and Mean Color Squared

This commit is contained in:
LordGrey 2023-01-25 21:59:31 +01:00
parent fa7a5b6b56
commit af2fa7bfd5
7 changed files with 382 additions and 97 deletions

View File

@ -279,7 +279,7 @@
"edt_conf_color_heading_title": "Color Calibration", "edt_conf_color_heading_title": "Color Calibration",
"edt_conf_color_id_expl": "User given name", "edt_conf_color_id_expl": "User given name",
"edt_conf_color_id_title": "ID", "edt_conf_color_id_title": "ID",
"edt_conf_color_imageToLedMappingType_expl": "Overwrites the LED area assignment of your LED layout if it's not \"multicolor\"", "edt_conf_color_imageToLedMappingType_expl": "Overwrites the LED area assignment of your LED layout if it's not \"Mean Color Simple\"",
"edt_conf_color_imageToLedMappingType_title": "LED area assignment", "edt_conf_color_imageToLedMappingType_title": "LED area assignment",
"edt_conf_color_leds_expl": "Assign this adjustment to all LEDs (*) or just some (0-24).", "edt_conf_color_leds_expl": "Assign this adjustment to all LEDs (*) or just some (0-24).",
"edt_conf_color_leds_title": "LED index", "edt_conf_color_leds_title": "LED index",
@ -320,6 +320,7 @@
"edt_conf_enum_color": "Color", "edt_conf_enum_color": "Color",
"edt_conf_enum_custom": "Custom", "edt_conf_enum_custom": "Custom",
"edt_conf_enum_decay": "Decay", "edt_conf_enum_decay": "Decay",
"edt_conf_enum_delay": "Delay only",
"edt_conf_enum_dl_error": "Error", "edt_conf_enum_dl_error": "Error",
"edt_conf_enum_dl_informational": "Informational", "edt_conf_enum_dl_informational": "Informational",
"edt_conf_enum_dl_nodebug": "No Debug output", "edt_conf_enum_dl_nodebug": "No Debug output",
@ -328,6 +329,7 @@
"edt_conf_enum_dl_verbose1": "Verbosity level 1", "edt_conf_enum_dl_verbose1": "Verbosity level 1",
"edt_conf_enum_dl_verbose2": "Verbosity level 2", "edt_conf_enum_dl_verbose2": "Verbosity level 2",
"edt_conf_enum_dl_verbose3": "Verbosity level 3", "edt_conf_enum_dl_verbose3": "Verbosity level 3",
"edt_conf_enum_dominant_color": "Dominant Color - per LED",
"edt_conf_enum_effect": "Effect", "edt_conf_enum_effect": "Effect",
"edt_conf_enum_gbr": "GBR", "edt_conf_enum_gbr": "GBR",
"edt_conf_enum_grb": "GRB", "edt_conf_enum_grb": "GRB",
@ -338,7 +340,8 @@
"edt_conf_enum_logsilent": "Silent", "edt_conf_enum_logsilent": "Silent",
"edt_conf_enum_logverbose": "Verbose", "edt_conf_enum_logverbose": "Verbose",
"edt_conf_enum_logwarn": "Warning", "edt_conf_enum_logwarn": "Warning",
"edt_conf_enum_multicolor_mean": "Multicolor", "edt_conf_enum_multicolor_mean": "Mean Color Simple - per LED",
"edt_conf_enum_multicolor_mean_squared": "Mean Color Squared - per LED",
"edt_conf_enum_please_select": "Please Select", "edt_conf_enum_please_select": "Please Select",
"edt_conf_enum_rbg": "RBG", "edt_conf_enum_rbg": "RBG",
"edt_conf_enum_rgb": "RGB", "edt_conf_enum_rgb": "RGB",
@ -348,7 +351,7 @@
"edt_conf_enum_transeffect_sudden": "Sudden", "edt_conf_enum_transeffect_sudden": "Sudden",
"edt_conf_enum_udp_ddp": "DDP", "edt_conf_enum_udp_ddp": "DDP",
"edt_conf_enum_udp_raw": "RAW", "edt_conf_enum_udp_raw": "RAW",
"edt_conf_enum_unicolor_mean": "Unicolor", "edt_conf_enum_unicolor_mean": "Mean Color Image - applied to all LEDs",
"edt_conf_fbs_heading_title": "Flatbuffers Server", "edt_conf_fbs_heading_title": "Flatbuffers Server",
"edt_conf_fbs_timeout_expl": "If no data is received for the given period, the component will be (soft) disabled.", "edt_conf_fbs_timeout_expl": "If no data is received for the given period, the component will be (soft) disabled.",
"edt_conf_fbs_timeout_title": "Timeout", "edt_conf_fbs_timeout_title": "Timeout",

View File

@ -98,12 +98,12 @@ public:
} }
/// ///
/// Processes the image to a list of led colors. This will update the size of the buffer-image /// 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. /// if required and call the image-to-LEDs mapping to determine the color per LED.
/// ///
/// @param[in] image The image to translate to led values /// @param[in] image The image to translate to LED values
/// ///
/// @return The color value per led /// @return The color value per LED
/// ///
template <typename Pixel_T> template <typename Pixel_T>
std::vector<ColorRgb> process(const Image<Pixel_T>& image) std::vector<ColorRgb> process(const Image<Pixel_T>& image)
@ -120,8 +120,17 @@ public:
// Create a result vector and call the 'in place' function // Create a result vector and call the 'in place' function
switch (_mappingType) switch (_mappingType)
{ {
case 1: colors = _imageToLeds->getUniLedColor(image); break; case 1:
default: colors = _imageToLeds->getMeanLedColor(image); colors = _imageToLeds->getUniLedColor(image);
break;
case 2:
colors = _imageToLeds->getMeanLedColorSqrt(image);
break;
case 3:
colors = _imageToLeds->getDominantLedColor(image);
break;
default:
colors = _imageToLeds->getMeanLedColor(image);
} }
} }
else else
@ -136,8 +145,8 @@ public:
/// ///
/// Determines the led colors of the image in the buffer. /// Determines the led colors of the image in the buffer.
/// ///
/// @param[in] image The image to translate to led values /// @param[in] image The image to translate to LED values
/// @param[out] ledColors The color value per led /// @param[out] ledColors The color value per LED
/// ///
template <typename Pixel_T> template <typename Pixel_T>
void process(const Image<Pixel_T>& image, std::vector<ColorRgb>& ledColors) void process(const Image<Pixel_T>& image, std::vector<ColorRgb>& ledColors)
@ -153,8 +162,17 @@ public:
// Determine the mean or uni colors of each led (using the existing mapping) // Determine the mean or uni colors of each led (using the existing mapping)
switch (_mappingType) switch (_mappingType)
{ {
case 1: _imageToLeds->getUniLedColor(image, ledColors); break; case 1:
default: _imageToLeds->getMeanLedColor(image, ledColors); _imageToLeds->getUniLedColor(image, ledColors);
break;
case 2:
_imageToLeds->getMeanLedColorSqrt(image, ledColors);
break;
case 3:
_imageToLeds->getDominantLedColor(image, ledColors);
break;
default:
_imageToLeds->getMeanLedColor(image, ledColors);
} }
} }
else else
@ -164,9 +182,9 @@ public:
} }
/// ///
/// Get the hscan and vscan parameters for a single led /// Get the hscan and vscan parameters for a single LED
/// ///
/// @param[in] led Index of the led /// @param[in] led Index of the LED
/// @param[out] hscanBegin begin of the hscan /// @param[out] hscanBegin begin of the hscan
/// @param[out] hscanEnd end of the hscan /// @param[out] hscanEnd end of the hscan
/// @param[out] vscanBegin begin of the hscan /// @param[out] vscanBegin begin of the hscan
@ -208,9 +226,6 @@ private:
// Construct a new buffer and mapping // Construct a new buffer and mapping
_imageToLeds = new hyperion::ImageToLedsMap(image.width(), image.height(), border.horizontalSize, border.verticalSize, _ledString.leds()); _imageToLeds = new hyperion::ImageToLedsMap(image.width(), image.height(), border.horizontalSize, border.verticalSize, _ledString.leds());
} }
//Debug(Logger::getInstance("BLACKBORDER"), "CURRENT BORDER TYPE: unknown=%d hor.size=%d vert.size=%d",
// border.unknown, border.horizontalSize, border.verticalSize );
} }
} }
@ -228,7 +243,7 @@ private:
/// The mapping of image-pixels to LEDs /// The mapping of image-pixels to LEDs
hyperion::ImageToLedsMap* _imageToLeds; hyperion::ImageToLedsMap* _imageToLeds;
/// Type of image 2 led mapping /// Type of image to LED mapping
int _mappingType; int _mappingType;
/// Type of last requested user type /// Type of last requested user type
int _userMappingType; int _userMappingType;

View File

@ -1,9 +1,10 @@
#ifndef IMAGETOLEDSMAP_H
#pragma once #define IMAGETOLEDSMAP_H
// STL includes // STL includes
#include <cassert> #include <cassert>
#include <sstream> #include <sstream>
#include <cmath>
// hyperion-utils includes // hyperion-utils includes
#include <utils/Image.h> #include <utils/Image.h>
@ -14,18 +15,17 @@
namespace hyperion namespace hyperion
{ {
/// ///
/// The ImageToLedsMap holds a mapping of indices into an image to leds. It can be used to /// The ImageToLedsMap holds a mapping of indices into an image to LEDs. It can be used to
/// calculate the average (or mean) color per led for a specific region. /// calculate the average (aka mean) or dominant color per LED for a given region.
/// ///
class ImageToLedsMap class ImageToLedsMap
{ {
public: public:
/// ///
/// Constructs an mapping from the absolute indices in an image to each led based on the border /// Constructs an mapping from the absolute indices in an image to each LED based on the border
/// definition given in the list of leds. The map holds absolute indices to any given image, /// definition given in the list of LEDs. The map holds absolute indices to any given image,
/// provided that it is row-oriented. /// provided that it is row-oriented.
/// The mapping is created purely on size (width and height). The given borders are excluded /// The mapping is created purely on size (width and height). The given borders are excluded
/// from indexing. /// from indexing.
@ -37,10 +37,10 @@ namespace hyperion
/// @param[in] leds The list with led specifications /// @param[in] leds The list with led specifications
/// ///
ImageToLedsMap( ImageToLedsMap(
const unsigned width, const int width,
const unsigned height, const int height,
const unsigned horizontalBorder, const int horizontalBorder,
const unsigned verticalBorder, const int verticalBorder,
const std::vector<Led> & leds); const std::vector<Led> & leds);
/// ///
@ -48,25 +48,25 @@ namespace hyperion
/// ///
/// @return The width of the indexed image [pixels] /// @return The width of the indexed image [pixels]
/// ///
unsigned width() const; int width() const;
/// ///
/// Returns the height of the indexed image /// Returns the height of the indexed image
/// ///
/// @return The height of the indexed image [pixels] /// @return The height of the indexed image [pixels]
/// ///
unsigned height() const; int height() const;
unsigned horizontalBorder() const { return _horizontalBorder; } int horizontalBorder() const { return _horizontalBorder; }
unsigned verticalBorder() const { return _verticalBorder; } int verticalBorder() const { return _verticalBorder; }
/// ///
/// Determines the mean color for each led using the mapping the image given /// Determines the mean color for each LED using the LED area mapping given
/// at construction. /// at construction.
/// ///
/// @param[in] image The image from which to extract the led colors /// @param[in] image The image from which to extract the led colors
/// ///
/// @return ledColors The vector containing the output /// @return The vector containing the output
/// ///
template <typename Pixel_T> template <typename Pixel_T>
std::vector<ColorRgb> getMeanLedColor(const Image<Pixel_T> & image) const std::vector<ColorRgb> getMeanLedColor(const Image<Pixel_T> & image) const
@ -77,17 +77,15 @@ namespace hyperion
} }
/// ///
/// Determines the mean color for each led using the mapping the image given /// Determines the mean color for each LED using the LED area mapping given
/// at construction. /// at construction.
/// ///
/// @param[in] image The image from which to extract the led colors /// @param[in] image The image from which to extract the LED colors
/// @param[out] ledColors The vector containing the output /// @param[out] ledColors The vector containing the output
/// ///
template <typename Pixel_T> template <typename Pixel_T>
void getMeanLedColor(const Image<Pixel_T> & image, std::vector<ColorRgb> & ledColors) const void getMeanLedColor(const Image<Pixel_T> & image, std::vector<ColorRgb> & ledColors) const
{ {
// Sanity check for the number of leds
//assert(_colorsMap.size() == ledColors.size());
if(_colorsMap.size() != ledColors.size()) if(_colorsMap.size() != ledColors.size())
{ {
Debug(Logger::getInstance("HYPERION"), "ImageToLedsMap: colorsMap.size != ledColors.size -> %d != %d", _colorsMap.size(), ledColors.size()); Debug(Logger::getInstance("HYPERION"), "ImageToLedsMap: colorsMap.size != ledColors.size -> %d != %d", _colorsMap.size(), ledColors.size());
@ -104,12 +102,52 @@ namespace hyperion
} }
/// ///
/// Determines the uni color for each led using the mapping the image given /// Determines the mean color squared for each LED using the LED area mapping given
/// at construction. /// at construction.
/// ///
/// @param[in] image The image from which to extract the led colors /// @param[in] image The image from which to extract the led colors
/// ///
/// @return ledColors The vector containing the output /// @return The vector containing the output
///
template <typename Pixel_T>
std::vector<ColorRgb> getMeanLedColorSqrt(const Image<Pixel_T> & image) const
{
std::vector<ColorRgb> colors(_colorsMap.size(), ColorRgb{0,0,0});
getMeanLedColorSqrt(image, colors);
return colors;
}
///
/// Determines the mean color squared for each LED using the LED area mapping 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 getMeanLedColorSqrt(const Image<Pixel_T> & image, std::vector<ColorRgb> & ledColors) const
{
if(_colorsMap.size() != ledColors.size())
{
Debug(Logger::getInstance("HYPERION"), "ImageToLedsMap: colorsMap.size != ledColors.size -> %d != %d", _colorsMap.size(), ledColors.size());
return;
}
// Iterate each led and compute the mean
auto led = ledColors.begin();
for (auto colors = _colorsMap.begin(); colors != _colorsMap.end(); ++colors, ++led)
{
const ColorRgb color = calcMeanColorSqrt(image, *colors);
*led = color;
}
}
///
/// Determines the mean color of the image and assigns it to all LEDs
///
/// @param[in] image The image from which to extract the led color
///
/// @return The vector containing the output
/// ///
template <typename Pixel_T> template <typename Pixel_T>
std::vector<ColorRgb> getUniLedColor(const Image<Pixel_T> & image) const std::vector<ColorRgb> getUniLedColor(const Image<Pixel_T> & image) const
@ -120,57 +158,95 @@ namespace hyperion
} }
/// ///
/// Determines the uni color for each led using the mapping the image given /// Determines the mean color of the image and assigns it to all LEDs
/// at construction.
/// ///
/// @param[in] image The image from which to extract the led colors /// @param[in] image The image from which to extract the LED colors
/// @param[out] ledColors The vector containing the output /// @param[out] ledColors The vector containing the output
/// ///
template <typename Pixel_T> template <typename Pixel_T>
void getUniLedColor(const Image<Pixel_T> & image, std::vector<ColorRgb> & ledColors) const void getUniLedColor(const Image<Pixel_T> & image, std::vector<ColorRgb> & ledColors) const
{ {
// Sanity check for the number of leds
// assert(_colorsMap.size() == ledColors.size());
if(_colorsMap.size() != ledColors.size()) if(_colorsMap.size() != ledColors.size())
{ {
Debug(Logger::getInstance("HYPERION"), "ImageToLedsMap: colorsMap.size != ledColors.size -> %d != %d", _colorsMap.size(), ledColors.size()); Debug(Logger::getInstance("HYPERION"), "ImageToLedsMap: colorsMap.size != ledColors.size -> %d != %d", _colorsMap.size(), ledColors.size());
return; return;
} }
// calculate uni color // calculate uni color
const ColorRgb color = calcMeanColor(image); const ColorRgb color = calcMeanColor(image);
//Update all LEDs with same color
std::fill(ledColors.begin(),ledColors.end(), color); std::fill(ledColors.begin(),ledColors.end(), color);
} }
///
/// Determines the dominant color for each LED using the LED area mapping given
/// at construction.
///
/// @param[in] image The image from which to extract the LED color
///
/// @return The vector containing the output
///
template <typename Pixel_T>
std::vector<ColorRgb> getDominantLedColor(const Image<Pixel_T> & image) const
{
std::vector<ColorRgb> colors(_colorsMap.size(), ColorRgb{0,0,0});
getDominantLedColor(image, colors);
return colors;
}
///
/// Determines the dominant color for each LED using the LED area mapping 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 getDominantLedColor(const Image<Pixel_T> & image, std::vector<ColorRgb> & ledColors) const
{
// Sanity check for the number of LEDs
if(_colorsMap.size() != ledColors.size())
{
Debug(Logger::getInstance("HYPERION"), "ImageToLedsMap: colorsMap.size != ledColors.size -> %d != %d", _colorsMap.size(), ledColors.size());
return;
}
// Iterate each led and compute the dominant color
auto led = ledColors.begin();
for (auto colors = _colorsMap.begin(); colors != _colorsMap.end(); ++colors, ++led)
{
const ColorRgb color = calculateDominantColor(image, *colors);
*led = color;
}
}
private: private:
/// The width of the indexed image /// The width of the indexed image
const unsigned _width; const int _width;
/// The height of the indexed image /// The height of the indexed image
const unsigned _height; const int _height;
const unsigned _horizontalBorder; const int _horizontalBorder;
const unsigned _verticalBorder; const int _verticalBorder;
/// The absolute indices into the image for each led /// The absolute indices into the image for each led
std::vector<std::vector<int32_t>> _colorsMap; std::vector<std::vector<int>> _colorsMap;
/// ///
/// Calculates the 'mean color' of the given list. This is the mean over each color-channel /// Calculates the 'mean color' over the given image. This is the mean over each color-channel
/// (red, green, blue) /// (red, green, blue)
/// ///
/// @param[in] image The image a section from which an average color must be computed /// @param[in] image The image a section from which an average color must be computed
/// @param[in] colors The list with colors /// @param[in] pixels The list of pixel indices for the given image to be evaluated///
/// ///
/// @return The mean of the given list of colors (or black when empty) /// @return The mean of the given list of colors (or black when empty)
/// ///
template <typename Pixel_T> template <typename Pixel_T>
ColorRgb calcMeanColor(const Image<Pixel_T> & image, const std::vector<int32_t> & colors) const ColorRgb calcMeanColor(const Image<Pixel_T> & image, const std::vector<int32_t> & pixels) const
{ {
const auto colorVecSize = colors.size(); const auto pixelNum = pixels.size();
if (pixelNum == 0)
if (colorVecSize == 0)
{ {
return ColorRgb::BLACK; return ColorRgb::BLACK;
} }
@ -179,20 +255,20 @@ namespace hyperion
uint_fast32_t cummRed = 0; uint_fast32_t cummRed = 0;
uint_fast32_t cummGreen = 0; uint_fast32_t cummGreen = 0;
uint_fast32_t cummBlue = 0; uint_fast32_t cummBlue = 0;
const auto& imgData = image.memptr();
for (const unsigned colorOffset : colors) const auto& imgData = image.memptr();
for (const int pixelOffset : pixels)
{ {
const auto& pixel = imgData[colorOffset]; const auto& pixel = imgData[pixelOffset];
cummRed += pixel.red; cummRed += pixel.red;
cummGreen += pixel.green; cummGreen += pixel.green;
cummBlue += pixel.blue; cummBlue += pixel.blue;
} }
// Compute the average of each color channel // Compute the average of each color channel
const uint8_t avgRed = uint8_t(cummRed/colorVecSize); const uint8_t avgRed = uint8_t(cummRed/pixelNum);
const uint8_t avgGreen = uint8_t(cummGreen/colorVecSize); const uint8_t avgGreen = uint8_t(cummGreen/pixelNum);
const uint8_t avgBlue = uint8_t(cummBlue/colorVecSize); const uint8_t avgBlue = uint8_t(cummBlue/pixelNum);
// Return the computed color // Return the computed color
return {avgRed, avgGreen, avgBlue}; return {avgRed, avgGreen, avgBlue};
@ -213,11 +289,11 @@ namespace hyperion
uint_fast32_t cummRed = 0; uint_fast32_t cummRed = 0;
uint_fast32_t cummGreen = 0; uint_fast32_t cummGreen = 0;
uint_fast32_t cummBlue = 0; uint_fast32_t cummBlue = 0;
const unsigned imageSize = image.width() * image.height();
const unsigned pixelNum = image.width() * image.height();
const auto& imgData = image.memptr(); const auto& imgData = image.memptr();
for (unsigned idx=0; idx<imageSize; idx++) for (unsigned idx=0; idx<pixelNum; idx++)
{ {
const auto& pixel = imgData[idx]; const auto& pixel = imgData[idx];
cummRed += pixel.red; cummRed += pixel.red;
@ -226,13 +302,152 @@ namespace hyperion
} }
// Compute the average of each color channel // Compute the average of each color channel
const uint8_t avgRed = uint8_t(cummRed/imageSize); const uint8_t avgRed = uint8_t(cummRed/pixelNum);
const uint8_t avgGreen = uint8_t(cummGreen/imageSize); const uint8_t avgGreen = uint8_t(cummGreen/pixelNum);
const uint8_t avgBlue = uint8_t(cummBlue/imageSize); const uint8_t avgBlue = uint8_t(cummBlue/pixelNum);
// Return the computed color // Return the computed color
return {avgRed, avgGreen, avgBlue}; return {avgRed, avgGreen, avgBlue};
} }
///
/// Calculates the 'mean color' squared 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
/// @param[in] pixels The list of pixel indices for the given image to be evaluated
///
/// @return The mean of the given list of colors (or black when empty)
///
template <typename Pixel_T>
ColorRgb calcMeanColorSqrt(const Image<Pixel_T> & image, const std::vector<int32_t> & pixels) const
{
const auto pixelNum = pixels.size();
if (pixelNum == 0)
{
return ColorRgb::BLACK;
}
// Accumulate the squared sum of each separate color channel
uint_fast32_t cummRed = 0;
uint_fast32_t cummGreen = 0;
uint_fast32_t cummBlue = 0;
const auto& imgData = image.memptr();
for (const int colorOffset : pixels)
{
const auto& pixel = imgData[colorOffset];
cummRed += pixel.red * pixel.red;
cummGreen += pixel.green * pixel.green;
cummBlue += pixel.blue * pixel.blue;
}
// Compute the average of each color channel
const uint8_t avgRed = uint8_t(std::min(std::lround(sqrt(static_cast<double>(cummRed/pixelNum))), 255L));
const uint8_t avgGreen = uint8_t(std::min(std::lround(sqrt(static_cast<double>(cummGreen/pixelNum))), 255L));
const uint8_t avgBlue = uint8_t(std::min(std::lround(sqrt(static_cast<double>(cummBlue/pixelNum))), 255L));
// Return the computed color
return {avgRed, avgGreen, avgBlue};
}
///
/// Calculates the 'mean color' squared 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 calcMeanColorSqrt(const Image<Pixel_T> & image) const
{
// Accumulate the squared sum of each separate color channel
uint_fast32_t cummRed = 0;
uint_fast32_t cummGreen = 0;
uint_fast32_t cummBlue = 0;
const unsigned pixelNum = image.width() * image.height();
const auto& imgData = image.memptr();
for (int idx=0; idx<pixelNum; ++idx)
{
const auto& pixel = imgData[idx];
cummRed += pixel.red * pixel.red;
cummGreen += pixel.green * pixel.green;
cummBlue += pixel.blue * pixel.blue;
}
// Compute the average of each color channel
const uint8_t avgRed = uint8_t(std::lround(sqrt(static_cast<double>(cummRed/pixelNum))));
const uint8_t avgGreen = uint8_t(std::lround(sqrt(static_cast<double>(cummGreen/pixelNum))));
const uint8_t avgBlue = uint8_t(std::lround(sqrt(static_cast<double>(cummBlue/pixelNum))));
// Return the computed color
return {avgRed, avgGreen, avgBlue};
}
///
/// Calculates the 'dominant color' of an image area defined by a list of pixel indices
///
/// @param[in] image The image for which a dominant color is to be computed
/// @param[in] pixels The list of pixel indices for the given image to be evaluated
///
/// @return The image area's dominant color or black, if no pixel indices provided
///
template <typename Pixel_T>
ColorRgb calculateDominantColor(const Image<Pixel_T> & image, const std::vector<int> & pixels) const
{
ColorRgb dominantColor {ColorRgb::BLACK};
const auto pixelNum = pixels.size();
if (pixelNum > 0)
{
const auto& imgData = image.memptr();
QMap<QRgb,int> colorDistributionMap;
int count = 0;
for (const int pixelOffset : pixels)
{
QRgb color = imgData[pixelOffset].rgb();
if (colorDistributionMap.contains(color)) {
colorDistributionMap[color] = colorDistributionMap[color] + 1;
}
else {
colorDistributionMap[color] = 1;
}
int colorsFound = colorDistributionMap[color];
if (colorsFound > count) {
dominantColor.setRgb(color);
count = colorsFound;
}
}
}
return dominantColor;
}
///
/// Calculates the 'dominant color' of an image
///
/// @param[in] image The image for which a dominant color is to be computed
///
/// @return The image's dominant color
///
template <typename Pixel_T>
ColorRgb calculateDominantColor(const Image<Pixel_T> & image) const
{
const unsigned pixelNum = image.width() * image.height();
std::vector<int> pixels(pixelNum);
std::iota(pixels.begin(), pixels.end(), 0);
return calculateDominantColor(image, pixels);
}
}; };
} // end namespace hyperion } // end namespace hyperion
#endif // IMAGETOLEDSMAP_H

View File

@ -6,6 +6,7 @@
#include <QString> #include <QString>
#include <QTextStream> #include <QTextStream>
#include <QRgb>
/// ///
/// Plain-Old-Data structure containing the red-green-blue color specification. Size of the /// Plain-Old-Data structure containing the red-green-blue color specification. Size of the
@ -52,6 +53,18 @@ struct ColorRgb
return a; return a;
} }
QRgb rgb() const
{
return qRgb(red,green,blue);
}
void setRgb(QRgb rgb)
{
red = static_cast<uint8_t>(qRed(rgb));
green = static_cast<uint8_t>(qGreen(rgb));
blue = static_cast<uint8_t>(qBlue(rgb));
}
QString toQString() const QString toQString() const
{ {
return QString("(%1,%2,%3)").arg(red).arg(green).arg(blue); return QString("(%1,%2,%3)").arg(red).arg(green).arg(blue);

View File

@ -7,23 +7,47 @@
// Blacborder includes // Blacborder includes
#include <blackborder/BlackBorderProcessor.h> #include <blackborder/BlackBorderProcessor.h>
#include <QRgb>
using namespace hyperion; using namespace hyperion;
// global transform method // global transform method
int ImageProcessor::mappingTypeToInt(const QString& mappingType) int ImageProcessor::mappingTypeToInt(const QString& mappingType)
{ {
if (mappingType == "unicolor_mean" ) if (mappingType == "unicolor_mean" )
{
return 1; return 1;
}
else if (mappingType == "multicolor_mean_squared" )
{
return 2;
}
else if (mappingType == "dominant_color" )
{
return 3;
}
return 0; return 0;
} }
// global transform method // global transform method
QString ImageProcessor::mappingTypeToStr(int mappingType) QString ImageProcessor::mappingTypeToStr(int mappingType)
{ {
if (mappingType == 1 ) QString typeText;
return "unicolor_mean"; switch (mappingType) {
case 1:
typeText = "unicolor_mean";
break;
case 2:
typeText = "multicolor_mean_squared";
break;
case 3:
typeText = "dominant_color";
break;
default:
typeText = "multicolor_mean";
break;
}
return "multicolor_mean"; return typeText;
} }
ImageProcessor::ImageProcessor(const LedString& ledString, Hyperion* hyperion) ImageProcessor::ImageProcessor(const LedString& ledString, Hyperion* hyperion)

View File

@ -3,10 +3,10 @@
using namespace hyperion; using namespace hyperion;
ImageToLedsMap::ImageToLedsMap( ImageToLedsMap::ImageToLedsMap(
unsigned width, int width,
unsigned height, int height,
unsigned horizontalBorder, int horizontalBorder,
unsigned verticalBorder, int verticalBorder,
const std::vector<Led>& leds) const std::vector<Led>& leds)
: _width(width) : _width(width)
, _height(height) , _height(height)
@ -23,10 +23,13 @@ ImageToLedsMap::ImageToLedsMap(
// Reserve enough space in the map for the leds // Reserve enough space in the map for the leds
_colorsMap.reserve(leds.size()); _colorsMap.reserve(leds.size());
const unsigned xOffset = _verticalBorder; const int xOffset = _verticalBorder;
const unsigned actualWidth = _width - 2 * _verticalBorder; const int actualWidth = _width - 2 * _verticalBorder;
const unsigned yOffset = _horizontalBorder; const int yOffset = _horizontalBorder;
const unsigned actualHeight = _height - 2 * _horizontalBorder; const int actualHeight = _height - 2 * _horizontalBorder;
size_t totalCount = 0;
size_t totalCapacity = 0;
for (const Led& led : leds) for (const Led& led : leds)
{ {
@ -38,10 +41,10 @@ ImageToLedsMap::ImageToLedsMap(
} }
// Compute the index boundaries for this led // Compute the index boundaries for this led
unsigned minX_idx = xOffset + unsigned(qRound(actualWidth * led.minX_frac)); int minX_idx = xOffset + int32_t(qRound(actualWidth * led.minX_frac));
unsigned maxX_idx = xOffset + unsigned(qRound(actualWidth * led.maxX_frac)); int maxX_idx = xOffset + int32_t(qRound(actualWidth * led.maxX_frac));
unsigned minY_idx = yOffset + unsigned(qRound(actualHeight * led.minY_frac)); int minY_idx = yOffset + int32_t(qRound(actualHeight * led.minY_frac));
unsigned maxY_idx = yOffset + unsigned(qRound(actualHeight * led.maxY_frac)); int maxY_idx = yOffset + int32_t(qRound(actualHeight * led.maxY_frac));
// make sure that the area is at least a single led large // make sure that the area is at least a single led large
minX_idx = qMin(minX_idx, xOffset + actualWidth - 1); minX_idx = qMin(minX_idx, xOffset + actualWidth - 1);
@ -56,15 +59,20 @@ ImageToLedsMap::ImageToLedsMap(
} }
// Add all the indices in the above defined rectangle to the indices for this led // Add all the indices in the above defined rectangle to the indices for this led
const auto maxYLedCount = qMin(maxY_idx, yOffset+actualHeight); const int maxYLedCount = qMin(maxY_idx, yOffset+actualHeight);
const auto maxXLedCount = qMin(maxX_idx, xOffset+actualWidth); const int maxXLedCount = qMin(maxX_idx, xOffset+actualWidth);
std::vector<int32_t> ledColors; const int realYLedCount = qAbs(maxYLedCount - minY_idx);
ledColors.reserve((size_t) maxXLedCount*maxYLedCount); const int realXLedCount = qAbs(maxXLedCount - minX_idx);
for (unsigned y = minY_idx; y < maxYLedCount; ++y) size_t totalSize = realYLedCount* realXLedCount;
std::vector<int> ledColors;
ledColors.reserve(totalSize);
for (int y = minY_idx; y < maxYLedCount; ++y)
{ {
for (unsigned x = minX_idx; x < maxXLedCount; ++x) for (int x = minX_idx; x < maxXLedCount; ++x)
{ {
ledColors.push_back( y * width + x); ledColors.push_back( y * width + x);
} }
@ -72,15 +80,22 @@ ImageToLedsMap::ImageToLedsMap(
// Add the constructed vector to the map // Add the constructed vector to the map
_colorsMap.push_back(ledColors); _colorsMap.push_back(ledColors);
totalCount += ledColors.size();
totalCapacity += ledColors.capacity();
} }
Debug(Logger::getInstance("HYPERION"), "Total index number is: %d (memory: %d). image size: %d x %d, LED areas: %d",
totalCount, totalCapacity, width, height, leds.size());
} }
unsigned ImageToLedsMap::width() const int ImageToLedsMap::width() const
{ {
return _width; return _width;
} }
unsigned ImageToLedsMap::height() const int ImageToLedsMap::height() const
{ {
return _height; return _height;
} }

View File

@ -9,10 +9,10 @@
"type" : "string", "type" : "string",
"required" : true, "required" : true,
"title" : "edt_conf_color_imageToLedMappingType_title", "title" : "edt_conf_color_imageToLedMappingType_title",
"enum" : ["multicolor_mean", "unicolor_mean"], "enum" : ["multicolor_mean", "unicolor_mean", "multicolor_mean_squared", "dominant_color"],
"default" : "multicolor_mean", "default" : "multicolor_mean",
"options" : { "options" : {
"enum_titles" : ["edt_conf_enum_multicolor_mean", "edt_conf_enum_unicolor_mean"] "enum_titles" : ["edt_conf_enum_multicolor_mean", "edt_conf_enum_unicolor_mean", "edt_conf_enum_multicolor_mean_squared", "edt_conf_enum_dominant_color"]
}, },
"propertyOrder" : 1 "propertyOrder" : 1
}, },