From 0ff4058ba425a16e3a9cae89559972eadfb2ea0d Mon Sep 17 00:00:00 2001 From: "T. van der Zwan" Date: Wed, 21 Aug 2013 20:44:17 +0000 Subject: [PATCH] Added wrapper for the blackborder detector to maintain state about detected borders. Added simple unit test for blackborder processor. --- libsrc/hyperion/BlackBorderDetector.cpp | 2 + libsrc/hyperion/BlackBorderProcessor.cpp | 57 +++++++++ libsrc/hyperion/BlackBorderProcessor.h | 30 +++++ libsrc/hyperion/CMakeLists.txt | 2 + test/CMakeLists.txt | 5 + test/TestBlackBorderProcessor.cpp | 143 +++++++++++++++++++++++ 6 files changed, 239 insertions(+) create mode 100644 libsrc/hyperion/BlackBorderProcessor.cpp create mode 100644 libsrc/hyperion/BlackBorderProcessor.h create mode 100644 test/TestBlackBorderProcessor.cpp diff --git a/libsrc/hyperion/BlackBorderDetector.cpp b/libsrc/hyperion/BlackBorderDetector.cpp index f0bcad62..6422bb42 100644 --- a/libsrc/hyperion/BlackBorderDetector.cpp +++ b/libsrc/hyperion/BlackBorderDetector.cpp @@ -1,3 +1,5 @@ + +// Local-Hyperion includes #include "BlackBorderDetector.h" BlackBorderDetector::BlackBorderDetector() diff --git a/libsrc/hyperion/BlackBorderProcessor.cpp b/libsrc/hyperion/BlackBorderProcessor.cpp new file mode 100644 index 00000000..2ad17f14 --- /dev/null +++ b/libsrc/hyperion/BlackBorderProcessor.cpp @@ -0,0 +1,57 @@ +#include "BlackBorderProcessor.h" + +BlackBorderProcessor::BlackBorderProcessor() : + _unknownSwitchCnt(600), + _borderSwitchCnt(50), + _detector(), + _currentBorder({BlackBorder::unknown, 0}), + _lastDetectedBorder({BlackBorder::unknown, 0}), + _consistentCnt(0) +{ +} + +BlackBorder BlackBorderProcessor::getCurrentBorder() const +{ + return _currentBorder; +} + +bool BlackBorderProcessor::process(const RgbImage& image) +{ + const BlackBorder imageBorder = _detector.process(image); + + if (imageBorder.type == _lastDetectedBorder.type && imageBorder.size == _lastDetectedBorder.size) + { + ++_consistentCnt; + } + else + { + _lastDetectedBorder = imageBorder; + _consistentCnt = 0; + } + + bool borderChanged = false; + switch (_lastDetectedBorder.type) + { + case BlackBorder::none: + borderChanged = (_currentBorder.type != BlackBorder::none); + _currentBorder = _lastDetectedBorder; + break; + case BlackBorder::horizontal: + case BlackBorder::vertical: + if (_consistentCnt == _borderSwitchCnt) + { + _currentBorder = _lastDetectedBorder; + borderChanged = true; + } + break; + case BlackBorder::unknown: + if (_consistentCnt == _unknownSwitchCnt) + { + _currentBorder = _lastDetectedBorder; + borderChanged = true; + } + break; + } + + return borderChanged; +} diff --git a/libsrc/hyperion/BlackBorderProcessor.h b/libsrc/hyperion/BlackBorderProcessor.h new file mode 100644 index 00000000..7ff2542b --- /dev/null +++ b/libsrc/hyperion/BlackBorderProcessor.h @@ -0,0 +1,30 @@ + +#pragma once + +// Local Hyperion includes +#include "BlackBorderDetector.h" + +class BlackBorderProcessor +{ +public: + BlackBorderProcessor(); + + BlackBorder getCurrentBorder() const; + + bool process(const RgbImage& image); + +private: + + const unsigned _unknownSwitchCnt; + + const unsigned _borderSwitchCnt; + + BlackBorderDetector _detector; + + BlackBorder _currentBorder; + + BlackBorder _lastDetectedBorder; + + unsigned _consistentCnt; +}; + diff --git a/libsrc/hyperion/CMakeLists.txt b/libsrc/hyperion/CMakeLists.txt index f997fdeb..3901d510 100644 --- a/libsrc/hyperion/CMakeLists.txt +++ b/libsrc/hyperion/CMakeLists.txt @@ -18,6 +18,7 @@ SET(Hyperion_HEADERS ${CURRENT_SOURCE_DIR}/LedDeviceWs2801.h ${CURRENT_SOURCE_DIR}/LedDeviceTest.h ${CURRENT_SOURCE_DIR}/ImageToLedsMap.h + ${CURRENT_SOURCE_DIR}/BlackBorderProcessor.h ${CURRENT_SOURCE_DIR}/BlackBorderDetector.h ${CURRENT_SOURCE_DIR}/ColorTransform.h ) @@ -32,6 +33,7 @@ SET(Hyperion_SOURCES ${CURRENT_SOURCE_DIR}/LedDeviceWs2801.cpp ${CURRENT_SOURCE_DIR}/LedDeviceTest.cpp ${CURRENT_SOURCE_DIR}/ImageToLedsMap.cpp + ${CURRENT_SOURCE_DIR}/BlackBorderProcessor.cpp ${CURRENT_SOURCE_DIR}/BlackBorderDetector.cpp ${CURRENT_SOURCE_DIR}/ColorTransform.cpp ) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 0ce25302..8a80c9c5 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -34,3 +34,8 @@ add_executable(test_blackborderdetector TestBlackBorderDetector.cpp) target_link_libraries(test_blackborderdetector hyperion) + +add_executable(test_blackborderprocessor + TestBlackBorderProcessor.cpp) +target_link_libraries(test_blackborderprocessor + hyperion) diff --git a/test/TestBlackBorderProcessor.cpp b/test/TestBlackBorderProcessor.cpp new file mode 100644 index 00000000..cfe88b6b --- /dev/null +++ b/test/TestBlackBorderProcessor.cpp @@ -0,0 +1,143 @@ + +// STL includes +#include + +// Utils includes +#include + +#include "hyperion/BlackBorderProcessor.h" + +RgbColor randomColor() +{ + const uint8_t randomRedValue = uint8_t(rand() % (std::numeric_limits::max() + 1)); + const uint8_t randomGreenValue = uint8_t(rand() % (std::numeric_limits::max() + 1)); + const uint8_t randomBlueValue = uint8_t(rand() % (std::numeric_limits::max() + 1)); + + return {randomRedValue, randomGreenValue, randomBlueValue}; +} + +RgbImage createImage(unsigned width, unsigned height, unsigned topBorder, unsigned leftBorder) +{ + RgbImage image(width, height); + for (unsigned x=0; x