Added border-ignore to the image-to-leds mapping.

Added doxygen comments.
This commit is contained in:
T. van der Zwan 2013-08-21 15:24:42 +00:00
parent b880603a30
commit 7c9ac7d151
4 changed files with 102 additions and 70 deletions

View File

@ -30,7 +30,7 @@ void ImageProcessor::setSize(const unsigned width, const unsigned height)
delete mImageToLeds; delete mImageToLeds;
// Construct a new buffer and mapping // Construct a new buffer and mapping
mImageToLeds = new ImageToLedsMap(width, height, mLedString.leds()); mImageToLeds = new ImageToLedsMap(width, height, 0, 0, mLedString.leds());
} }
std::vector<RgbColor> ImageProcessor::process(const RgbImage& image) std::vector<RgbColor> ImageProcessor::process(const RgbImage& image)

View File

@ -7,21 +7,37 @@
using namespace hyperion; using namespace hyperion;
ImageToLedsMap::ImageToLedsMap(const unsigned width, const unsigned height, const std::vector<Led>& leds) : ImageToLedsMap::ImageToLedsMap(
const unsigned width,
const unsigned height,
const unsigned horizontalBorder,
const unsigned verticalBorder,
const std::vector<Led>& leds) :
_width(width), _width(width),
_height(height), _height(height),
mColorsMap() mColorsMap()
{ {
// Sanity check of the size of the borders (and width and height)
assert(width > 2*verticalBorder);
assert(height > 2*horizontalBorder);
// Reserve enough space in the map for the leds // Reserve enough space in the map for the leds
mColorsMap.reserve(leds.size()); mColorsMap.reserve(leds.size());
const unsigned xOffset = verticalBorder;
const unsigned actualWidth = width - 2 * verticalBorder;
const unsigned yOffset = horizontalBorder;
const unsigned actualHeight = height - 2 * horizontalBorder;
for (const Led& led : leds) for (const Led& led : leds)
{ {
const unsigned minX_idx = unsigned(width * led.minX_frac); // Compute the index boundaries for this led
const unsigned maxX_idx = unsigned(width * led.maxX_frac); const unsigned minX_idx = xOffset + unsigned(std::round((actualWidth-1) * led.minX_frac));
const unsigned minY_idx = unsigned(height * led.minY_frac); const unsigned maxX_idx = xOffset + unsigned(std::round((actualWidth-1) * led.maxX_frac));
const unsigned maxY_idx = unsigned(height * led.maxY_frac); const unsigned minY_idx = yOffset + unsigned(std::round((actualHeight-1) * led.minY_frac));
const unsigned maxY_idx = yOffset + unsigned(std::round((actualHeight-1) * led.maxY_frac));
// Add all the indices in the above defined rectangle to the indices for this led
std::vector<unsigned> ledColors; std::vector<unsigned> ledColors;
for (unsigned y = minY_idx; y<=maxY_idx && y<height; ++y) for (unsigned y = minY_idx; y<=maxY_idx && y<height; ++y)
{ {
@ -30,6 +46,8 @@ ImageToLedsMap::ImageToLedsMap(const unsigned width, const unsigned height, cons
ledColors.push_back(y*width + x); ledColors.push_back(y*width + x);
} }
} }
// Add the constructed vector to the map
mColorsMap.push_back(ledColors); mColorsMap.push_back(ledColors);
} }
} }
@ -56,6 +74,7 @@ void ImageToLedsMap::getMeanLedColor(const RgbImage & image, std::vector<RgbColo
// Sanity check for the number of leds // Sanity check for the number of leds
assert(mColorsMap.size() == ledColors.size()); assert(mColorsMap.size() == ledColors.size());
// Iterate each led and compute the mean
auto led = ledColors.begin(); auto led = ledColors.begin();
for (auto ledColors = mColorsMap.begin(); ledColors != mColorsMap.end(); ++ledColors, ++led) for (auto ledColors = mColorsMap.begin(); ledColors != mColorsMap.end(); ++ledColors, ++led)
{ {
@ -66,6 +85,7 @@ void ImageToLedsMap::getMeanLedColor(const RgbImage & image, std::vector<RgbColo
RgbColor ImageToLedsMap::calcMeanColor(const RgbImage & image, const std::vector<unsigned> & colors) const RgbColor ImageToLedsMap::calcMeanColor(const RgbImage & image, const std::vector<unsigned> & colors) const
{ {
// Accumulate the sum of each seperate color channel
uint_fast16_t cummRed = 0; uint_fast16_t cummRed = 0;
uint_fast16_t cummGreen = 0; uint_fast16_t cummGreen = 0;
uint_fast16_t cummBlue = 0; uint_fast16_t cummBlue = 0;
@ -77,9 +97,11 @@ RgbColor ImageToLedsMap::calcMeanColor(const RgbImage & image, const std::vector
cummBlue += color.blue; cummBlue += color.blue;
} }
// Compute the average of each color channel
const uint8_t avgRed = uint8_t(cummRed/colors.size()); const uint8_t avgRed = uint8_t(cummRed/colors.size());
const uint8_t avgGreen = uint8_t(cummGreen/colors.size()); const uint8_t avgGreen = uint8_t(cummGreen/colors.size());
const uint8_t avgBlue = uint8_t(cummBlue/colors.size()); const uint8_t avgBlue = uint8_t(cummBlue/colors.size());
// Return the computed color
return {avgRed, avgGreen, avgBlue}; return {avgRed, avgGreen, avgBlue};
} }

View File

@ -13,71 +13,81 @@
namespace hyperion namespace hyperion
{ {
class ImageToLedsMap ///
{ /// The ImageToLedsMap holds a mapping of indices into an image to leds. It can be used to
public: /// calculate the average (or mean) color per led for a specific region.
///
class ImageToLedsMap
{
public:
/** ///
* Constructs an mapping from the colors in the 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 pointers to the given image and its /// definition given in the list of leds. The map holds absolute indices to any given image,
* lifetime should never exceed that of the given image /// provided that it is row-oriented.
* /// The mapping is created purely on size (width and height). The given borders are excluded
* @param[in] image The RGB image /// from indexing.
* @param[in] leds The list with led specifications ///
*/ /// @param[in] width The width of the indexed image
ImageToLedsMap(const unsigned width, const unsigned height, const std::vector<Led> & leds); /// @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; unsigned width() const;
///
/// Returns the height of the indexed image
///
/// @return The height of the indexed image [pixels]
///
unsigned height() const; unsigned height() const;
/** ///
* Determines the mean-color for each led using the mapping the image given /// Determines the mean-color for each led using the mapping the image given
* at construction. /// at construction.
* ///
* @return ledColors The vector containing the output /// @return ledColors The vector containing the output
*/ ///
std::vector<RgbColor> getMeanLedColor(const RgbImage & image) const; std::vector<RgbColor> getMeanLedColor(const RgbImage & image) const;
/** ///
* Determines the mean-color for each led using the mapping the image given /// Determines the mean color for each led using the mapping the image given
* at construction. /// at construction.
* ///
* @param[out] ledColors The vector containing the output /// @param[out] ledColors The vector containing the output
*/ ///
void getMeanLedColor(const RgbImage & image, std::vector<RgbColor> & ledColors) const; void getMeanLedColor(const RgbImage & image, std::vector<RgbColor> & ledColors) const;
std::string toString() const private:
{ /// The width of the indexed image
std::stringstream sstream;
sstream << "ImageToLedsMap(" << _width << "x" << _height << ") [";
for (const std::vector<unsigned> imageIndices : mColorsMap)
{
sstream << "{";
for (unsigned imageIndex : imageIndices)
{
sstream << imageIndex << ";";
}
sstream << "}";
}
sstream << "]" << std::endl;
return sstream.str();
}
private:
const unsigned _width; const unsigned _width;
/// The height of the indexed image
const unsigned _height; const unsigned _height;
/// The absolute indices into the image for each led
std::vector<std::vector<unsigned> > mColorsMap; std::vector<std::vector<unsigned> > mColorsMap;
/** ///
* Finds the 'mean color' of the given list. This is the mean over each color-channel (red, /// Calculates the 'mean color' of the given list. This is the mean over each color-channel
* green, blue) /// (red, green, blue)
* ///
* @param colors The list with colors /// @param[in] colors The list with colors
* ///
* @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)
*/ ///
RgbColor calcMeanColor(const RgbImage & image, const std::vector<unsigned> & colors) const; RgbColor calcMeanColor(const RgbImage & image, const std::vector<unsigned> & colors) const;
}; };
} // end namespace hyperion } // end namespace hyperion

View File

@ -28,7 +28,7 @@ int main()
const RgbColor testColor = {64, 123, 12}; const RgbColor testColor = {64, 123, 12};
RgbImage image(64, 64, testColor); RgbImage image(64, 64, testColor);
ImageToLedsMap map(64, 64, ledString.leds()); ImageToLedsMap map(64, 64, 0, 0, ledString.leds());
std::vector<RgbColor> ledColors(ledString.leds().size()); std::vector<RgbColor> ledColors(ledString.leds().size());
map.getMeanLedColor(image, ledColors); map.getMeanLedColor(image, ledColors);