Move the sources to trunk
[opencv] / tests / cv / src / aadaptthresh.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 "cvtest.h"
43
44 #if 0
45  
46 //extern "C"{
47 //  #include "HighGUI.h"
48 //}
49
50
51 static char cTestName[] = "Image Adaptive Thresholding";
52 static char cTestClass[] = "Algorithm";
53 static char cFuncName[] = "cvAdaptThreshold";
54 static int  aAdaptThreshold()
55 {
56
57     CvPoint *cp;
58     int parameter1 = 3;
59     double parameter2 = 10;
60     int width = 128;
61     int height = 128;
62     int kp = 5;
63     int nPoints2 = 20;
64
65     int fi = 0;
66     int a2 = 20;
67     int b2 = 25,xc,yc;
68
69     double pi = 3.1415926;
70
71     double lower, upper;
72     unsigned seed;
73     char rand;
74     AtsRandState state;
75
76     long diff_binary, diff_binary_inv;
77     
78     int l,i,j;
79
80     IplImage *imBinary, *imBinary_inv, *imTo_zero, *imTo_zero_inv, *imInput, *imOutput;
81     CvSize size;
82
83     int code = TRS_OK;
84
85 //  read tests params 
86     if(!trsiRead( &width, "128", "image width" ))
87         return TRS_UNDEF;
88     if(!trsiRead( &height, "128", "image height" ))
89         return TRS_UNDEF;
90
91 //  initialized image
92     l = width*height*sizeof(uchar);
93
94     cp = (CvPoint*) trsmAlloc(nPoints2*sizeof(CvPoint));
95
96     xc = (int)( width/2.);
97     yc = (int)( height/2.);
98
99     kp = nPoints2;
100
101     size.width = width;
102     size.height = height;
103
104     int xmin = width;
105     int ymin = height;
106     int xmax = 0;
107     int ymax = 0;
108     
109     
110     for(i=0;i<nPoints2;i++)
111     {
112         cp[i].x = (int)(a2*cos(2*pi*i/nPoints2)*cos(2*pi*fi/360.))-
113         (int)(b2*sin(2*pi*i/nPoints2)*sin(2*pi*fi/360.))+xc;
114         if(xmin> cp[i].x) xmin = cp[i].x;
115         if(xmax< cp[i].x) xmax = cp[i].x;
116         cp[i].y = (int)(a2*cos(2*pi*i/nPoints2)*sin(2*pi*fi/360.))+
117                     (int)(b2*sin(2*pi*i/nPoints2)*cos(2*pi*fi/360.))+yc;
118         if(ymin> cp[i].y) ymin = cp[i].y;
119         if(ymax< cp[i].y) ymax = cp[i].y;
120     }
121
122     if(xmax>width||xmin<0||ymax>height||ymin<0) return TRS_FAIL;
123     
124 //  IPL image moment calculation  
125 //  create image  
126     imBinary = cvCreateImage( size, 8, 1 );
127     imBinary_inv = cvCreateImage( size, 8, 1 );
128     imTo_zero = cvCreateImage( size, 8, 1 );
129     imTo_zero_inv = cvCreateImage( size, 8, 1 );
130     imOutput = cvCreateImage( size, 8, 1 );
131     imInput = cvCreateImage( size, 8, 1 );
132
133     int bgrn = 50;
134     int signal = 150;
135     
136     memset(imInput->imageData,bgrn,l);
137
138     cvFillPoly(imInput, &cp, &kp, 1, cvScalarAll(signal));
139
140 //  do noise   
141     upper = 22;
142     lower = -upper;
143     seed = 345753;
144     atsRandInit( &state, lower, upper, seed );
145     
146     uchar *input = (uchar*)imInput->imageData;
147     uchar *binary = (uchar*)imBinary->imageData;
148     uchar *binary_inv = (uchar*)imBinary_inv->imageData;
149     uchar *to_zero = (uchar*)imTo_zero->imageData;
150     uchar *to_zero_inv = (uchar*)imTo_zero_inv->imageData;
151     double *parameter = (double*)trsmAlloc(2*sizeof(double));
152
153     int step = imInput->widthStep;
154
155     for(i = 0; i<size.height; i++, input+=step, binary+=step, binary_inv+=step, to_zero+=step,to_zero_inv+=step)
156     {
157          for(j = 0; j<size.width; j++)
158          {
159                 atsbRand8s( &state, &rand, 1);   
160                 if(input[j] == bgrn) 
161                 {
162                     binary[j] = to_zero[j] = (uchar)0;
163                     binary_inv[j] = (uchar)255;
164                     to_zero_inv[j] = input [j] = (uchar)(bgrn + rand);
165                 }
166                 else 
167                 {
168                     binary[j] = (uchar)255;
169                     binary_inv[j] = to_zero_inv[j] = (uchar)0;
170                     to_zero[j] = input[j] = (uchar)(signal + rand);
171                 }
172         
173          }
174     }
175
176
177
178     cvAdaptiveThreshold( imInput, imOutput, (double)255, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY, parameter1, parameter2 ); 
179     diff_binary = atsCompare1Db( (uchar*)imOutput->imageData, (uchar*)imBinary->imageData, l, 5);
180
181     cvAdaptiveThreshold( imInput, imOutput, (double)255, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY_INV, parameter1, parameter2 ); 
182     diff_binary_inv = atsCompare1Db( (uchar*)imOutput->imageData, (uchar*)imBinary_inv->imageData, l, 5);
183
184     if( diff_binary > 5 || diff_binary_inv > 5 )
185         code = TRS_FAIL;  
186     
187     cvReleaseImage(&imInput);
188     cvReleaseImage(&imOutput);
189     cvReleaseImage(&imBinary);
190     cvReleaseImage(&imBinary_inv);
191     cvReleaseImage(&imTo_zero);
192     cvReleaseImage(&imTo_zero_inv);
193
194     trsWrite( ATS_CON | ATS_LST | ATS_SUM, "diff_binary =%ld \n", diff_binary); 
195     trsWrite( ATS_CON | ATS_LST | ATS_SUM, "diff_binary_inv =%ld \n", diff_binary_inv); 
196
197     trsFree(parameter);
198     trsFree(cp);
199     return code;
200 }
201
202
203 void InitAAdaptThreshold( void )
204 {
205 /* Test Registartion */
206     trsReg(cFuncName,cTestName,cTestClass,aAdaptThreshold); 
207 } /* InitAAdaptThreshold */
208
209 #endif
210
211 /* End of file. */