mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
Changed RgbImage to template based Image (with template for pixel type)
Former-commit-id: ef02f164eaf3c2f9dd552c1c17b525cf6eed499c
This commit is contained in:
131
include/hyperion/BlackBorderDetector.h
Normal file
131
include/hyperion/BlackBorderDetector.h
Normal file
@@ -0,0 +1,131 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
// Utils includes
|
||||
#include <utils/Image.h>
|
||||
|
||||
namespace hyperion
|
||||
{
|
||||
///
|
||||
/// Result structure of the detected blackborder.
|
||||
///
|
||||
struct BlackBorder
|
||||
{
|
||||
/// Falg indicating if the border is unknown
|
||||
bool unknown;
|
||||
|
||||
/// The size of the detected horizontal border
|
||||
int horizontalSize;
|
||||
|
||||
/// The size of the detected vertical border
|
||||
int verticalSize;
|
||||
|
||||
///
|
||||
/// Compares this BlackBorder to the given other BlackBorder
|
||||
///
|
||||
/// @param[in] other The other BlackBorder
|
||||
///
|
||||
/// @return True if this is the same border as other
|
||||
///
|
||||
inline bool operator== (const BlackBorder& other) const
|
||||
{
|
||||
if (unknown)
|
||||
{
|
||||
return other.unknown;
|
||||
}
|
||||
|
||||
return other.unknown==false && horizontalSize==other.horizontalSize && verticalSize==other.verticalSize;
|
||||
}
|
||||
};
|
||||
|
||||
///
|
||||
/// The BlackBorderDetector performs detection of black-borders on a single image.
|
||||
/// The detector will search for the upper left corner of the picture in the frame.
|
||||
/// Based on detected black pixels it will give an estimate of the black-border.
|
||||
///
|
||||
class BlackBorderDetector
|
||||
{
|
||||
public:
|
||||
///
|
||||
/// Constructs a black-border detector
|
||||
///
|
||||
BlackBorderDetector();
|
||||
|
||||
///
|
||||
/// Performs the actual black-border detection on the given image
|
||||
///
|
||||
/// @param[in] image The image on which detection is performed
|
||||
///
|
||||
/// @return The detected (or not detected) black border info
|
||||
///
|
||||
template <typename Pixel_T>
|
||||
BlackBorder process(const Image<Pixel_T> & image)
|
||||
{
|
||||
// only test the topleft third of the image
|
||||
int width = image.width() /3;
|
||||
int height = image.height() / 3;
|
||||
int maxSize = std::max(width, height);
|
||||
|
||||
int firstNonBlackXPixelIndex = -1;
|
||||
int firstNonBlackYPixelIndex = -1;
|
||||
|
||||
// find some pixel of the image
|
||||
for (int i = 0; i < maxSize; ++i)
|
||||
{
|
||||
int x = std::min(i, width);
|
||||
int y = std::min(i, height);
|
||||
|
||||
const Pixel_T & color = image(x, y);
|
||||
if (!isBlack(color))
|
||||
{
|
||||
firstNonBlackXPixelIndex = x;
|
||||
firstNonBlackYPixelIndex = y;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// expand image to the left
|
||||
for(; firstNonBlackXPixelIndex > 0; --firstNonBlackXPixelIndex)
|
||||
{
|
||||
const Pixel_T & color = image(firstNonBlackXPixelIndex-1, firstNonBlackYPixelIndex);
|
||||
if (isBlack(color))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// expand image to the top
|
||||
for(; firstNonBlackYPixelIndex > 0; --firstNonBlackYPixelIndex)
|
||||
{
|
||||
const Pixel_T & color = image(firstNonBlackXPixelIndex, firstNonBlackYPixelIndex-1);
|
||||
if (isBlack(color))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Construct result
|
||||
BlackBorder detectedBorder;
|
||||
detectedBorder.unknown = firstNonBlackXPixelIndex == -1 || firstNonBlackYPixelIndex == -1;
|
||||
detectedBorder.horizontalSize = firstNonBlackYPixelIndex;
|
||||
detectedBorder.verticalSize = firstNonBlackXPixelIndex;
|
||||
return detectedBorder;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
///
|
||||
/// Checks if a given color is considered black and therefor could be part of the border.
|
||||
///
|
||||
/// @param[in] color The color to check
|
||||
///
|
||||
/// @return True if the color is considered black else false
|
||||
///
|
||||
template <typename Pixel_T>
|
||||
inline bool isBlack(const Pixel_T & color)
|
||||
{
|
||||
// Return the simple compare of the color against black
|
||||
return color.red+color.green+color.green == 0;
|
||||
}
|
||||
};
|
||||
} // end namespace hyperion
|
96
include/hyperion/BlackBorderProcessor.h
Normal file
96
include/hyperion/BlackBorderProcessor.h
Normal file
@@ -0,0 +1,96 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
// Local Hyperion includes
|
||||
#include "BlackBorderDetector.h"
|
||||
|
||||
namespace hyperion
|
||||
{
|
||||
///
|
||||
/// The BlackBorder processor is a wrapper around the black-border detector for keeping track of
|
||||
/// detected borders and count of the type and size of detected borders.
|
||||
///
|
||||
class BlackBorderProcessor
|
||||
{
|
||||
public:
|
||||
///
|
||||
/// Constructor for the BlackBorderProcessor
|
||||
/// @param unknownFrameCnt The number of frames(images) that need to contain an unknown
|
||||
/// border before the current border is set to unknown
|
||||
/// @param borderFrameCnt The number of frames(images) that need to contain a vertical or
|
||||
/// horizontal border becomes the current border
|
||||
/// @param blurRemoveCnt The size to add to a horizontal or vertical border (because the
|
||||
/// outer pixels is blurred (black and color combined due to image scaling))
|
||||
///
|
||||
BlackBorderProcessor(
|
||||
const unsigned unknownFrameCnt,
|
||||
const unsigned borderFrameCnt,
|
||||
const unsigned blurRemoveCnt);
|
||||
|
||||
///
|
||||
/// Return the current (detected) border
|
||||
/// @return The current border
|
||||
///
|
||||
BlackBorder getCurrentBorder() const;
|
||||
|
||||
///
|
||||
/// Processes the image. This performs detecion of black-border on the given image and
|
||||
/// updates the current border accordingly. If the current border is updated the method call
|
||||
/// will return true else false
|
||||
///
|
||||
/// @param image The image to process
|
||||
///
|
||||
/// @return True if a different border was detected than the current else false
|
||||
///
|
||||
template <typename Pixel_T>
|
||||
bool process(const Image<Pixel_T> & image)
|
||||
{
|
||||
// get the border for the single image
|
||||
BlackBorder imageBorder = _detector.process(image);
|
||||
// add blur to the border
|
||||
if (imageBorder.horizontalSize > 0)
|
||||
{
|
||||
imageBorder.horizontalSize += _blurRemoveCnt;
|
||||
}
|
||||
if (imageBorder.verticalSize > 0)
|
||||
{
|
||||
imageBorder.verticalSize += _blurRemoveCnt;
|
||||
}
|
||||
|
||||
const bool borderUpdated = updateBorder(imageBorder);
|
||||
return borderUpdated;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
///
|
||||
/// Updates the current border based on the newly detected border. Returns true if the
|
||||
/// current border has changed.
|
||||
///
|
||||
/// @param newDetectedBorder The newly detected border
|
||||
/// @return True if the current border changed else false
|
||||
///
|
||||
bool updateBorder(const BlackBorder & newDetectedBorder);
|
||||
|
||||
/// The number of unknown-borders detected before it becomes the current border
|
||||
const unsigned _unknownSwitchCnt;
|
||||
|
||||
/// The number of horizontal/vertical borders detected before it becomes the current border
|
||||
const unsigned _borderSwitchCnt;
|
||||
|
||||
/// The number of pixels to increase a detected border for removing blury pixels
|
||||
unsigned _blurRemoveCnt;
|
||||
|
||||
/// The blackborder detector
|
||||
BlackBorderDetector _detector;
|
||||
|
||||
/// The current detected border
|
||||
BlackBorder _currentBorder;
|
||||
|
||||
/// The border detected in the previous frame
|
||||
BlackBorder _previousDetectedBorder;
|
||||
|
||||
/// The number of frame the previous detected border matched the incomming border
|
||||
unsigned _consistentCnt;
|
||||
};
|
||||
} // end namespace hyperion
|
@@ -8,7 +8,7 @@
|
||||
#include <QTimer>
|
||||
|
||||
// hyperion-utils includes
|
||||
#include <utils/RgbImage.h>
|
||||
#include <utils/Image.h>
|
||||
|
||||
// Hyperion includes
|
||||
#include <hyperion/LedString.h>
|
||||
@@ -76,7 +76,7 @@ public:
|
||||
/// @param[in] ledColor The color to write to the leds
|
||||
/// @param[in] timeout_ms The time the leds are set to the given color [ms]
|
||||
///
|
||||
void setColor(int priority, const RgbColor &ledColor, const int timeout_ms);
|
||||
void setColor(int priority, const ColorRgb &ledColor, const int timeout_ms);
|
||||
|
||||
///
|
||||
/// Writes the given colors to all leds for the given time and priority
|
||||
@@ -85,7 +85,7 @@ public:
|
||||
/// @param[in] ledColors The colors to write to the leds
|
||||
/// @param[in] timeout_ms The time the leds are set to the given colors [ms]
|
||||
///
|
||||
void setColors(int priority, const std::vector<RgbColor> &ledColors, const int timeout_ms);
|
||||
void setColors(int priority, const std::vector<ColorRgb> &ledColors, const int timeout_ms);
|
||||
|
||||
///
|
||||
/// Sets/Updates a part of the color transformation.
|
||||
@@ -162,7 +162,7 @@ private:
|
||||
///
|
||||
/// @param colors The colors to be transformed
|
||||
///
|
||||
void applyTransform(std::vector<RgbColor>& colors) const;
|
||||
void applyTransform(std::vector<ColorRgb>& colors) const;
|
||||
|
||||
/// The specifiation of the led frame construction and picture integration
|
||||
LedString _ledString;
|
||||
|
@@ -2,17 +2,13 @@
|
||||
#pragma once
|
||||
|
||||
// Utils includes
|
||||
#include <utils/RgbImage.h>
|
||||
#include <utils/Image.h>
|
||||
|
||||
// Hyperion includes
|
||||
#include <hyperion/ImageProcessorFactory.h>
|
||||
#include <hyperion/LedString.h>
|
||||
|
||||
// Forward class declaration
|
||||
namespace hyperion {
|
||||
class ImageToLedsMap;
|
||||
class BlackBorderProcessor;
|
||||
}
|
||||
#include <hyperion/ImageToLedsMap.h>
|
||||
#include <hyperion/BlackBorderProcessor.h>
|
||||
|
||||
///
|
||||
/// The ImageProcessor translates an RGB-image to RGB-values for the leds. The processing is
|
||||
@@ -42,7 +38,21 @@ public:
|
||||
///
|
||||
/// @return The color value per led
|
||||
///
|
||||
std::vector<RgbColor> process(const RgbImage& image);
|
||||
template <typename Pixel_T>
|
||||
std::vector<ColorRgb> process(const Image<Pixel_T>& image)
|
||||
{
|
||||
// Ensure that the buffer-image is the proper size
|
||||
setSize(image.width(), image.height());
|
||||
|
||||
// Check black border detection
|
||||
verifyBorder(image);
|
||||
|
||||
// Create a result vector and call the 'in place' functionl
|
||||
std::vector<ColorRgb> colors = mImageToLeds->getMeanLedColor(image);
|
||||
|
||||
// return the computed colors
|
||||
return colors;
|
||||
}
|
||||
|
||||
///
|
||||
/// Determines the led colors of the image in the buffer.
|
||||
@@ -50,7 +60,18 @@ public:
|
||||
/// @param[in] image The image to translate to led values
|
||||
/// @param[out] ledColors The color value per led
|
||||
///
|
||||
void process(const RgbImage& image, std::vector<RgbColor>& ledColors);
|
||||
template <typename Pixel_T>
|
||||
void process(const Image<Pixel_T>& image, std::vector<ColorRgb>& ledColors)
|
||||
{
|
||||
// Ensure that the buffer-image is the proper size
|
||||
setSize(image.width(), image.height());
|
||||
|
||||
// Check black border detection
|
||||
verifyBorder(image);
|
||||
|
||||
// Determine the mean-colors of each led (using the existing mapping)
|
||||
mImageToLeds->getMeanLedColor(image, ledColors);
|
||||
}
|
||||
|
||||
///
|
||||
/// Get the hscan and vscan parameters for a single led
|
||||
@@ -80,7 +101,33 @@ private:
|
||||
///
|
||||
/// @param[in] image The image to perform black-border detection on
|
||||
///
|
||||
void verifyBorder(const RgbImage& image);
|
||||
template <typename Pixel_T>
|
||||
void verifyBorder(const Image<Pixel_T> & image)
|
||||
{
|
||||
if(_enableBlackBorderRemoval && _borderProcessor->process(image))
|
||||
{
|
||||
std::cout << "BORDER SWITCH REQUIRED!!" << std::endl;
|
||||
|
||||
const hyperion::BlackBorder border = _borderProcessor->getCurrentBorder();
|
||||
|
||||
// Clean up the old mapping
|
||||
delete mImageToLeds;
|
||||
|
||||
if (border.unknown)
|
||||
{
|
||||
// Construct a new buffer and mapping
|
||||
mImageToLeds = new hyperion::ImageToLedsMap(image.width(), image.height(), 0, 0, mLedString.leds());
|
||||
}
|
||||
else
|
||||
{
|
||||
// Construct a new buffer and mapping
|
||||
mImageToLeds = new hyperion::ImageToLedsMap(image.width(), image.height(), border.horizontalSize, border.verticalSize, mLedString.leds());
|
||||
}
|
||||
|
||||
std::cout << "CURRENT BORDER TYPE: unknown=" << border.unknown << " hor.size=" << border.horizontalSize << " vert.size=" << border.verticalSize << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
/// The Led-string specification
|
||||
const LedString mLedString;
|
||||
@@ -89,7 +136,7 @@ private:
|
||||
bool _enableBlackBorderRemoval;
|
||||
|
||||
/// The processor for black border detection
|
||||
hyperion::BlackBorderProcessor* _borderProcessor;
|
||||
hyperion::BlackBorderProcessor * _borderProcessor;
|
||||
|
||||
/// The mapping of image-pixels to leds
|
||||
hyperion::ImageToLedsMap* mImageToLeds;
|
||||
|
144
include/hyperion/ImageToLedsMap.h
Normal file
144
include/hyperion/ImageToLedsMap.h
Normal file
@@ -0,0 +1,144 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
// STL includes
|
||||
#include <cassert>
|
||||
#include <sstream>
|
||||
|
||||
// hyperion-utils includes
|
||||
#include <utils/Image.h>
|
||||
|
||||
// hyperion includes
|
||||
#include <hyperion/LedString.h>
|
||||
|
||||
namespace hyperion
|
||||
{
|
||||
|
||||
///
|
||||
/// 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.
|
||||
///
|
||||
class ImageToLedsMap
|
||||
{
|
||||
public:
|
||||
|
||||
///
|
||||
/// 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,
|
||||
/// provided that it is row-oriented.
|
||||
/// The mapping is created purely on size (width and height). The given borders are excluded
|
||||
/// from indexing.
|
||||
///
|
||||
/// @param[in] width The width of the indexed image
|
||||
/// @param[in] height The width of the indexed image
|
||||
/// @param[in] horizontalBorder The size of the horizontal border (0=no border)
|
||||
/// @param[in] verticalBorder The size of the vertical border (0=no border)
|
||||
/// @param[in] leds The list with led specifications
|
||||
///
|
||||
ImageToLedsMap(
|
||||
const unsigned width,
|
||||
const unsigned height,
|
||||
const unsigned horizontalBorder,
|
||||
const unsigned verticalBorder,
|
||||
const std::vector<Led> & leds);
|
||||
|
||||
///
|
||||
/// Returns the width of the indexed image
|
||||
///
|
||||
/// @return The width of the indexed image [pixels]
|
||||
///
|
||||
unsigned width() const;
|
||||
|
||||
///
|
||||
/// Returns the height of the indexed image
|
||||
///
|
||||
/// @return The height of the indexed image [pixels]
|
||||
///
|
||||
unsigned height() const;
|
||||
|
||||
///
|
||||
/// 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> getMeanLedColor(const Image<Pixel_T> & image) const
|
||||
{
|
||||
std::vector<ColorRgb> colors(mColorsMap.size(), ColorRgb{0,0,0});
|
||||
getMeanLedColor(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 getMeanLedColor(const Image<Pixel_T> & image, std::vector<ColorRgb> & ledColors) const
|
||||
{
|
||||
// Sanity check for the number of leds
|
||||
assert(mColorsMap.size() == ledColors.size());
|
||||
|
||||
// Iterate each led and compute the mean
|
||||
auto led = ledColors.begin();
|
||||
for (auto ledColors = mColorsMap.begin(); ledColors != mColorsMap.end(); ++ledColors, ++led)
|
||||
{
|
||||
const ColorRgb color = calcMeanColor(image, *ledColors);
|
||||
*led = color;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
/// The width of the indexed image
|
||||
const unsigned _width;
|
||||
/// The height of the indexed image
|
||||
const unsigned _height;
|
||||
/// The absolute indices into the image for each led
|
||||
std::vector<std::vector<unsigned>> mColorsMap;
|
||||
|
||||
///
|
||||
/// Calculates the 'mean color' of the given list. 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] colors The list with colors
|
||||
///
|
||||
/// @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 std::vector<unsigned> & colors) const
|
||||
{
|
||||
if (colors.size() == 0)
|
||||
{
|
||||
return ColorRgb::BLACK;
|
||||
}
|
||||
|
||||
// Accumulate the sum of each seperate color channel
|
||||
uint_fast16_t cummRed = 0;
|
||||
uint_fast16_t cummGreen = 0;
|
||||
uint_fast16_t cummBlue = 0;
|
||||
for (const unsigned colorOffset : colors)
|
||||
{
|
||||
const Pixel_T& pixel = image.memptr()[colorOffset];
|
||||
cummRed += pixel.red;
|
||||
cummGreen += pixel.green;
|
||||
cummBlue += pixel.blue;
|
||||
}
|
||||
|
||||
// Compute the average of each color channel
|
||||
const uint8_t avgRed = uint8_t(cummRed/colors.size());
|
||||
const uint8_t avgGreen = uint8_t(cummGreen/colors.size());
|
||||
const uint8_t avgBlue = uint8_t(cummBlue/colors.size());
|
||||
|
||||
// Return the computed color
|
||||
return {avgRed, avgGreen, avgBlue};
|
||||
}
|
||||
};
|
||||
|
||||
} // end namespace hyperion
|
@@ -4,7 +4,7 @@
|
||||
#include <vector>
|
||||
|
||||
// Utility includes
|
||||
#include <utils/RgbColor.h>
|
||||
#include <utils/ColorRgb.h>
|
||||
|
||||
///
|
||||
/// Interface (pure virtual base class) for LedDevices.
|
||||
@@ -28,7 +28,7 @@ public:
|
||||
///
|
||||
/// @return Zero on success else negative
|
||||
///
|
||||
virtual int write(const std::vector<RgbColor>& ledValues) = 0;
|
||||
virtual int write(const std::vector<ColorRgb>& ledValues) = 0;
|
||||
|
||||
/// Switch the leds off
|
||||
virtual int switchOff() = 0;
|
||||
|
@@ -7,7 +7,7 @@
|
||||
#include <vector>
|
||||
|
||||
// Local includes
|
||||
#include <utils/RgbColor.h>
|
||||
#include <utils/ColorRgb.h>
|
||||
|
||||
// Forward class declarations
|
||||
namespace Json { class Value; }
|
||||
|
@@ -10,7 +10,7 @@
|
||||
#include <QMap>
|
||||
|
||||
// Utils includes
|
||||
#include <utils/RgbColor.h>
|
||||
#include <utils/ColorRgb.h>
|
||||
|
||||
// Hyperion includes
|
||||
#include <hyperion/LedDevice.h>
|
||||
@@ -34,7 +34,7 @@ public:
|
||||
/// The absolute timeout of the channel
|
||||
int64_t timeoutTime_ms;
|
||||
/// The colors for each led of the channel
|
||||
std::vector<RgbColor> ledColors;
|
||||
std::vector<ColorRgb> ledColors;
|
||||
};
|
||||
|
||||
///
|
||||
@@ -89,7 +89,7 @@ public:
|
||||
/// @param[in] ledColors The led colors of the priority channel
|
||||
/// @param[in] timeoutTime_ms The absolute timeout time of the channel
|
||||
///
|
||||
void setInput(const int priority, const std::vector<RgbColor>& ledColors, const int64_t timeoutTime_ms=-1);
|
||||
void setInput(const int priority, const std::vector<ColorRgb>& ledColors, const int64_t timeoutTime_ms=-1);
|
||||
|
||||
///
|
||||
/// Clears the specified priority channel
|
||||
|
Reference in New Issue
Block a user