test_detection.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #include "test_precomp.hpp"
  5. #include "opencv2/imgcodecs.hpp"
  6. namespace opencv_test { namespace {
  7. // Just skip test in case of missed testdata
  8. static cv::String findDataFile(const String& path)
  9. {
  10. return cvtest::findDataFile(path, false);
  11. }
  12. PARAM_TEST_CASE(Detection, std::string, bool)
  13. {
  14. Ptr<ERFilter> er_filter1;
  15. Ptr<ERFilter> er_filter2;
  16. // SetUp doesn't handle SkipTestException
  17. void InitERFilter()
  18. {
  19. String nm1_file = findDataFile("trained_classifierNM1.xml");
  20. String nm2_file = findDataFile("trained_classifierNM2.xml");
  21. // Create ERFilter objects with the 1st and 2nd stage default classifiers
  22. er_filter1 = createERFilterNM1(loadClassifierNM1(nm1_file),16,0.00015f,0.13f,0.2f,true,0.1f);
  23. er_filter2 = createERFilterNM2(loadClassifierNM2(nm2_file),0.5);
  24. }
  25. };
  26. TEST_P(Detection, sample)
  27. {
  28. InitERFilter();
  29. std::string imageName = GET_PARAM(0);
  30. bool anyDirection = GET_PARAM(1);
  31. if (anyDirection)
  32. throw SkipTestException("ERGROUPING_ORIENTATION_ANY mode is not supported");
  33. std::cout << "Image: " << imageName << std::endl;
  34. std::cout << "Orientation: " << (anyDirection ? "any" : "horiz") << std::endl;
  35. Mat src = cv::imread(findDataFile(imageName));
  36. ASSERT_FALSE(src.empty());
  37. // Extract channels to be processed individually
  38. std::vector<Mat> channels;
  39. computeNMChannels(src, channels);
  40. // Append negative channels to detect ER- (bright regions over dark background)
  41. for (size_t c = channels.size(); c > 0; c--)
  42. channels.push_back(255 - channels[c - 1]);
  43. std::vector<std::vector<ERStat> > regions(channels.size());
  44. // Apply the default cascade classifier to each independent channel (could be done in parallel)
  45. for (size_t c = 0; c < channels.size(); c++)
  46. {
  47. er_filter1->run(channels[c], regions[c]);
  48. er_filter2->run(channels[c], regions[c]);
  49. }
  50. // Detect character groups
  51. std::vector< std::vector<Vec2i> > region_groups;
  52. std::vector<Rect> groups_boxes;
  53. if (!anyDirection)
  54. erGrouping(src, channels, regions, region_groups, groups_boxes, ERGROUPING_ORIENTATION_HORIZ);
  55. else
  56. erGrouping(src, channels, regions, region_groups, groups_boxes, ERGROUPING_ORIENTATION_ANY,
  57. findDataFile("trained_classifier_erGrouping.xml"), 0.5);
  58. std::cout << "Found groups: " << groups_boxes.size() << std::endl;
  59. EXPECT_GT(groups_boxes.size(), 3u);
  60. }
  61. INSTANTIATE_TEST_CASE_P(Text, Detection,
  62. testing::Combine(
  63. testing::Values(
  64. "text/scenetext01.jpg",
  65. "text/scenetext02.jpg",
  66. "text/scenetext03.jpg",
  67. "text/scenetext04.jpg",
  68. "text/scenetext05.jpg",
  69. "text/scenetext06.jpg"
  70. ),
  71. testing::Bool()
  72. ));
  73. }} // namespace