3bd278c0953b280ea5dc9bf4e7addb63aedf5bb5
[platform/upstream/opencv.git] / modules / legacy / test / test_pyrsegmentation.cpp
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 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of Intel Corporation may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41
42 #include "test_precomp.hpp"
43
44 using namespace cv;
45 using namespace std;
46
47 class CV_PyrSegmentationTest : public cvtest::BaseTest
48 {
49 public:
50     CV_PyrSegmentationTest();
51 protected:
52     void run(int);
53 };
54
55 #define SCAN  0
56
57 CV_PyrSegmentationTest::CV_PyrSegmentationTest()
58 {
59 }
60
61 void CV_PyrSegmentationTest::run( int /*start_from*/ )
62 {
63     Mat _image_f, _image, _image_s;
64     const int level = 5;
65     const double range = 15;
66
67     int code = cvtest::TS::OK;
68
69     CvPoint _cp[] ={ CvPoint(33,33), CvPoint(43,33), CvPoint(43,43), CvPoint(33,43)};
70     CvPoint _cp2[] ={CvPoint(50,50), CvPoint(70,50), CvPoint(70,70), CvPoint(50,70)};
71     CvPoint* cp = _cp;
72     CvPoint* cp2 = _cp2;
73     CvConnectedComp *dst_comp[3];
74     CvRect rect[3] = {CvRect(50,50,21,21), CvRect(0,0,128,128), CvRect(33,33,11,11)};
75     double a[3] = {441.0, 15822.0, 121.0};
76
77 /*    ippiPoint cp3[] ={130,130, 150,130, 150,150, 130,150};  */
78 /*      CvPoint cp[] ={0,0, 5,5, 5,0, 10,5, 10,0, 15,5, 15,0};  */
79     int nPoints = 4;
80     int block_size = 1000;
81
82     CvMemStorage *storage;   /*   storage for connected component writing  */
83     CvSeq *comp;
84
85     RNG& rng = ts->get_rng();
86     int i, j, iter;
87
88     IplImage *image, *image_f, *image_s;
89     CvSize size(128, 128);
90     const int threshold1 = 50, threshold2 = 50;
91
92     rect[1].width = size.width;
93     rect[1].height = size.height;
94     a[1] = size.width*size.height - a[0] - a[2];
95
96     OPENCV_CALL( storage = cvCreateMemStorage( block_size ) );
97
98     for( iter = 0; iter < 2; iter++ )
99     {
100         int channels = iter == 0 ? 1 : 3;
101         int mask[] = {0,0,0};
102
103         image = cvCreateImage(size, 8, channels );
104         image_s = cvCloneImage( image );
105         image_f = cvCloneImage( image );
106
107         if( channels == 1 )
108         {
109             int color1 = 30, color2 = 110, color3 = 190;
110
111             cvSet( image, cvScalarAll(color1));
112             cvFillPoly( image, &cp, &nPoints, 1, cvScalar(color2));
113             cvFillPoly( image, &cp2, &nPoints, 1, cvScalar(color3));
114         }
115         else
116         {
117             CvScalar color1 = CV_RGB(30,30,30), color2 = CV_RGB(255,0,0), color3 = CV_RGB(0,255,0);
118
119             assert( channels == 3 );
120             cvSet( image, color1 );
121             cvFillPoly( image, &cp, &nPoints, 1, color2);
122             cvFillPoly( image, &cp2, &nPoints, 1, color3);
123         }
124
125         _image_f = cvarrToMat(image_f);
126         cvtest::randUni( rng, _image_f, cvScalarAll(0), cvScalarAll(range*2) );
127         cvAddWeighted( image, 1, image_f, 1, -range, image_f );
128
129         cvPyrSegmentation( image_f, image_s,
130                            storage, &comp,
131                            level, threshold1, threshold2 );
132
133         if(comp->total != 3)
134         {
135             ts->printf( cvtest::TS::LOG,
136                 "The segmentation function returned %d (not 3) components\n", comp->total );
137             code = cvtest::TS::FAIL_INVALID_OUTPUT;
138             goto _exit_;
139         }
140         /*  read the connected components     */
141         dst_comp[0] = (CvConnectedComp*)CV_GET_SEQ_ELEM( CvConnectedComp, comp, 0 );
142         dst_comp[1] = (CvConnectedComp*)CV_GET_SEQ_ELEM( CvConnectedComp, comp, 1 );
143         dst_comp[2] = (CvConnectedComp*)CV_GET_SEQ_ELEM( CvConnectedComp, comp, 2 );
144
145         /*{
146             for( i = 0; i < 3; i++ )
147             {
148                 CvRect r = dst_comp[i]->rect;
149                 cvRectangle( image_s, cvPoint(r.x,r.y), cvPoint(r.x+r.width,r.y+r.height),
150                     CV_RGB(255,255,255), 3, 8, 0 );
151             }
152
153             cvNamedWindow( "test", 1 );
154             cvShowImage( "test", image_s );
155             cvWaitKey(0);
156         }*/
157
158         _image = cvarrToMat(image);
159         _image_s = cvarrToMat(image_s);
160         code = cvtest::cmpEps2( ts, _image, _image_s, 10, false, "the output image" );
161         if( code < 0 )
162             goto _exit_;
163
164         for( i = 0; i < 3; i++)
165         {
166             for( j = 0; j < 3; j++ )
167             {
168                 if( !mask[j] && dst_comp[i]->area == a[j] &&
169                     dst_comp[i]->rect.x == rect[j].x &&
170                     dst_comp[i]->rect.y == rect[j].y &&
171                     dst_comp[i]->rect.width == rect[j].width &&
172                     dst_comp[i]->rect.height == rect[j].height )
173                 {
174                     mask[j] = 1;
175                     break;
176                 }
177             }
178             if( j == 3 )
179             {
180                 ts->printf( cvtest::TS::LOG, "The component #%d is incorrect\n", i );
181                 code = cvtest::TS::FAIL_BAD_ACCURACY;
182                 goto _exit_;
183             }
184         }
185
186         cvReleaseImage(&image_f);
187         cvReleaseImage(&image);
188         cvReleaseImage(&image_s);
189     }
190
191 _exit_:
192
193     cvReleaseMemStorage( &storage );
194     cvReleaseImage(&image_f);
195     cvReleaseImage(&image);
196     cvReleaseImage(&image_s);
197
198     if( code < 0 )
199         ts->set_failed_test_info( code );
200 }
201
202 TEST(Legacy_PyrSegmentation, regression) { CV_PyrSegmentationTest test; test.safe_run(); }
203
204 /* End of file. */