1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
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.
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.
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.
42 #include "test_precomp.hpp"
47 class CV_PyrSegmentationTest : public cvtest::BaseTest
50 CV_PyrSegmentationTest();
57 CV_PyrSegmentationTest::CV_PyrSegmentationTest()
61 void CV_PyrSegmentationTest::run( int /*start_from*/ )
63 Mat _image_f, _image, _image_s;
65 const double range = 15;
67 int code = cvtest::TS::OK;
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)};
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};
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}; */
80 int block_size = 1000;
82 CvMemStorage *storage; /* storage for connected component writing */
85 RNG& rng = ts->get_rng();
88 IplImage *image, *image_f, *image_s;
89 CvSize size(128, 128);
90 const int threshold1 = 50, threshold2 = 50;
92 rect[1].width = size.width;
93 rect[1].height = size.height;
94 a[1] = size.width*size.height - a[0] - a[2];
96 OPENCV_CALL( storage = cvCreateMemStorage( block_size ) );
98 for( iter = 0; iter < 2; iter++ )
100 int channels = iter == 0 ? 1 : 3;
101 int mask[] = {0,0,0};
103 image = cvCreateImage(size, 8, channels );
104 image_s = cvCloneImage( image );
105 image_f = cvCloneImage( image );
109 int color1 = 30, color2 = 110, color3 = 190;
111 cvSet( image, cvScalarAll(color1));
112 cvFillPoly( image, &cp, &nPoints, 1, cvScalar(color2));
113 cvFillPoly( image, &cp2, &nPoints, 1, cvScalar(color3));
117 CvScalar color1 = CV_RGB(30,30,30), color2 = CV_RGB(255,0,0), color3 = CV_RGB(0,255,0);
119 assert( channels == 3 );
120 cvSet( image, color1 );
121 cvFillPoly( image, &cp, &nPoints, 1, color2);
122 cvFillPoly( image, &cp2, &nPoints, 1, color3);
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 );
129 cvPyrSegmentation( image_f, image_s,
131 level, threshold1, threshold2 );
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;
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 );
146 for( i = 0; i < 3; i++ )
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 );
153 cvNamedWindow( "test", 1 );
154 cvShowImage( "test", image_s );
158 _image = cvarrToMat(image);
159 _image_s = cvarrToMat(image_s);
160 code = cvtest::cmpEps2( ts, _image, _image_s, 10, false, "the output image" );
164 for( i = 0; i < 3; i++)
166 for( j = 0; j < 3; j++ )
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 )
180 ts->printf( cvtest::TS::LOG, "The component #%d is incorrect\n", i );
181 code = cvtest::TS::FAIL_BAD_ACCURACY;
186 cvReleaseImage(&image_f);
187 cvReleaseImage(&image);
188 cvReleaseImage(&image_s);
193 cvReleaseMemStorage( &storage );
194 cvReleaseImage(&image_f);
195 cvReleaseImage(&image);
196 cvReleaseImage(&image_s);
199 ts->set_failed_test_info( code );
202 TEST(Legacy_PyrSegmentation, regression) { CV_PyrSegmentationTest test; test.safe_run(); }