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.
44 static CvStatus CV_STDCALL
45 icvThresh_8u_C1R( const uchar* src, int src_step, uchar* dst, int dst_step,
46 CvSize roi, uchar thresh, uchar maxval, int type )
53 case CV_THRESH_BINARY:
54 for( i = 0; i <= thresh; i++ )
59 case CV_THRESH_BINARY_INV:
60 for( i = 0; i <= thresh; i++ )
66 for( i = 0; i <= thresh; i++ )
71 case CV_THRESH_TOZERO:
72 for( i = 0; i <= thresh; i++ )
77 case CV_THRESH_TOZERO_INV:
78 for( i = 0; i <= thresh; i++ )
84 return CV_BADFLAG_ERR;
87 for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
89 for( j = 0; j <= roi.width - 4; j += 4 )
91 uchar t0 = tab[src[j]];
92 uchar t1 = tab[src[j+1]];
104 for( ; j < roi.width; j++ )
105 dst[j] = tab[src[j]];
112 static CvStatus CV_STDCALL
113 icvThresh_32f_C1R( const float *src, int src_step, float *dst, int dst_step,
114 CvSize roi, float thresh, float maxval, int type )
117 const int* isrc = (const int*)src;
118 int* idst = (int*)dst;
122 v.f = thresh; iThresh = CV_TOGGLE_FLT(v.i);
123 v.f = maxval; iMax = v.i;
125 src_step /= sizeof(src[0]);
126 dst_step /= sizeof(dst[0]);
130 case CV_THRESH_BINARY:
131 for( i = 0; i < roi.height; i++, isrc += src_step, idst += dst_step )
133 for( j = 0; j < roi.width; j++ )
136 idst[j] = ((CV_TOGGLE_FLT(temp) <= iThresh) - 1) & iMax;
141 case CV_THRESH_BINARY_INV:
142 for( i = 0; i < roi.height; i++, isrc += src_step, idst += dst_step )
144 for( j = 0; j < roi.width; j++ )
147 idst[j] = ((CV_TOGGLE_FLT(temp) > iThresh) - 1) & iMax;
152 case CV_THRESH_TRUNC:
153 for( i = 0; i < roi.height; i++, src += src_step, dst += dst_step )
155 for( j = 0; j < roi.width; j++ )
166 case CV_THRESH_TOZERO:
167 for( i = 0; i < roi.height; i++, isrc += src_step, idst += dst_step )
169 for( j = 0; j < roi.width; j++ )
172 idst[j] = ((CV_TOGGLE_FLT( temp ) <= iThresh) - 1) & temp;
177 case CV_THRESH_TOZERO_INV:
178 for( i = 0; i < roi.height; i++, isrc += src_step, idst += dst_step )
180 for( j = 0; j < roi.width; j++ )
183 idst[j] = ((CV_TOGGLE_FLT( temp ) > iThresh) - 1) & temp;
189 return CV_BADFLAG_ERR;
197 icvGetThreshVal_Otsu( const CvHistogram* hist )
201 CV_FUNCNAME( "icvGetThreshVal_Otsu" );
207 double sum = 0, mu = 0;
208 bool uniform = false;
209 double low = 0, high = 0, delta = 0;
210 float* nu_thresh = 0;
211 double mu1 = 0, q1 = 0;
212 double max_sigma = 0;
214 if( !CV_IS_HIST(hist) || CV_IS_SPARSE_HIST(hist) || hist->mat.dims != 1 )
215 CV_ERROR( CV_StsBadArg,
216 "The histogram in Otsu method must be a valid dense 1D histogram" );
218 count = hist->mat.dim[0].size;
219 h = (float*)cvPtr1D( hist->bins, 0 );
221 if( !CV_HIST_HAS_RANGES(hist) || CV_IS_UNIFORM_HIST(hist) )
223 if( CV_HIST_HAS_RANGES(hist) )
225 low = hist->thresh[0][0];
226 high = hist->thresh[0][1];
234 delta = (high-low)/count;
239 nu_thresh = hist->thresh2[0];
241 for( i = 0; i < count; i++ )
245 mu += (i*delta + low)*h[i];
247 mu += (nu_thresh[i*2] + nu_thresh[i*2+1])*0.5*h[i];
250 sum = fabs(sum) > FLT_EPSILON ? 1./sum : 0;
256 for( i = 0; i < count; i++ )
258 double p_i, q2, mu2, val_i, sigma;
265 if( MIN(q1,q2) < FLT_EPSILON || MAX(q1,q2) > 1. - FLT_EPSILON )
269 val_i = i*delta + low;
271 val_i = (nu_thresh[i*2] + nu_thresh[i*2+1])*0.5;
273 mu1 = (mu1 + val_i*p_i)/q1;
274 mu2 = (mu - q1*mu1)/q2;
275 sigma = q1*q2*(mu1 - mu2)*(mu1 - mu2);
276 if( sigma > max_sigma )
289 icvAndC_8u_C1R_t icvAndC_8u_C1R_p = 0;
290 icvCompareC_8u_C1R_cv_t icvCompareC_8u_C1R_cv_p = 0;
291 icvThreshold_GTVal_8u_C1R_t icvThreshold_GTVal_8u_C1R_p = 0;
292 icvThreshold_GTVal_32f_C1R_t icvThreshold_GTVal_32f_C1R_p = 0;
293 icvThreshold_LTVal_8u_C1R_t icvThreshold_LTVal_8u_C1R_p = 0;
294 icvThreshold_LTVal_32f_C1R_t icvThreshold_LTVal_32f_C1R_p = 0;
297 cvThreshold( const void* srcarr, void* dstarr, double thresh, double maxval, int type )
299 CvHistogram* hist = 0;
301 CV_FUNCNAME( "cvThreshold" );
306 int src_step, dst_step;
307 CvMat src_stub, *src = (CvMat*)srcarr;
308 CvMat dst_stub, *dst = (CvMat*)dstarr;
310 int coi1 = 0, coi2 = 0;
311 int ithresh, imaxval, cn;
314 CV_CALL( src = cvGetMat( src, &src_stub, &coi1 ));
315 CV_CALL( dst = cvGetMat( dst, &dst_stub, &coi2 ));
318 CV_ERROR( CV_BadCOI, "COI is not supported by the function" );
320 if( !CV_ARE_CNS_EQ( src, dst ) )
321 CV_ERROR( CV_StsUnmatchedFormats, "Both arrays must have equal number of channels" );
323 cn = CV_MAT_CN(src->type);
326 src = cvReshape( src, &src0, 1 );
327 dst = cvReshape( dst, &dst0, 1 );
330 use_otsu = (type & ~CV_THRESH_MASK) == CV_THRESH_OTSU;
331 type &= CV_THRESH_MASK;
335 float _ranges[] = { 0, 256 };
336 float* ranges = _ranges;
340 if( CV_MAT_TYPE(src->type) != CV_8UC1 )
341 CV_ERROR( CV_StsNotImplemented, "Otsu method can only be used with 8uC1 images" );
343 CV_CALL( hist = cvCreateHist( 1, &hist_size, CV_HIST_ARRAY, &ranges ));
344 cvCalcArrHist( &srcarr0, hist );
345 thresh = cvFloor(icvGetThreshVal_Otsu( hist ));
348 if( !CV_ARE_DEPTHS_EQ( src, dst ) )
350 if( CV_MAT_TYPE(dst->type) != CV_8UC1 )
351 CV_ERROR( CV_StsUnsupportedFormat, "In case of different types destination should be 8uC1" );
353 if( type != CV_THRESH_BINARY && type != CV_THRESH_BINARY_INV )
354 CV_ERROR( CV_StsBadArg,
355 "In case of different types only CV_THRESH_BINARY "
356 "and CV_THRESH_BINARY_INV thresholding types are supported" );
360 CV_CALL( cvSetZero( dst ));
364 CV_CALL( cvCmpS( src, thresh, dst, type == CV_THRESH_BINARY ? CV_CMP_GT : CV_CMP_LE ));
366 CV_CALL( cvAndS( dst, cvScalarAll( maxval ), dst ));
371 if( !CV_ARE_SIZES_EQ( src, dst ) )
372 CV_ERROR( CV_StsUnmatchedSizes, "" );
374 roi = cvGetMatSize( src );
375 if( CV_IS_MAT_CONT( src->type & dst->type ))
377 roi.width *= roi.height;
379 src_step = dst_step = CV_STUB_STEP;
383 src_step = src->step;
384 dst_step = dst->step;
387 switch( CV_MAT_DEPTH(src->type) )
391 ithresh = cvFloor(thresh);
392 imaxval = cvRound(maxval);
393 if( type == CV_THRESH_TRUNC )
395 imaxval = CV_CAST_8U(imaxval);
397 if( ithresh < 0 || ithresh >= 255 )
399 if( type == CV_THRESH_BINARY || type == CV_THRESH_BINARY_INV ||
400 (type == CV_THRESH_TRUNC || type == CV_THRESH_TOZERO_INV) && ithresh < 0 ||
401 type == CV_THRESH_TOZERO && ithresh >= 255 )
403 int v = type == CV_THRESH_BINARY ? (ithresh >= 255 ? 0 : imaxval) :
404 type == CV_THRESH_BINARY_INV ? (ithresh >= 255 ? imaxval : 0) :
405 type == CV_THRESH_TRUNC ? imaxval : 0;
407 cvSet( dst, cvScalarAll(v) );
417 if( type == CV_THRESH_BINARY || type == CV_THRESH_BINARY_INV )
419 if( icvCompareC_8u_C1R_cv_p && icvAndC_8u_C1R_p )
421 IPPI_CALL( icvCompareC_8u_C1R_cv_p( src->data.ptr, src_step,
422 (uchar)ithresh, dst->data.ptr, dst_step, roi,
423 type == CV_THRESH_BINARY ? cvCmpGreater : cvCmpLessEq ));
426 IPPI_CALL( icvAndC_8u_C1R_p( dst->data.ptr, dst_step,
427 (uchar)imaxval, dst->data.ptr, dst_step, roi ));
431 else if( type == CV_THRESH_TRUNC || type == CV_THRESH_TOZERO_INV )
433 if( icvThreshold_GTVal_8u_C1R_p )
435 IPPI_CALL( icvThreshold_GTVal_8u_C1R_p( src->data.ptr, src_step,
436 dst->data.ptr, dst_step, roi, (uchar)ithresh,
437 (uchar)(type == CV_THRESH_TRUNC ? ithresh : 0) ));
443 assert( type == CV_THRESH_TOZERO );
444 if( icvThreshold_LTVal_8u_C1R_p )
446 ithresh = cvFloor(thresh+1.);
447 ithresh = CV_CAST_8U(ithresh);
448 IPPI_CALL( icvThreshold_LTVal_8u_C1R_p( src->data.ptr, src_step,
449 dst->data.ptr, dst_step, roi, (uchar)ithresh, 0 ));
454 icvThresh_8u_C1R( src->data.ptr, src_step,
455 dst->data.ptr, dst_step, roi,
456 (uchar)ithresh, (uchar)imaxval, type );
460 if( type == CV_THRESH_TRUNC || type == CV_THRESH_TOZERO_INV )
462 if( icvThreshold_GTVal_32f_C1R_p )
464 IPPI_CALL( icvThreshold_GTVal_32f_C1R_p( src->data.fl, src_step,
465 dst->data.fl, dst_step, roi, (float)thresh,
466 type == CV_THRESH_TRUNC ? (float)thresh : 0 ));
470 else if( type == CV_THRESH_TOZERO )
472 if( icvThreshold_LTVal_32f_C1R_p )
474 IPPI_CALL( icvThreshold_LTVal_32f_C1R_p( src->data.fl, src_step,
475 dst->data.fl, dst_step, roi, (float)(thresh*(1 + FLT_EPSILON)), 0 ));
480 icvThresh_32f_C1R( src->data.fl, src_step, dst->data.fl, dst_step, roi,
481 (float)thresh, (float)maxval, type );
484 CV_ERROR( CV_BadDepth, cvUnsupportedFormat );
490 cvReleaseHist( &hist );