mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
Dominant Color support (#1569)
* Dominant Color and Mean Color Squared * Workaround - Suppress empty LED updates * Add missing text * Dominant Colors advanced * Test with fixed initial colors * Test with fixed initial colors * Support new processing values via API * ImageToLED - Add reduced pixel processing, make dominant color advanced configurable * Updates on Grabber fps setting * ImageToLedMap - Remove maptype and update test * Update dynamic cluster array allocation
This commit is contained in:
@@ -12,7 +12,7 @@
|
||||
},
|
||||
"mappingType": {
|
||||
"type" : "string",
|
||||
"enum" : ["multicolor_mean", "unicolor_mean"]
|
||||
"enum" : ["multicolor_mean", "unicolor_mean", "multicolor_mean_squared", "dominant_color", "dominant_color_advanced"]
|
||||
}
|
||||
},
|
||||
"additionalProperties": false
|
||||
|
@@ -74,9 +74,6 @@ void VideoWrapper::handleSettingsUpdate(settings::type type, const QJsonDocument
|
||||
// Device resolution
|
||||
_grabber.setWidthHeight(obj["width"].toInt(0), obj["height"].toInt(0));
|
||||
|
||||
// Device framerate
|
||||
_grabber.setFramerate(obj["fps"].toInt(15));
|
||||
|
||||
// Device encoding format
|
||||
_grabber.setEncoding(obj["encoding"].toString("NO_CHANGE"));
|
||||
|
||||
@@ -124,6 +121,11 @@ void VideoWrapper::handleSettingsUpdate(settings::type type, const QJsonDocument
|
||||
obj["blueSignalThreshold"].toDouble(0.0)/100.0,
|
||||
obj["noSignalCounterThreshold"].toInt(50));
|
||||
|
||||
// Device framerate
|
||||
_grabber.setFramerate(obj["fps"].toInt(15));
|
||||
|
||||
updateTimer(_ggrabber->getUpdateInterval());
|
||||
|
||||
// Reload the Grabber if any settings have been changed that require it
|
||||
_grabber.reload(getV4lGrabberState());
|
||||
}
|
||||
|
@@ -149,6 +149,8 @@ bool Grabber::setWidthHeight(int width, int height)
|
||||
|
||||
bool Grabber::setFramerate(int fps)
|
||||
{
|
||||
Debug(_log,"Set new frames per second to: %i fps, current fps: %i", fps, _fps);
|
||||
|
||||
if((fps > 0) && (_fps != fps))
|
||||
{
|
||||
Info(_log,"Set new frames per second to: %i fps", fps);
|
||||
|
@@ -186,7 +186,8 @@ void GrabberWrapper::updateTimer(int interval)
|
||||
}
|
||||
|
||||
void GrabberWrapper::handleSettingsUpdate(settings::type type, const QJsonDocument& config)
|
||||
{ if(type == settings::SYSTEMCAPTURE && !_grabberName.startsWith("V4L"))
|
||||
{
|
||||
if(type == settings::SYSTEMCAPTURE && !_grabberName.startsWith("V4L"))
|
||||
{
|
||||
// extract settings
|
||||
const QJsonObject& obj = config.object();
|
||||
|
@@ -4,26 +4,83 @@
|
||||
#include <hyperion/ImageProcessor.h>
|
||||
#include <hyperion/ImageToLedsMap.h>
|
||||
|
||||
// Blacborder includes
|
||||
// Blackborder includes
|
||||
#include <blackborder/BlackBorderProcessor.h>
|
||||
|
||||
#include <QSharedPointer>
|
||||
#include <QRgb>
|
||||
|
||||
using namespace hyperion;
|
||||
|
||||
void ImageProcessor::registerProcessingUnit(
|
||||
int width,
|
||||
int height,
|
||||
int horizontalBorder,
|
||||
int verticalBorder)
|
||||
{
|
||||
if (width > 0 && height > 0)
|
||||
{
|
||||
_imageToLedColors = QSharedPointer<ImageToLedsMap>(new ImageToLedsMap(
|
||||
_log,
|
||||
width,
|
||||
height,
|
||||
horizontalBorder,
|
||||
verticalBorder,
|
||||
_ledString.leds(),
|
||||
_reducedPixelSetFactorFactor,
|
||||
_accuraryLevel
|
||||
));
|
||||
}
|
||||
else
|
||||
{
|
||||
_imageToLedColors = QSharedPointer<ImageToLedsMap>(nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
// global transform method
|
||||
int ImageProcessor::mappingTypeToInt(const QString& mappingType)
|
||||
{
|
||||
if (mappingType == "unicolor_mean" )
|
||||
{
|
||||
return 1;
|
||||
|
||||
}
|
||||
else if (mappingType == "multicolor_mean_squared" )
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
else if (mappingType == "dominant_color" )
|
||||
{
|
||||
return 3;
|
||||
}
|
||||
else if (mappingType == "dominant_color_advanced" )
|
||||
{
|
||||
return 4;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
// global transform method
|
||||
QString ImageProcessor::mappingTypeToStr(int mappingType)
|
||||
{
|
||||
if (mappingType == 1 )
|
||||
return "unicolor_mean";
|
||||
QString typeText;
|
||||
switch (mappingType) {
|
||||
case 1:
|
||||
typeText = "unicolor_mean";
|
||||
break;
|
||||
case 2:
|
||||
typeText = "multicolor_mean_squared";
|
||||
break;
|
||||
case 3:
|
||||
typeText = "dominant_color";
|
||||
break;
|
||||
case 4:
|
||||
typeText = "dominant_color_advanced";
|
||||
break;
|
||||
default:
|
||||
typeText = "multicolor_mean";
|
||||
break;
|
||||
}
|
||||
|
||||
return "multicolor_mean";
|
||||
return typeText;
|
||||
}
|
||||
|
||||
ImageProcessor::ImageProcessor(const LedString& ledString, Hyperion* hyperion)
|
||||
@@ -31,10 +88,12 @@ ImageProcessor::ImageProcessor(const LedString& ledString, Hyperion* hyperion)
|
||||
, _log(nullptr)
|
||||
, _ledString(ledString)
|
||||
, _borderProcessor(new BlackBorderProcessor(hyperion, this))
|
||||
, _imageToLeds(nullptr)
|
||||
, _imageToLedColors(nullptr)
|
||||
, _mappingType(0)
|
||||
, _userMappingType(0)
|
||||
, _hardMappingType(0)
|
||||
, _hardMappingType(-1)
|
||||
, _accuraryLevel(0)
|
||||
, _reducedPixelSetFactorFactor(1)
|
||||
, _hyperion(hyperion)
|
||||
{
|
||||
QString subComponent = hyperion->property("instance").toString();
|
||||
@@ -48,7 +107,6 @@ ImageProcessor::ImageProcessor(const LedString& ledString, Hyperion* hyperion)
|
||||
|
||||
ImageProcessor::~ImageProcessor()
|
||||
{
|
||||
delete _imageToLeds;
|
||||
}
|
||||
|
||||
void ImageProcessor::handleSettingsUpdate(settings::type type, const QJsonDocument& config)
|
||||
@@ -61,39 +119,40 @@ void ImageProcessor::handleSettingsUpdate(settings::type type, const QJsonDocume
|
||||
{
|
||||
setLedMappingType(newType);
|
||||
}
|
||||
|
||||
int reducedPixelSetFactorFactor = obj["reducedPixelSetFactorFactor"].toString().toInt();
|
||||
setReducedPixelSetFactorFactor(reducedPixelSetFactorFactor);
|
||||
|
||||
int accuracyLevel = obj["accuracyLevel"].toInt();
|
||||
setAccuracyLevel(accuracyLevel);
|
||||
}
|
||||
}
|
||||
|
||||
void ImageProcessor::setSize(unsigned width, unsigned height)
|
||||
void ImageProcessor::setSize(int width, int height)
|
||||
{
|
||||
// Check if the existing buffer-image is already the correct dimensions
|
||||
if (_imageToLeds && _imageToLeds->width() == width && _imageToLeds->height() == height)
|
||||
if (!_imageToLedColors.isNull() && _imageToLedColors->width() == width && _imageToLedColors->height() == height)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Clean up the old buffer and mapping
|
||||
delete _imageToLeds;
|
||||
|
||||
// Construct a new buffer and mapping
|
||||
_imageToLeds = (width>0 && height>0) ? (new ImageToLedsMap(width, height, 0, 0, _ledString.leds())) : nullptr;
|
||||
registerProcessingUnit(width, height, 0, 0);
|
||||
}
|
||||
|
||||
void ImageProcessor::setLedString(const LedString& ledString)
|
||||
{
|
||||
if ( _imageToLeds != nullptr)
|
||||
Debug(_log,"");
|
||||
if ( !_imageToLedColors.isNull() )
|
||||
{
|
||||
_ledString = ledString;
|
||||
|
||||
// get current width/height
|
||||
unsigned width = _imageToLeds->width();
|
||||
unsigned height = _imageToLeds->height();
|
||||
|
||||
// Clean up the old buffer and mapping
|
||||
delete _imageToLeds;
|
||||
int width = _imageToLedColors->width();
|
||||
int height = _imageToLedColors->height();
|
||||
|
||||
// Construct a new buffer and mapping
|
||||
_imageToLeds = new ImageToLedsMap(width, height, 0, 0, _ledString.leds());
|
||||
registerProcessingUnit(width, height, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,15 +166,55 @@ bool ImageProcessor::blackBorderDetectorEnabled() const
|
||||
return _borderProcessor->enabled();
|
||||
}
|
||||
|
||||
void ImageProcessor::setReducedPixelSetFactorFactor(int count)
|
||||
{
|
||||
int currentReducedPixelSetFactor= _reducedPixelSetFactorFactor;
|
||||
|
||||
_reducedPixelSetFactorFactor = count;
|
||||
Debug(_log, "Set reduced pixel set factor to %d", _reducedPixelSetFactorFactor);
|
||||
|
||||
if (currentReducedPixelSetFactor != _reducedPixelSetFactorFactor && !_imageToLedColors.isNull())
|
||||
{
|
||||
int width = _imageToLedColors->width();
|
||||
int height = _imageToLedColors->height();
|
||||
|
||||
// Construct a new buffer and mapping
|
||||
registerProcessingUnit(width, height, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void ImageProcessor::setAccuracyLevel(int level)
|
||||
{
|
||||
_accuraryLevel = level;
|
||||
Debug(_log, "Set processing accuracy level to %d", _accuraryLevel);
|
||||
|
||||
if (!_imageToLedColors.isNull())
|
||||
{
|
||||
_imageToLedColors->setAccuracyLevel(_accuraryLevel);
|
||||
}
|
||||
}
|
||||
|
||||
void ImageProcessor::setLedMappingType(int mapType)
|
||||
{
|
||||
int currentMappingType = _mappingType;
|
||||
|
||||
// if the _hardMappingType is >-1 we aren't allowed to overwrite it
|
||||
_userMappingType = mapType;
|
||||
Debug(_log, "set user led mapping to %s", QSTRING_CSTR(mappingTypeToStr(mapType)));
|
||||
|
||||
Debug(_log, "Set user LED mapping to %s", QSTRING_CSTR(mappingTypeToStr(mapType)));
|
||||
|
||||
if(_hardMappingType == -1)
|
||||
{
|
||||
_mappingType = mapType;
|
||||
}
|
||||
|
||||
if (currentMappingType != _mappingType && !_imageToLedColors.isNull())
|
||||
{
|
||||
int width = _imageToLedColors->width();
|
||||
int height = _imageToLedColors->height();
|
||||
|
||||
registerProcessingUnit(width, height, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void ImageProcessor::setHardLedMappingType(int mapType)
|
||||
|
@@ -3,17 +3,26 @@
|
||||
using namespace hyperion;
|
||||
|
||||
ImageToLedsMap::ImageToLedsMap(
|
||||
unsigned width,
|
||||
unsigned height,
|
||||
unsigned horizontalBorder,
|
||||
unsigned verticalBorder,
|
||||
const std::vector<Led>& leds)
|
||||
: _width(width)
|
||||
Logger* log,
|
||||
int width,
|
||||
int height,
|
||||
int horizontalBorder,
|
||||
int verticalBorder,
|
||||
const std::vector<Led>& leds,
|
||||
int reducedPixelSetFactor,
|
||||
int accuracyLevel)
|
||||
: _log(log)
|
||||
, _width(width)
|
||||
, _height(height)
|
||||
, _horizontalBorder(horizontalBorder)
|
||||
, _verticalBorder(verticalBorder)
|
||||
, _nextPixelCount(reducedPixelSetFactor)
|
||||
, _clusterCount()
|
||||
, _colorsMap()
|
||||
{
|
||||
_nextPixelCount = reducedPixelSetFactor + 1;
|
||||
setAccuracyLevel(accuracyLevel);
|
||||
|
||||
// Sanity check of the size of the borders (and width and height)
|
||||
Q_ASSERT(_width > 2*_verticalBorder);
|
||||
Q_ASSERT(_height > 2*_horizontalBorder);
|
||||
@@ -23,10 +32,14 @@ ImageToLedsMap::ImageToLedsMap(
|
||||
// Reserve enough space in the map for the leds
|
||||
_colorsMap.reserve(leds.size());
|
||||
|
||||
const unsigned xOffset = _verticalBorder;
|
||||
const unsigned actualWidth = _width - 2 * _verticalBorder;
|
||||
const unsigned yOffset = _horizontalBorder;
|
||||
const unsigned actualHeight = _height - 2 * _horizontalBorder;
|
||||
const int xOffset = _verticalBorder;
|
||||
const int actualWidth = _width - 2 * _verticalBorder;
|
||||
const int yOffset = _horizontalBorder;
|
||||
const int actualHeight = _height - 2 * _horizontalBorder;
|
||||
|
||||
size_t totalCount = 0;
|
||||
size_t totalCapacity = 0;
|
||||
int ledCounter = 0;
|
||||
|
||||
for (const Led& led : leds)
|
||||
{
|
||||
@@ -38,10 +51,10 @@ ImageToLedsMap::ImageToLedsMap(
|
||||
}
|
||||
|
||||
// Compute the index boundaries for this led
|
||||
unsigned minX_idx = xOffset + unsigned(qRound(actualWidth * led.minX_frac));
|
||||
unsigned maxX_idx = xOffset + unsigned(qRound(actualWidth * led.maxX_frac));
|
||||
unsigned minY_idx = yOffset + unsigned(qRound(actualHeight * led.minY_frac));
|
||||
unsigned maxY_idx = yOffset + unsigned(qRound(actualHeight * led.maxY_frac));
|
||||
int minX_idx = xOffset + int32_t(qRound(actualWidth * led.minX_frac));
|
||||
int maxX_idx = xOffset + int32_t(qRound(actualWidth * led.maxX_frac));
|
||||
int minY_idx = yOffset + int32_t(qRound(actualHeight * led.minY_frac));
|
||||
int maxY_idx = yOffset + int32_t(qRound(actualHeight * led.maxY_frac));
|
||||
|
||||
// make sure that the area is at least a single led large
|
||||
minX_idx = qMin(minX_idx, xOffset + actualWidth - 1);
|
||||
@@ -56,31 +69,70 @@ ImageToLedsMap::ImageToLedsMap(
|
||||
}
|
||||
|
||||
// Add all the indices in the above defined rectangle to the indices for this led
|
||||
const auto maxYLedCount = qMin(maxY_idx, yOffset+actualHeight);
|
||||
const auto maxXLedCount = qMin(maxX_idx, xOffset+actualWidth);
|
||||
const int maxYLedCount = qMin(maxY_idx, yOffset+actualHeight);
|
||||
const int maxXLedCount = qMin(maxX_idx, xOffset+actualWidth);
|
||||
|
||||
std::vector<int32_t> ledColors;
|
||||
ledColors.reserve((size_t) maxXLedCount*maxYLedCount);
|
||||
const int realYLedCount = qAbs(maxYLedCount - minY_idx);
|
||||
const int realXLedCount = qAbs(maxXLedCount - minX_idx);
|
||||
|
||||
for (unsigned y = minY_idx; y < maxYLedCount; ++y)
|
||||
bool skipPixelProcessing {false};
|
||||
if (_nextPixelCount > 1)
|
||||
{
|
||||
for (unsigned x = minX_idx; x < maxXLedCount; ++x)
|
||||
skipPixelProcessing = true;
|
||||
}
|
||||
|
||||
size_t totalSize = static_cast<size_t>(realYLedCount * realXLedCount);
|
||||
|
||||
if (!skipPixelProcessing && totalSize > 1600)
|
||||
{
|
||||
skipPixelProcessing = true;
|
||||
_nextPixelCount = 2;
|
||||
Warning(_log, "Mapping LED/light [%d]. The current mapping area contains %d pixels which is huge. Therefore every %d pixels will be skipped. You can enable reduced processing to hide that warning.", ledCounter, totalSize, _nextPixelCount);
|
||||
}
|
||||
|
||||
std::vector<int> ledColors;
|
||||
ledColors.reserve(totalSize);
|
||||
|
||||
for (int y = minY_idx; y < maxYLedCount; y += _nextPixelCount)
|
||||
{
|
||||
for (int x = minX_idx; x < maxXLedCount; x += _nextPixelCount)
|
||||
{
|
||||
ledColors.push_back(y*width + x);
|
||||
ledColors.push_back( y * width + x);
|
||||
}
|
||||
}
|
||||
|
||||
// Add the constructed vector to the map
|
||||
_colorsMap.push_back(ledColors);
|
||||
|
||||
totalCount += ledColors.size();
|
||||
totalCapacity += ledColors.capacity();
|
||||
|
||||
ledCounter++;
|
||||
}
|
||||
Debug(_log, "Total index number is: %d (memory: %d). Reduced pixel set factor: %d, Accuracy level: %d, Image size: %d x %d, LED areas: %d",
|
||||
totalCount, totalCapacity, reducedPixelSetFactor, accuracyLevel, width, height, leds.size());
|
||||
|
||||
}
|
||||
|
||||
unsigned ImageToLedsMap::width() const
|
||||
int ImageToLedsMap::width() const
|
||||
{
|
||||
return _width;
|
||||
}
|
||||
|
||||
unsigned ImageToLedsMap::height() const
|
||||
int ImageToLedsMap::height() const
|
||||
{
|
||||
return _height;
|
||||
}
|
||||
|
||||
void ImageToLedsMap::setAccuracyLevel (int accuracyLevel)
|
||||
{
|
||||
if (accuracyLevel > 4 )
|
||||
{
|
||||
Warning(_log, "Accuracy level %d is too high, it will be set to 4", accuracyLevel);
|
||||
accuracyLevel = 4;
|
||||
}
|
||||
//Set cluster number for dominant color advanced
|
||||
_clusterCount = accuracyLevel + 1;
|
||||
|
||||
}
|
||||
|
||||
|
@@ -9,20 +9,44 @@
|
||||
"type" : "string",
|
||||
"required" : true,
|
||||
"title" : "edt_conf_color_imageToLedMappingType_title",
|
||||
"enum" : ["multicolor_mean", "unicolor_mean"],
|
||||
"enum" : ["multicolor_mean", "unicolor_mean", "multicolor_mean_squared", "dominant_color", "dominant_color_advanced"],
|
||||
"default" : "multicolor_mean",
|
||||
"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", "edt_conf_enum_dominant_color_advanced"]
|
||||
},
|
||||
"propertyOrder" : 1
|
||||
},
|
||||
"accuracyLevel": {
|
||||
"type": "integer",
|
||||
"title": "edt_conf_color_accuracyLevel_title",
|
||||
"minimum": 1,
|
||||
"maximum": 4,
|
||||
"default": 2,
|
||||
"propertyOrder": 2,
|
||||
"options": {
|
||||
"dependencies": {
|
||||
"imageToLedMappingType": "dominant_color_advanced"
|
||||
}
|
||||
}
|
||||
},
|
||||
"reducedPixelSetFactorFactor": {
|
||||
"type": "string",
|
||||
"title": "edt_conf_color_reducedPixelSetFactorFactor_title",
|
||||
"default": 0,
|
||||
"enum" : ["0", "1", "2", "3"],
|
||||
"default" : "0",
|
||||
"options" : {
|
||||
"enum_titles" : ["edt_conf_enum_disabled", "edt_conf_enum_low", "edt_conf_enum_medium", "edt_conf_enum_high"]
|
||||
},
|
||||
"propertyOrder": 3
|
||||
},
|
||||
"channelAdjustment" :
|
||||
{
|
||||
"type" : "array",
|
||||
"title" : "edt_conf_color_channelAdjustment_header_title",
|
||||
"minItems": 1,
|
||||
"required" : true,
|
||||
"propertyOrder" : 3,
|
||||
"propertyOrder" : 4,
|
||||
"items" :
|
||||
{
|
||||
"type" : "object",
|
||||
|
Reference in New Issue
Block a user