TestBlackBorderDetector.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // STL includes
  2. #include <random>
  3. // Hyperion includes
  4. #include <utils/ColorRgb.h>
  5. // Blackborder includes
  6. #include <blackborder/BlackBorderDetector.h>
  7. using namespace hyperion;
  8. ColorRgb randomColor()
  9. {
  10. const uint8_t randomRedValue = uint8_t(rand() % (std::numeric_limits<uint8_t>::max() + 1));
  11. const uint8_t randomGreenValue = uint8_t(rand() % (std::numeric_limits<uint8_t>::max() + 1));
  12. const uint8_t randomBlueValue = uint8_t(rand() % (std::numeric_limits<uint8_t>::max() + 1));
  13. return {randomRedValue, randomGreenValue, randomBlueValue};
  14. }
  15. Image<ColorRgb> createImage(unsigned width, unsigned height, unsigned topBorder, unsigned leftBorder)
  16. {
  17. Image<ColorRgb> image(width, height);
  18. for (unsigned x=0; x<image.width(); ++x)
  19. {
  20. for (unsigned y=0; y<image.height(); ++y)
  21. {
  22. if (y < topBorder || x < leftBorder)
  23. {
  24. image(x,y) = ColorRgb::BLACK;
  25. }
  26. else
  27. {
  28. image(x,y) = randomColor();
  29. }
  30. }
  31. }
  32. return image;
  33. }
  34. int TC_NO_BORDER()
  35. {
  36. int result = 0;
  37. BlackBorderDetector detector(3);
  38. {
  39. Image<ColorRgb> image = createImage(64, 64, 0, 0);
  40. BlackBorder border = detector.process(image);
  41. if (border.unknown != false && border.horizontalSize != 0 && border.verticalSize != 0)
  42. {
  43. std::cerr << "Failed to correctly detect no border" << std::endl;
  44. result = -1;
  45. }
  46. else std::cout << "Correctly detected no border" << std::endl;
  47. }
  48. return result;
  49. }
  50. int TC_TOP_BORDER()
  51. {
  52. int result = 0;
  53. BlackBorderDetector detector(3);
  54. {
  55. Image<ColorRgb> image = createImage(64, 64, 12, 0);
  56. BlackBorder border = detector.process(image);
  57. if (border.unknown != false && border.horizontalSize == 12 && border.verticalSize != 0)
  58. {
  59. std::cerr << "Failed to correctly detect horizontal border with correct size" << std::endl;
  60. result = -1;
  61. }
  62. else std::cout << "Correctly detected horizontal border with correct size" << std::endl;
  63. }
  64. return result;
  65. }
  66. int TC_LEFT_BORDER()
  67. {
  68. int result = 0;
  69. BlackBorderDetector detector(3);
  70. {
  71. Image<ColorRgb> image = createImage(64, 64, 0, 12);
  72. BlackBorder border = detector.process(image);
  73. if (border.unknown != false && border.horizontalSize != 0 && border.verticalSize == 12)
  74. {
  75. std::cerr << "Failed to correctly detect vertical border with correct size" << std::endl;
  76. result = -1;
  77. }
  78. else std::cout << "Correctly detected vertical border with correct size" << std::endl;
  79. }
  80. return result;
  81. }
  82. int TC_DUAL_BORDER()
  83. {
  84. int result = 0;
  85. BlackBorderDetector detector(3);
  86. {
  87. Image<ColorRgb> image = createImage(64, 64, 12, 12);
  88. BlackBorder border = detector.process(image);
  89. if (border.unknown != false && border.horizontalSize == 12 && border.verticalSize == 12)
  90. {
  91. std::cerr << "Failed to correctly detect two-sided border" << std::endl;
  92. result = -1;
  93. }
  94. else std::cout << "Correctly detected two-sided border" << std::endl;
  95. }
  96. return result;
  97. }
  98. int TC_UNKNOWN_BORDER()
  99. {
  100. int result = 0;
  101. BlackBorderDetector detector(3);
  102. {
  103. Image<ColorRgb> image = createImage(64, 64, 30, 30);
  104. BlackBorder border = detector.process(image);
  105. if (border.unknown != true)
  106. {
  107. std::cerr << "Failed to correctly detect unknown border" << std::endl;
  108. result = -1;
  109. }
  110. else std::cout << "Correctly detected unknown border" << std::endl;
  111. }
  112. return result;
  113. }
  114. int main()
  115. {
  116. TC_NO_BORDER();
  117. TC_TOP_BORDER();
  118. TC_LEFT_BORDER();
  119. TC_DUAL_BORDER();
  120. TC_UNKNOWN_BORDER();
  121. return 0;
  122. }