2013-08-21 16:25:27 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
// Utils includes
|
2013-11-11 10:00:37 +01:00
|
|
|
#include <utils/Image.h>
|
2013-08-21 16:25:27 +02:00
|
|
|
|
2013-08-23 07:08:44 +02:00
|
|
|
namespace hyperion
|
2013-08-21 16:25:27 +02:00
|
|
|
{
|
2013-08-21 16:52:03 +02:00
|
|
|
///
|
2013-08-23 07:08:44 +02:00
|
|
|
/// Result structure of the detected blackborder.
|
2013-08-21 16:52:03 +02:00
|
|
|
///
|
2013-08-23 07:08:44 +02:00
|
|
|
struct BlackBorder
|
2013-08-21 16:25:27 +02:00
|
|
|
{
|
2013-10-27 09:25:02 +01:00
|
|
|
/// Falg indicating if the border is unknown
|
|
|
|
bool unknown;
|
2013-08-21 16:25:27 +02:00
|
|
|
|
2013-10-27 09:25:02 +01:00
|
|
|
/// The size of the detected horizontal border
|
|
|
|
int horizontalSize;
|
2013-08-21 16:25:27 +02:00
|
|
|
|
2013-10-27 09:25:02 +01:00
|
|
|
/// The size of the detected vertical border
|
|
|
|
int verticalSize;
|
2013-08-23 18:24:10 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// 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
|
|
|
|
{
|
2013-10-27 09:25:02 +01:00
|
|
|
if (unknown)
|
2013-08-23 18:24:10 +02:00
|
|
|
{
|
2013-10-27 09:25:02 +01:00
|
|
|
return other.unknown;
|
2013-08-23 18:24:10 +02:00
|
|
|
}
|
|
|
|
|
2013-10-27 09:25:02 +01:00
|
|
|
return other.unknown==false && horizontalSize==other.horizontalSize && verticalSize==other.verticalSize;
|
2013-08-23 18:24:10 +02:00
|
|
|
}
|
2013-08-23 07:08:44 +02:00
|
|
|
};
|
2013-08-21 16:25:27 +02:00
|
|
|
|
2013-08-21 16:52:03 +02:00
|
|
|
///
|
2013-08-23 07:08:44 +02:00
|
|
|
/// The BlackBorderDetector performs detection of black-borders on a single image.
|
2013-10-27 09:25:02 +01:00
|
|
|
/// 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.
|
2013-08-21 16:52:03 +02:00
|
|
|
///
|
2013-08-23 07:08:44 +02:00
|
|
|
class BlackBorderDetector
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
///
|
|
|
|
/// Constructs a black-border detector
|
2014-01-20 20:46:38 +01:00
|
|
|
/// @param[in] blackborderThreshold The threshold which the blackborder detector should use
|
2013-08-23 07:08:44 +02:00
|
|
|
///
|
2014-01-20 20:46:38 +01:00
|
|
|
BlackBorderDetector(uint8_t blackborderThreshold);
|
2013-08-21 16:25:27 +02:00
|
|
|
|
2013-08-23 07:08:44 +02:00
|
|
|
///
|
|
|
|
/// 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
|
|
|
|
///
|
2013-11-11 10:00:37 +01:00
|
|
|
template <typename Pixel_T>
|
|
|
|
BlackBorder process(const Image<Pixel_T> & image)
|
|
|
|
{
|
2016-01-02 02:31:13 +01:00
|
|
|
|
2016-01-10 18:42:55 +01:00
|
|
|
// test center and 1/3, 2/3 of width/heigth
|
2016-01-02 02:31:13 +01:00
|
|
|
int width = image.width() / 3;
|
|
|
|
int height = image.height() / 3;
|
2016-01-04 00:20:47 +01:00
|
|
|
int width2 = width * 2;
|
|
|
|
int height2 = height * 2;
|
2016-01-02 02:31:13 +01:00
|
|
|
int xCenter = image.width() / 2;
|
|
|
|
int yCenter = image.height() / 2;
|
|
|
|
|
|
|
|
|
|
|
|
int firstNonBlackXPixelIndex = -1;
|
|
|
|
int firstNonBlackYPixelIndex = -1;
|
|
|
|
|
|
|
|
// find first X pixel of the image
|
|
|
|
for (int x = 0; x < width; ++x)
|
|
|
|
{
|
2016-01-04 00:20:47 +01:00
|
|
|
const Pixel_T & color1 = image(x, yCenter);
|
|
|
|
const Pixel_T & color2 = image(x, height);
|
|
|
|
const Pixel_T & color3 = image(x, height2);
|
|
|
|
if (!isBlack(color1) || !isBlack(color2) || !isBlack(color3))
|
2016-01-02 02:31:13 +01:00
|
|
|
{
|
|
|
|
firstNonBlackXPixelIndex = x;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// find first Y pixel of the image
|
|
|
|
for (int y = 0; y < height; ++y)
|
|
|
|
{
|
2016-01-04 00:20:47 +01:00
|
|
|
const Pixel_T & color1 = image(xCenter, y);
|
|
|
|
const Pixel_T & color2 = image(width, y);
|
|
|
|
const Pixel_T & color3 = image(width2, y);
|
|
|
|
if (!isBlack(color1) || !isBlack(color2) || !isBlack(color3))
|
2016-01-02 02:31:13 +01:00
|
|
|
{
|
|
|
|
firstNonBlackYPixelIndex = y;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-11 10:00:37 +01:00
|
|
|
// Construct result
|
|
|
|
BlackBorder detectedBorder;
|
|
|
|
detectedBorder.unknown = firstNonBlackXPixelIndex == -1 || firstNonBlackYPixelIndex == -1;
|
|
|
|
detectedBorder.horizontalSize = firstNonBlackYPixelIndex;
|
|
|
|
detectedBorder.verticalSize = firstNonBlackXPixelIndex;
|
|
|
|
return detectedBorder;
|
|
|
|
}
|
2013-08-21 16:25:27 +02:00
|
|
|
|
2013-08-23 07:08:44 +02:00
|
|
|
private:
|
2013-08-21 16:25:27 +02:00
|
|
|
|
2013-08-23 07:08:44 +02:00
|
|
|
///
|
|
|
|
/// 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
|
|
|
|
///
|
2013-11-11 10:00:37 +01:00
|
|
|
template <typename Pixel_T>
|
|
|
|
inline bool isBlack(const Pixel_T & color)
|
2013-08-23 07:08:44 +02:00
|
|
|
{
|
|
|
|
// Return the simple compare of the color against black
|
2014-03-17 11:06:30 +01:00
|
|
|
return color.red < _blackborderThreshold && color.green < _blackborderThreshold && color.blue < _blackborderThreshold;
|
2013-08-23 07:08:44 +02:00
|
|
|
}
|
2014-01-20 20:46:38 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// Threshold for the blackborder detector [0 .. 255]
|
|
|
|
const uint8_t _blackborderThreshold;
|
2013-08-23 07:08:44 +02:00
|
|
|
};
|
|
|
|
} // end namespace hyperion
|