TestBlackBorderProcessor.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // STL includes
  2. #include <cassert>
  3. #include <random>
  4. #include <iostream>
  5. // Utils includes
  6. #include <utils/Image.h>
  7. #include <utils/ColorRgb.h>
  8. // Blackborder includes
  9. #include "blackborder/BlackBorderProcessor.h"
  10. using namespace hyperion;
  11. ColorRgb randomColor()
  12. {
  13. const uint8_t randomRedValue = uint8_t(rand() % (std::numeric_limits<uint8_t>::max() + 1));
  14. const uint8_t randomGreenValue = uint8_t(rand() % (std::numeric_limits<uint8_t>::max() + 1));
  15. const uint8_t randomBlueValue = uint8_t(rand() % (std::numeric_limits<uint8_t>::max() + 1));
  16. return {randomRedValue, randomGreenValue, randomBlueValue};
  17. }
  18. Image<ColorRgb> createImage(unsigned width, unsigned height, unsigned topBorder, unsigned leftBorder)
  19. {
  20. Image<ColorRgb> image(width, height);
  21. for (unsigned x=0; x<image.width(); ++x)
  22. {
  23. for (unsigned y=0; y<image.height(); ++y)
  24. {
  25. if (y < topBorder || y > ( height - topBorder ) || x < leftBorder || x > (width - leftBorder) )
  26. {
  27. image(x,y) = ColorRgb::BLACK;
  28. }
  29. else
  30. {
  31. image(x,y) = randomColor();
  32. }
  33. }
  34. }
  35. return image;
  36. }
  37. int main()
  38. {
  39. // unsigned unknownCnt = 600;
  40. unsigned borderCnt = 50;
  41. // unsigned blurCnt = 0;
  42. QJsonObject config;
  43. // BlackBorderProcessor processor(unknownCnt, borderCnt, blurCnt, 3, config);
  44. BlackBorderProcessor processor(config);
  45. // Start with 'no border' detection
  46. Image<ColorRgb> noBorderImage = createImage(64, 64, 0, 0);
  47. for (unsigned i=0; i<10; ++i)
  48. {
  49. bool newBorder = processor.process(noBorderImage);
  50. if (i == 0)
  51. {
  52. // Switch to 'no border' should immediate
  53. if (!newBorder)
  54. {
  55. std::cerr << "Failed to detect 'no border' when required" << std::endl;
  56. exit(EXIT_FAILURE);
  57. }
  58. }
  59. else
  60. {
  61. if (newBorder)
  62. {
  63. std::cerr << "Incorrectly detected new border, when there in none" << std::endl;
  64. exit(EXIT_FAILURE);
  65. }
  66. }
  67. }
  68. // Verify that the border is indeed
  69. if (processor.getCurrentBorder().unknown != false || processor.getCurrentBorder().horizontalSize != 0 || processor.getCurrentBorder().verticalSize != 0)
  70. {
  71. std::cerr << "Incorrectlty identified 'no border'" << std::endl;
  72. exit(EXIT_FAILURE);
  73. }
  74. int borderSize = 12;
  75. Image<ColorRgb> horzImage = createImage(64, 64, borderSize, 0);
  76. for (unsigned i=0; i<borderCnt*2; ++i)
  77. {
  78. bool newBorder = processor.process(horzImage);
  79. if (i == borderCnt+10)// 10 frames till new border gets a chance to proof consistency
  80. {
  81. if (!newBorder)
  82. {
  83. std::cerr << "Failed to detect 'horizontal border' when required after " << borderCnt << " images" << std::endl;
  84. exit(EXIT_FAILURE);
  85. }
  86. }
  87. else
  88. {
  89. if (newBorder)
  90. {
  91. std::cerr << "Incorrectly detected new border, when there in none" << std::endl;
  92. exit(EXIT_FAILURE);
  93. }
  94. }
  95. }
  96. if (processor.getCurrentBorder().unknown != false || processor.getCurrentBorder().horizontalSize != borderSize || processor.getCurrentBorder().verticalSize != 0)
  97. {
  98. std::cerr << "Incorrectlty found 'horizontal border' (" << processor.getCurrentBorder().unknown << "," << processor.getCurrentBorder().horizontalSize << "," << processor.getCurrentBorder().verticalSize << ")" << std::endl;
  99. exit(EXIT_FAILURE);
  100. }
  101. for (unsigned i=0; i<borderCnt*2; ++i)
  102. {
  103. bool newBorder = processor.process(noBorderImage);
  104. if (i == borderCnt+10)// 10 frames till new border gets a chance to proof consistency
  105. {
  106. if (!newBorder)
  107. {
  108. std::cerr << "Failed to detect 'no border' when required after " << borderCnt << " images" << std::endl;
  109. exit(EXIT_FAILURE);
  110. }
  111. }
  112. else
  113. {
  114. if (newBorder)
  115. {
  116. std::cerr << "Incorrectly detected no border, when there in none" << std::endl;
  117. exit(EXIT_FAILURE);
  118. }
  119. }
  120. }
  121. // Check switch back to no border
  122. if ( (processor.getCurrentBorder().unknown != false || processor.getCurrentBorder().horizontalSize != 0 || processor.getCurrentBorder().verticalSize != 0))
  123. {
  124. std::cerr << "Failed to switch back to 'no border'" << std::endl;
  125. exit(EXIT_FAILURE);
  126. }
  127. Image<ColorRgb> vertImage = createImage(64, 64, 0, borderSize);
  128. for (unsigned i=0; i<borderCnt*2; ++i)
  129. {
  130. bool newBorder = processor.process(vertImage);
  131. if (i == borderCnt+10)// 10 frames till new border gets a chance to proof consistency
  132. {
  133. if (!newBorder)
  134. {
  135. std::cerr << "Failed to detect 'vertical border' when required after " << borderCnt << " images" << std::endl;
  136. exit(EXIT_FAILURE);
  137. }
  138. }
  139. else
  140. {
  141. if (newBorder)
  142. {
  143. std::cerr << "Incorrectly detected new border, when there in none" << std::endl;
  144. exit(EXIT_FAILURE);
  145. }
  146. }
  147. }
  148. if (processor.getCurrentBorder().unknown != false || processor.getCurrentBorder().horizontalSize != 0 || processor.getCurrentBorder().verticalSize != borderSize)
  149. {
  150. std::cerr << "Incorrectlty found 'vertical border'" << std::endl;
  151. exit(EXIT_FAILURE);
  152. }
  153. // Switch back (in one shot) to no border
  154. // assert(processor.process(noBorderImage));
  155. // assert(processor.getCurrentBorder().verticalSize == 0 && processor.getCurrentBorder().horizontalSize == 0);
  156. return 0;
  157. }