1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Copyright (C) 2014, Itseez, Inc, all rights reserved.
16 // Third party copyrights are property of their respective owners.
17 //
18 // Redistribution and use in source and binary forms, with or without modification,
19 // are permitted provided that the following conditions are met:
20 //
21 //   * Redistribution's of source code must retain the above copyright notice,
22 //     this list of conditions and the following disclaimer.
23 //
24 //   * Redistribution's in binary form must reproduce the above copyright notice,
25 //     this list of conditions and the following disclaimer in the documentation
26 //     and/or other materials provided with the distribution.
27 //
28 //   * The name of the copyright holders may not be used to endorse or promote products
29 //     derived from this software without specific prior written permission.
30 //
31 // This software is provided by the copyright holders and contributors "as is" and
32 // any express or implied warranties, including, but not limited to, the implied
33 // warranties of merchantability and fitness for a particular purpose are disclaimed.
34 // In no event shall the Intel Corporation or contributors be liable for any direct,
35 // indirect, incidental, special, exemplary, or consequential damages
36 // (including, but not limited to, procurement of substitute goods or services;
37 // loss of use, data, or profits; or business interruption) however caused
38 // and on any theory of liability, whether in contract, strict liability,
39 // or tort (including negligence or otherwise) arising in any way out of
40 // the use of this software, even if advised of the possibility of such damage.
41 //
42 //M*/
43 
44 #include "test_precomp.hpp"
45 
46 using namespace cv;
47 using namespace std;
48 
49 template<typename T>
50 struct SimilarWith
51 {
52     T value;
53     float theta_eps;
54     float rho_eps;
SimilarWithSimilarWith55     SimilarWith<T>(T val, float e, float r_e): value(val), theta_eps(e), rho_eps(r_e) { };
56     bool operator()(T other);
57 };
58 
59 template<>
operator ()(Vec2f other)60 bool SimilarWith<Vec2f>::operator()(Vec2f other)
61 {
62     return abs(other[0] - value[0]) < rho_eps && abs(other[1] - value[1]) < theta_eps;
63 }
64 
65 template<>
operator ()(Vec4i other)66 bool SimilarWith<Vec4i>::operator()(Vec4i other)
67 {
68     return norm(value, other) < theta_eps;
69 }
70 
71 template <typename T>
countMatIntersection(Mat expect,Mat actual,float eps,float rho_eps)72 int countMatIntersection(Mat expect, Mat actual, float eps, float rho_eps)
73 {
74     int count = 0;
75     if (!expect.empty() && !actual.empty())
76     {
77         for (MatIterator_<T> it=expect.begin<T>(); it!=expect.end<T>(); it++)
78         {
79             MatIterator_<T> f = std::find_if(actual.begin<T>(), actual.end<T>(), SimilarWith<T>(*it, eps, rho_eps));
80             if (f != actual.end<T>())
81                 count++;
82         }
83     }
84     return count;
85 }
86 
getTestCaseName(String filename)87 String getTestCaseName(String filename)
88 {
89     string temp(filename);
90     size_t pos = temp.find_first_of("\\/.");
91     while ( pos != string::npos ) {
92        temp.replace( pos, 1, "_" );
93        pos = temp.find_first_of("\\/.");
94     }
95     return String(temp);
96 }
97 
98 class BaseHoughLineTest
99 {
100 public:
101     enum {STANDART = 0, PROBABILISTIC};
102 protected:
103     void run_test(int type);
104 
105     string picture_name;
106     double rhoStep;
107     double thetaStep;
108     int threshold;
109     int minLineLength;
110     int maxGap;
111 };
112 
113 typedef std::tr1::tuple<string, double, double, int> Image_RhoStep_ThetaStep_Threshold_t;
114 class StandartHoughLinesTest : public BaseHoughLineTest, public testing::TestWithParam<Image_RhoStep_ThetaStep_Threshold_t>
115 {
116 public:
StandartHoughLinesTest()117     StandartHoughLinesTest()
118     {
119         picture_name = std::tr1::get<0>(GetParam());
120         rhoStep = std::tr1::get<1>(GetParam());
121         thetaStep = std::tr1::get<2>(GetParam());
122         threshold = std::tr1::get<3>(GetParam());
123         minLineLength = 0;
124         maxGap = 0;
125     }
126 };
127 
128 typedef std::tr1::tuple<string, double, double, int, int, int> Image_RhoStep_ThetaStep_Threshold_MinLine_MaxGap_t;
129 class ProbabilisticHoughLinesTest : public BaseHoughLineTest, public testing::TestWithParam<Image_RhoStep_ThetaStep_Threshold_MinLine_MaxGap_t>
130 {
131 public:
ProbabilisticHoughLinesTest()132     ProbabilisticHoughLinesTest()
133     {
134         picture_name = std::tr1::get<0>(GetParam());
135         rhoStep = std::tr1::get<1>(GetParam());
136         thetaStep = std::tr1::get<2>(GetParam());
137         threshold = std::tr1::get<3>(GetParam());
138         minLineLength = std::tr1::get<4>(GetParam());
139         maxGap = std::tr1::get<5>(GetParam());
140     }
141 };
142 
run_test(int type)143 void BaseHoughLineTest::run_test(int type)
144 {
145     string filename = cvtest::TS::ptr()->get_data_path() + picture_name;
146     Mat src = imread(filename, IMREAD_GRAYSCALE);
147     EXPECT_FALSE(src.empty()) << "Invalid test image: " << filename;
148 
149     string xml;
150     if (type == STANDART)
151         xml = string(cvtest::TS::ptr()->get_data_path()) + "imgproc/HoughLines.xml";
152     else if (type == PROBABILISTIC)
153         xml = string(cvtest::TS::ptr()->get_data_path()) + "imgproc/HoughLinesP.xml";
154 
155     Mat dst;
156     Canny(src, dst, 100, 150, 3);
157     EXPECT_FALSE(dst.empty()) << "Failed Canny edge detector";
158 
159     Mat lines;
160     if (type == STANDART)
161         HoughLines(dst, lines, rhoStep, thetaStep, threshold, 0, 0);
162     else if (type == PROBABILISTIC)
163         HoughLinesP(dst, lines, rhoStep, thetaStep, threshold, minLineLength, maxGap);
164 
165     String test_case_name = format("lines_%s_%.0f_%.2f_%d_%d_%d", picture_name.c_str(), rhoStep, thetaStep,
166                                     threshold, minLineLength, maxGap);
167     test_case_name = getTestCaseName(test_case_name);
168 
169     FileStorage fs(xml, FileStorage::READ);
170     FileNode node = fs[test_case_name];
171     if (node.empty())
172     {
173         fs.release();
174         fs.open(xml, FileStorage::APPEND);
175         EXPECT_TRUE(fs.isOpened()) << "Cannot open sanity data file: " << xml;
176         fs << test_case_name << lines;
177         fs.release();
178         fs.open(xml, FileStorage::READ);
179         EXPECT_TRUE(fs.isOpened()) << "Cannot open sanity data file: " << xml;
180     }
181 
182     Mat exp_lines;
183     read( fs[test_case_name], exp_lines, Mat() );
184     fs.release();
185 
186     int count = -1;
187     if (type == STANDART)
188         count = countMatIntersection<Vec2f>(exp_lines, lines, (float) thetaStep + FLT_EPSILON, (float) rhoStep + FLT_EPSILON);
189     else if (type == PROBABILISTIC)
190         count = countMatIntersection<Vec4i>(exp_lines, lines, 1e-4f, 0.f);
191 
192 #if (0 && defined(HAVE_IPP) && !defined(HAVE_IPP_ICV_ONLY) && IPP_VERSION_X100 >= 801)
193     EXPECT_GE( count, (int) (exp_lines.total() * 0.8) );
194 #else
195     EXPECT_EQ( count, (int) exp_lines.total());
196 #endif
197 }
198 
TEST_P(StandartHoughLinesTest,regression)199 TEST_P(StandartHoughLinesTest, regression)
200 {
201     run_test(STANDART);
202 }
203 
TEST_P(ProbabilisticHoughLinesTest,regression)204 TEST_P(ProbabilisticHoughLinesTest, regression)
205 {
206     run_test(PROBABILISTIC);
207 }
208 
209 INSTANTIATE_TEST_CASE_P( ImgProc, StandartHoughLinesTest, testing::Combine(testing::Values( "shared/pic5.png", "../stitching/a1.png" ),
210                                                                            testing::Values( 1, 10 ),
211                                                                            testing::Values( 0.05, 0.1 ),
212                                                                            testing::Values( 80, 150 )
213                                                                            ));
214 
215 INSTANTIATE_TEST_CASE_P( ImgProc, ProbabilisticHoughLinesTest, testing::Combine(testing::Values( "shared/pic5.png", "shared/pic1.png" ),
216                                                                                 testing::Values( 5, 10 ),
217                                                                                 testing::Values( 0.05, 0.1 ),
218                                                                                 testing::Values( 75, 150 ),
219                                                                                 testing::Values( 0, 10 ),
220                                                                                 testing::Values( 0, 4 )
221                                                                                 ));
222