diff --git a/include/blackborder/BlackBorderDetector.h b/include/blackborder/BlackBorderDetector.h index c5ee91e7..14cf051c 100644 --- a/include/blackborder/BlackBorderDetector.h +++ b/include/blackborder/BlackBorderDetector.h @@ -61,49 +61,45 @@ namespace hyperion template BlackBorder process(const Image & image) { - // only test the topleft third of the image - int width = image.width() /3; + + // test center and 1/3, 2/3 of width/heigth + int width = image.width() / 3; int height = image.height() / 3; - int maxSize = std::max(width, height); + int width2 = width * 2; + int height2 = height * 2; + int xCenter = image.width() / 2; + int yCenter = image.height() / 2; + int firstNonBlackXPixelIndex = -1; int firstNonBlackYPixelIndex = -1; - // find some pixel of the image - for (int i = 0; i < maxSize; ++i) + // find first X pixel of the image + for (int x = 0; x < width; ++x) { - int x = std::min(i, width); - int y = std::min(i, height); - - const Pixel_T & color = image(x, y); - if (!isBlack(color)) + 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)) { firstNonBlackXPixelIndex = x; + break; + } + } + + // find first Y pixel of the image + for (int y = 0; y < height; ++y) + { + 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)) + { 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; diff --git a/include/blackborder/BlackBorderProcessor.h b/include/blackborder/BlackBorderProcessor.h index 4d0c4fca..14be0585 100644 --- a/include/blackborder/BlackBorderProcessor.h +++ b/include/blackborder/BlackBorderProcessor.h @@ -94,5 +94,7 @@ namespace hyperion /// The number of frame the previous detected border matched the incomming border unsigned _consistentCnt; + /// The number of frame the previous detected border NOT matched the incomming border + unsigned _inconsistentCnt; }; } // end namespace hyperion diff --git a/libsrc/blackborder/BlackBorderProcessor.cpp b/libsrc/blackborder/BlackBorderProcessor.cpp index ee78efb9..2312da31 100644 --- a/libsrc/blackborder/BlackBorderProcessor.cpp +++ b/libsrc/blackborder/BlackBorderProcessor.cpp @@ -14,7 +14,8 @@ BlackBorderProcessor::BlackBorderProcessor(const unsigned unknownFrameCnt, _detector(blackborderThreshold), _currentBorder({true, -1, -1}), _previousDetectedBorder({true, -1, -1}), - _consistentCnt(0) + _consistentCnt(0), + _inconsistentCnt(0) { // empty } @@ -30,11 +31,13 @@ bool BlackBorderProcessor::updateBorder(const BlackBorder & newDetectedBorder) if (newDetectedBorder == _previousDetectedBorder) { ++_consistentCnt; + _inconsistentCnt = 0; } else { _previousDetectedBorder = newDetectedBorder; _consistentCnt = 0; + ++_inconsistentCnt; } // check if there is a change @@ -64,14 +67,18 @@ bool BlackBorderProcessor::updateBorder(const BlackBorder & newDetectedBorder) } else { - // apply smaller borders immediately - if (newDetectedBorder.verticalSize < _currentBorder.verticalSize) + bool stable = (_consistentCnt >= 10) || (_inconsistentCnt >=30 ); + //more then 10 consistent seems like a new size not only a short flicker + //more then 30 inconsistent seems like the image is changing a lot and we need to set smaller border + + // apply smaller borders (almost) immediately -> avoid switching for "abnormal" frames + if ( (newDetectedBorder.verticalSize < _currentBorder.verticalSize) && (stable) ) { _currentBorder.verticalSize = newDetectedBorder.verticalSize; borderChanged = true; } - if (newDetectedBorder.horizontalSize < _currentBorder.horizontalSize) + if ( (newDetectedBorder.horizontalSize < _currentBorder.horizontalSize) && (stable) ) { _currentBorder.horizontalSize = newDetectedBorder.horizontalSize; borderChanged = true; @@ -80,4 +87,4 @@ bool BlackBorderProcessor::updateBorder(const BlackBorder & newDetectedBorder) } return borderChanged; -} +} \ No newline at end of file