File: test_detection.cpp

package info (click to toggle)
opencv 4.5.1%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 268,248 kB
  • sloc: cpp: 969,170; xml: 682,525; python: 36,732; lisp: 30,170; java: 25,155; ansic: 7,927; javascript: 5,643; objc: 2,041; sh: 935; cs: 601; perl: 494; makefile: 145
file content (91 lines) | stat: -rw-r--r-- 3,034 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.

#include "test_precomp.hpp"
#include "opencv2/imgcodecs.hpp"

namespace opencv_test { namespace {

// Just skip test in case of missed testdata
static cv::String findDataFile(const String& path)
{
    return cvtest::findDataFile(path, false);
}


PARAM_TEST_CASE(Detection, std::string, bool)
{
    Ptr<ERFilter> er_filter1;
    Ptr<ERFilter> er_filter2;

    // SetUp doesn't handle SkipTestException
    void InitERFilter()
    {
        String nm1_file = findDataFile("trained_classifierNM1.xml");
        String nm2_file = findDataFile("trained_classifierNM2.xml");

        // Create ERFilter objects with the 1st and 2nd stage default classifiers
        er_filter1 = createERFilterNM1(loadClassifierNM1(nm1_file),16,0.00015f,0.13f,0.2f,true,0.1f);
        er_filter2 = createERFilterNM2(loadClassifierNM2(nm2_file),0.5);
    }
};

TEST_P(Detection, sample)
{
    InitERFilter();

    std::string imageName = GET_PARAM(0);
    bool anyDirection = GET_PARAM(1);
    if (anyDirection)
        throw SkipTestException("ERGROUPING_ORIENTATION_ANY mode is not supported");
    std::cout << "Image: " << imageName << std::endl;
    std::cout << "Orientation: " << (anyDirection ? "any" : "horiz") << std::endl;
    Mat src = cv::imread(findDataFile(imageName));
    ASSERT_FALSE(src.empty());

    // Extract channels to be processed individually
    std::vector<Mat> channels;
    computeNMChannels(src, channels);

    // Append negative channels to detect ER- (bright regions over dark background)
    for (size_t c = channels.size(); c > 0; c--)
        channels.push_back(255 - channels[c - 1]);

    std::vector<std::vector<ERStat> > regions(channels.size());
    // Apply the default cascade classifier to each independent channel (could be done in parallel)
    for (size_t c = 0; c < channels.size(); c++)
    {
        er_filter1->run(channels[c], regions[c]);
        er_filter2->run(channels[c], regions[c]);
    }

    // Detect character groups
    std::vector< std::vector<Vec2i> > region_groups;
    std::vector<Rect> groups_boxes;
    if (!anyDirection)
        erGrouping(src, channels, regions, region_groups, groups_boxes, ERGROUPING_ORIENTATION_HORIZ);
    else
        erGrouping(src, channels, regions, region_groups, groups_boxes, ERGROUPING_ORIENTATION_ANY,
                   findDataFile("trained_classifier_erGrouping.xml"), 0.5);

    std::cout << "Found groups: " << groups_boxes.size() << std::endl;

    EXPECT_GT(groups_boxes.size(), 3u);
}

INSTANTIATE_TEST_CASE_P(Text, Detection,
    testing::Combine(
        testing::Values(
            "text/scenetext01.jpg",
            "text/scenetext02.jpg",
            "text/scenetext03.jpg",
            "text/scenetext04.jpg",
            "text/scenetext05.jpg",
            "text/scenetext06.jpg"
        ),
        testing::Bool()
    ));


}} // namespace