File: gapi_imgproc_tests_common.hpp

package info (click to toggle)
opencv 4.10.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 282,092 kB
  • sloc: cpp: 1,178,079; xml: 682,621; python: 49,092; lisp: 31,150; java: 25,469; ansic: 11,039; javascript: 6,085; sh: 1,214; cs: 601; perl: 494; objc: 210; makefile: 173
file content (197 lines) | stat: -rw-r--r-- 8,671 bytes parent folder | download | duplicates (2)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// 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.
//
// Copyright (C) 2020 Intel Corporation

#ifndef OPENCV_GAPI_IMGPROC_TESTS_COMMON_HPP
#define OPENCV_GAPI_IMGPROC_TESTS_COMMON_HPP

#include "gapi_tests_common.hpp"
#include "../../include/opencv2/gapi/imgproc.hpp"

#include <opencv2/imgproc.hpp>

namespace opencv_test
{
// Draw random ellipses on given cv::Mat of given size and type
static void initMatForFindingContours(cv::Mat& mat, const cv::Size& sz, const int type)
{
    cv::RNG& rng = theRNG();
    mat = cv::Mat(sz, type, cv::Scalar::all(0));
    const size_t numEllipses = rng.uniform(1, 10);

    for( size_t i = 0; i < numEllipses; i++ )
    {
        cv::Point center;
        cv::Size  axes;
        center.x    = rng.uniform(0, sz.width);
        center.y    = rng.uniform(0, sz.height);
        axes.width  = rng.uniform(2, sz.width);
        axes.height = rng.uniform(2, sz.height);
        const int    color = rng.uniform(1, 256);
        const double angle = rng.uniform(0., 180.);
        cv::ellipse(mat, center, axes, angle, 0., 360., color, 1, FILLED);
    }
}

enum OptionalFindContoursOutput {NONE, HIERARCHY};

template<OptionalFindContoursOutput optional = NONE>
cv::GComputation findContoursTestGAPI(const cv::Mat& in, const cv::RetrievalModes mode,
                                      const cv::ContourApproximationModes method,
                                      cv::GCompileArgs&& args,
                                      std::vector<std::vector<cv::Point>>& out_cnts_gapi,
                                      std::vector<cv::Vec4i>& /*out_hier_gapi*/,
                                      const cv::Point& offset = cv::Point())
{
    cv::GMat g_in;
    cv::GOpaque<cv::Point> gOffset;
    cv::GArray<cv::GArray<cv::Point>> outCts;
    outCts = cv::gapi::findContours(g_in, mode, method, gOffset);
    cv::GComputation c(GIn(g_in, gOffset), GOut(outCts));
    c.apply(gin(in, offset), gout(out_cnts_gapi), std::move(args));
    return c;
}

template<> cv::GComputation findContoursTestGAPI<HIERARCHY> (
    const cv::Mat& in, const cv::RetrievalModes mode, const cv::ContourApproximationModes method,
    cv::GCompileArgs&& args, std::vector<std::vector<cv::Point>>& out_cnts_gapi,
    std::vector<cv::Vec4i>& out_hier_gapi, const cv::Point& offset)
{
    cv::GMat g_in;
    cv::GOpaque<cv::Point> gOffset;
    cv::GArray<cv::GArray<cv::Point>> outCts;
    cv::GArray<cv::Vec4i> outHier;
    std::tie(outCts, outHier) = cv::gapi::findContoursH(g_in, mode, method, gOffset);
    cv::GComputation c(GIn(g_in, gOffset), GOut(outCts, outHier));
    c.apply(gin(in, offset), gout(out_cnts_gapi, out_hier_gapi), std::move(args));
    return c;
}

template<OptionalFindContoursOutput optional = NONE>
void findContoursTestOpenCVCompare(const cv::Mat& in, const cv::RetrievalModes mode,
                                   const cv::ContourApproximationModes method,
                                   const std::vector<std::vector<cv::Point>>& out_cnts_gapi,
                                   const std::vector<cv::Vec4i>&              out_hier_gapi,
                                   const CompareMats& cmpF, const cv::Point& offset = cv::Point())
{
    // OpenCV code /////////////////////////////////////////////////////////////
    std::vector<std::vector<cv::Point>> out_cnts_ocv;
    std::vector<cv::Vec4i>              out_hier_ocv;
    cv::findContours(in, out_cnts_ocv, out_hier_ocv, mode, method, offset);
    // Comparison //////////////////////////////////////////////////////////////
    EXPECT_TRUE(out_cnts_gapi.size() == out_cnts_ocv.size());

    cv::Mat out_mat_ocv  = cv::Mat(cv::Size{ in.cols, in.rows }, in.type(), cv::Scalar::all(0));
    cv::Mat out_mat_gapi = cv::Mat(cv::Size{ in.cols, in.rows }, in.type(), cv::Scalar::all(0));
    cv::fillPoly(out_mat_ocv,  out_cnts_ocv,  cv::Scalar::all(1));
    cv::fillPoly(out_mat_gapi, out_cnts_gapi, cv::Scalar::all(1));
    EXPECT_TRUE(cmpF(out_mat_ocv, out_mat_gapi));
    if (optional == HIERARCHY)
    {
        EXPECT_TRUE(out_hier_ocv.size() == out_hier_gapi.size());
        EXPECT_TRUE(AbsExactVector<cv::Vec4i>().to_compare_f()(out_hier_ocv, out_hier_gapi));
    }
}

template<OptionalFindContoursOutput optional = NONE>
void findContoursTestBody(const cv::Size& sz, const MatType2& type, const cv::RetrievalModes mode,
                          const cv::ContourApproximationModes method, const CompareMats& cmpF,
                          cv::GCompileArgs&& args, const cv::Point& offset = cv::Point())
{
    cv::Mat in;
    initMatForFindingContours(in, sz, type);

    std::vector<std::vector<cv::Point>> out_cnts_gapi;
    std::vector<cv::Vec4i>              out_hier_gapi;
    findContoursTestGAPI<optional>(in, mode, method, std::move(args), out_cnts_gapi, out_hier_gapi,
                                   offset);
    findContoursTestOpenCVCompare<optional>(in, mode, method, out_cnts_gapi, out_hier_gapi, cmpF,
                                            offset);
}

//-------------------------------------------------------------------------------------------------

template<typename In>
static cv::GComputation boundingRectTestGAPI(const In& in, cv::GCompileArgs&& args,
                                             cv::Rect& out_rect_gapi)
{
    cv::detail::g_type_of_t<In> g_in;
    auto out = cv::gapi::boundingRect(g_in);
    cv::GComputation c(cv::GIn(g_in), cv::GOut(out));
    c.apply(cv::gin(in), cv::gout(out_rect_gapi), std::move(args));
    return c;
}

template<typename In>
static void boundingRectTestOpenCVCompare(const In& in, const cv::Rect& out_rect_gapi,
                                          const CompareRects& cmpF)
{
    // OpenCV code /////////////////////////////////////////////////////////////
    cv::Rect out_rect_ocv = cv::boundingRect(in);
    // Comparison //////////////////////////////////////////////////////////////
    EXPECT_TRUE(cmpF(out_rect_gapi, out_rect_ocv));
}

template<typename In>
static void boundingRectTestBody(const In& in, const CompareRects& cmpF, cv::GCompileArgs&& args)
{
    cv::Rect out_rect_gapi;
    boundingRectTestGAPI(in, std::move(args), out_rect_gapi);
    boundingRectTestOpenCVCompare(in, out_rect_gapi, cmpF);
}

//-------------------------------------------------------------------------------------------------

template<typename In>
static cv::GComputation fitLineTestGAPI(const In& in, const cv::DistanceTypes distType,
                                        cv::GCompileArgs&& args, cv::Vec4f& out_vec_gapi)
{
    const double paramDefault = 0., repsDefault = 0., aepsDefault = 0.;

    cv::detail::g_type_of_t<In> g_in;
    auto out = cv::gapi::fitLine2D(g_in, distType, paramDefault, repsDefault, aepsDefault);
    cv::GComputation c(cv::GIn(g_in), cv::GOut(out));
    c.apply(cv::gin(in), cv::gout(out_vec_gapi), std::move(args));
    return c;
}

template<typename In>
static cv::GComputation fitLineTestGAPI(const In& in, const cv::DistanceTypes distType,
                                        cv::GCompileArgs&& args, cv::Vec6f& out_vec_gapi)
{
    const double paramDefault = 0., repsDefault = 0., aepsDefault = 0.;

    cv::detail::g_type_of_t<In> g_in;
    auto out = cv::gapi::fitLine3D(g_in, distType, paramDefault, repsDefault, aepsDefault);
    cv::GComputation c(cv::GIn(g_in), cv::GOut(out));
    c.apply(cv::gin(in), cv::gout(out_vec_gapi), std::move(args));
    return c;
}

template<typename In, int dim>
static void fitLineTestOpenCVCompare(const In& in, const cv::DistanceTypes distType,
                                     const cv::Vec<float, dim>& out_vec_gapi,
                                     const CompareVecs<float, dim>& cmpF)
{
    const double paramDefault = 0., repsDefault = 0., aepsDefault = 0.;

    // OpenCV code /////////////////////////////////////////////////////////////
    cv::Vec<float, dim> out_vec_ocv;
    cv::fitLine(in, out_vec_ocv, distType, paramDefault, repsDefault, aepsDefault);
    // Comparison //////////////////////////////////////////////////////////////
    EXPECT_TRUE(cmpF(out_vec_gapi, out_vec_ocv));
}

template<typename In, int dim>
static void fitLineTestBody(const In& in, const cv::DistanceTypes distType,
                            const CompareVecs<float, dim>& cmpF, cv::GCompileArgs&& args)
{
    cv::Vec<float, dim> out_vec_gapi;
    fitLineTestGAPI(in, distType, std::move(args), out_vec_gapi);
    fitLineTestOpenCVCompare(in, distType, out_vec_gapi, cmpF);
}
} // namespace opencv_test

#endif // OPENCV_GAPI_IMGPROC_TESTS_COMMON_HPP