]> rtime.felk.cvut.cz Git - opencv.git/blob - opencv/tests/cv/src/detectors_test.cpp
9f9876cbc15ab72574b988959b5a9885a6a9352a
[opencv.git] / opencv / tests / cv / src / detectors_test.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////\r
2 //\r
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
4 //\r
5 //  By downloading, copying, installing or using the software you agree to this license.\r
6 //  If you do not agree to this license, do not download, install,\r
7 //  copy or use the software.\r
8 //\r
9 //\r
10 //                           License Agreement\r
11 //                For Open Source Computer Vision Library\r
12 //\r
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.\r
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.\r
15 // Third party copyrights are property of their respective owners.\r
16 //\r
17 // Redistribution and use in source and binary forms, with or without modification,\r
18 // are permitted provided that the following conditions are met:\r
19 //\r
20 //   * Redistribution's of source code must retain the above copyright notice,\r
21 //     this list of conditions and the following disclaimer.\r
22 //\r
23 //   * Redistribution's in binary form must reproduce the above copyright notice,\r
24 //     this list of conditions and the following disclaimer in the documentation\r
25 //     and/or other materials provided with the distribution.\r
26 //\r
27 //   * The name of the copyright holders may not be used to endorse or promote products\r
28 //     derived from this software without specific prior written permission.\r
29 //\r
30 // This software is provided by the copyright holders and contributors "as is" and\r
31 // any express or implied warranties, including, but not limited to, the implied\r
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.\r
33 // In no event shall the Intel Corporation or contributors be liable for any direct,\r
34 // indirect, incidental, special, exemplary, or consequential damages\r
35 // (including, but not limited to, procurement of substitute goods or services;\r
36 // loss of use, data, or profits; or business interruption) however caused\r
37 // and on any theory of liability, whether in contract, strict liability,\r
38 // or tort (including negligence or otherwise) arising in any way out of\r
39 // the use of this software, even if advised of the possibility of such damage.\r
40 //\r
41 //M*/\r
42 \r
43 #include "cvtest.h"\r
44 #include <string>\r
45 #include <iostream>\r
46 #include <fstream>\r
47 #include <iterator>\r
48 #include "cvaux.h"\r
49 \r
50 using namespace cv;\r
51 using namespace std;\r
52 \r
53 class CV_DetectorsTest : public CvTest\r
54 {\r
55 public:\r
56     CV_DetectorsTest();\r
57     ~CV_DetectorsTest();    \r
58 protected:    \r
59     void run(int);    \r
60 };\r
61 \r
62 CV_DetectorsTest::CV_DetectorsTest(): CvTest( "feature-detectors", "?" )\r
63 {\r
64     support_testing_modes = CvTS::CORRECTNESS_CHECK_MODE;\r
65 }\r
66 CV_DetectorsTest::~CV_DetectorsTest() {}\r
67 \r
68 struct OutOfMask\r
69 {\r
70     const Mat& msk;\r
71     OutOfMask(const Mat& mask) : msk(mask) {};\r
72     OutOfMask& operator=(const OutOfMask&);\r
73     bool operator()(const KeyPoint& kp) const { return msk.at<uchar>(kp.pt) == 0; }\r
74 };\r
75 \r
76 struct WrapPoint\r
77 {\r
78     const double* R;\r
79     WrapPoint(const Mat& rmat) : R(rmat.ptr<double>()) { };\r
80     \r
81     KeyPoint operator()(const KeyPoint& kp) const \r
82     {                \r
83         KeyPoint res = kp;\r
84         res.pt.x = static_cast<float>(kp.pt.x * R[0] + kp.pt.y * R[1] + R[2]);\r
85         res.pt.y = static_cast<float>(kp.pt.x * R[3] + kp.pt.y * R[4] + R[5]);                \r
86         return res;                             \r
87     }\r
88 };\r
89 \r
90 void CV_DetectorsTest::run( int /*start_from*/ )\r
91 {              \r
92     Mat oimg = imread(string(ts->get_data_path()) + "shared/baboon.jpg", 0);\r
93     //Mat oimg = imread(string(ts->get_data_path()) + "shared/fruits.jpg", 0);\r
94 \r
95     if (oimg.empty())\r
96     {\r
97         ts->set_failed_test_info( CvTS::FAIL_INVALID_TEST_DATA );\r
98         return;\r
99     }    \r
100 \r
101     Point center(oimg.cols/2, oimg.rows/2);\r
102               \r
103     Mat aff = getRotationMatrix2D(center, 45, 1);\r
104 \r
105     Mat rimg;  \r
106     warpAffine( oimg, rimg, aff, oimg.size());\r
107 \r
108     Mat mask(oimg.size(), CV_8U, Scalar(0));    \r
109     circle(mask, center, std::min(center.x, center.y) - 10, Scalar(255), CV_FILLED);\r
110     Mat inv_mask;\r
111     mask.convertTo(inv_mask, CV_8U, -1, 255);\r
112 \r
113     Mat oimg_color, rimg_color;\r
114     cvtColor(oimg, oimg_color, CV_GRAY2BGR);   oimg_color.setTo(Scalar(0), inv_mask);\r
115     cvtColor(rimg, rimg_color, CV_GRAY2BGR);   rimg_color.setTo(Scalar(0), inv_mask);   \r
116 \r
117     SURF surf(1536, 2);\r
118     StarDetector star(45, 30, 10, 8, 5);\r
119 \r
120     vector<KeyPoint> opoints_surf, rpoints_surf, opoints_star, rpoints_star;\r
121     surf(oimg, mask, opoints_surf);\r
122     surf(rimg, mask, rpoints_surf);\r
123 \r
124     star(oimg, opoints_star);\r
125     star(rimg, rpoints_star);\r
126     \r
127     opoints_star.erase(\r
128         remove_if(opoints_star.begin(), opoints_star.end(), OutOfMask(mask)),   \r
129         opoints_star.end());\r
130 \r
131     rpoints_star.erase(\r
132         remove_if(rpoints_star.begin(), rpoints_star.end(), OutOfMask(mask)), \r
133         rpoints_star.end());\r
134 \r
135     vector<KeyPoint> exp_rpoints_surf(opoints_surf.size()), exp_rpoints_star(opoints_star.size());\r
136     transform(opoints_surf.begin(), opoints_surf.end(), exp_rpoints_surf.begin(), WrapPoint(aff));\r
137     transform(opoints_star.begin(), opoints_star.end(), exp_rpoints_star.begin(), WrapPoint(aff));\r
138     \r
139     for(size_t i = 0; i < opoints_surf.size(); ++i)\r
140     {\r
141         circle(oimg_color, opoints_surf[i].pt, (int)opoints_surf[i].size/2, CV_RGB(0, 255, 0));\r
142         circle(rimg_color, exp_rpoints_surf[i].pt, (int)exp_rpoints_surf[i].size/2, CV_RGB(255, 0, 0));\r
143     }\r
144 \r
145     for(size_t i = 0; i < rpoints_surf.size(); ++i)\r
146         circle(rimg_color, rpoints_surf[i].pt, (int)rpoints_surf[i].size/2, CV_RGB(0, 255, 0));\r
147 \r
148     for(size_t i = 0; i < opoints_star.size(); ++i)\r
149     {\r
150         circle(oimg_color, opoints_star[i].pt, (int)opoints_star[i].size/2, CV_RGB(0, 0, 255));\r
151         circle(rimg_color, exp_rpoints_surf[i].pt, (int)exp_rpoints_surf[i].size/2, CV_RGB(255, 0, 0));\r
152     }\r
153 \r
154     for(size_t i = 0; i < rpoints_star.size(); ++i)\r
155         circle(rimg_color, rpoints_star[i].pt, (int)rpoints_star[i].size/2, CV_RGB(0, 0, 255));\r
156     \r
157   /*  namedWindow("R"); imshow("R", rimg_color); \r
158     namedWindow("O"); imshow("O", oimg_color); \r
159     waitKey();*/\r
160 \r
161     ts->set_failed_test_info( CvTS::FAIL_GENERIC );             \r
162 }\r
163 \r
164 \r
165 CV_DetectorsTest Detectors_test;\r
166 \r
167 \r
168 \r