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.
45 icvAdaptiveThreshold_MeanC( const CvMat* src, CvMat* dst, int method,
46 int maxValue, int type, int size, double delta )
49 CV_FUNCNAME( "icvAdaptiveThreshold_MeanC" );
54 int idelta = type == CV_THRESH_BINARY ? cvCeil(delta) : cvFloor(delta);
57 if( size <= 1 || (size&1) == 0 )
58 CV_ERROR( CV_StsOutOfRange, "Neighborhood size must be >=3 and odd (3, 5, 7, ...)" );
62 CV_CALL( cvSetZero( dst ));
69 if( src->data.ptr != dst->data.ptr )
72 CV_CALL( mean = cvCreateMat( rows, cols, CV_8UC1 ));
74 CV_CALL( cvSmooth( src, mean, method == CV_ADAPTIVE_THRESH_MEAN_C ?
75 CV_BLUR : CV_GAUSSIAN, size, size ));
79 if( type == CV_THRESH_BINARY )
80 for( i = 0; i < 768; i++ )
81 tab[i] = (uchar)(i - 255 > -idelta ? maxValue : 0);
83 for( i = 0; i < 768; i++ )
84 tab[i] = (uchar)(i - 255 <= -idelta ? maxValue : 0);
86 for( i = 0; i < rows; i++ )
88 const uchar* s = src->data.ptr + i*src->step;
89 const uchar* m = mean->data.ptr + i*mean->step;
90 uchar* d = dst->data.ptr + i*dst->step;
92 for( j = 0; j < cols; j++ )
93 d[j] = tab[s[j] - m[j] + 255];
99 cvReleaseMat( &mean );
104 cvAdaptiveThreshold( const void *srcIm, void *dstIm, double maxValue,
105 int method, int type, int blockSize, double param1 )
107 CvMat src_stub, dst_stub;
108 CvMat *src = 0, *dst = 0;
110 CV_FUNCNAME( "cvAdaptiveThreshold" );
114 if( type != CV_THRESH_BINARY && type != CV_THRESH_BINARY_INV )
115 CV_ERROR( CV_StsBadArg, "Only CV_TRESH_BINARY and CV_THRESH_BINARY_INV "
116 "threshold types are acceptable" );
118 CV_CALL( src = cvGetMat( srcIm, &src_stub ));
119 CV_CALL( dst = cvGetMat( dstIm, &dst_stub ));
121 if( !CV_ARE_CNS_EQ( src, dst ))
122 CV_ERROR( CV_StsUnmatchedFormats, "" );
124 if( CV_MAT_TYPE(dst->type) != CV_8UC1 )
125 CV_ERROR( CV_StsUnsupportedFormat, "" );
127 if( !CV_ARE_SIZES_EQ( src, dst ) )
128 CV_ERROR( CV_StsUnmatchedSizes, "" );
132 case CV_ADAPTIVE_THRESH_MEAN_C:
133 case CV_ADAPTIVE_THRESH_GAUSSIAN_C:
134 CV_CALL( icvAdaptiveThreshold_MeanC( src, dst, method, cvRound(maxValue),type,
135 blockSize, param1 ));
138 CV_ERROR( CV_BADCOEF_ERR, "" );