Home | History | Annotate | Download | only in src
      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 "_cv.h"
     43 
     44 static void
     45 icvAdaptiveThreshold_MeanC( const CvMat* src, CvMat* dst, int method,
     46                             int maxValue, int type, int size, double delta )
     47 {
     48     CvMat* mean = 0;
     49     CV_FUNCNAME( "icvAdaptiveThreshold_MeanC" );
     50 
     51     __BEGIN__;
     52 
     53     int i, j, rows, cols;
     54     int idelta = type == CV_THRESH_BINARY ? cvCeil(delta) : cvFloor(delta);
     55     uchar tab[768];
     56 
     57     if( size <= 1 || (size&1) == 0 )
     58         CV_ERROR( CV_StsOutOfRange, "Neighborhood size must be >=3 and odd (3, 5, 7, ...)" );
     59 
     60     if( maxValue < 0 )
     61     {
     62         CV_CALL( cvSetZero( dst ));
     63         EXIT;
     64     }
     65 
     66     rows = src->rows;
     67     cols = src->cols;
     68 
     69     if( src->data.ptr != dst->data.ptr )
     70         mean = dst;
     71     else
     72         CV_CALL( mean = cvCreateMat( rows, cols, CV_8UC1 ));
     73 
     74     CV_CALL( cvSmooth( src, mean, method == CV_ADAPTIVE_THRESH_MEAN_C ?
     75                        CV_BLUR : CV_GAUSSIAN, size, size ));
     76     if( maxValue > 255 )
     77         maxValue = 255;
     78 
     79     if( type == CV_THRESH_BINARY )
     80         for( i = 0; i < 768; i++ )
     81             tab[i] = (uchar)(i - 255 > -idelta ? maxValue : 0);
     82     else
     83         for( i = 0; i < 768; i++ )
     84             tab[i] = (uchar)(i - 255 <= -idelta ? maxValue : 0);
     85 
     86     for( i = 0; i < rows; i++ )
     87     {
     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;
     91 
     92         for( j = 0; j < cols; j++ )
     93             d[j] = tab[s[j] - m[j] + 255];
     94     }
     95 
     96     __END__;
     97 
     98     if( mean != dst )
     99         cvReleaseMat( &mean );
    100 }
    101 
    102 
    103 CV_IMPL void
    104 cvAdaptiveThreshold( const void *srcIm, void *dstIm, double maxValue,
    105                      int method, int type, int blockSize, double param1 )
    106 {
    107     CvMat src_stub, dst_stub;
    108     CvMat *src = 0, *dst = 0;
    109 
    110     CV_FUNCNAME( "cvAdaptiveThreshold" );
    111 
    112     __BEGIN__;
    113 
    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" );
    117 
    118     CV_CALL( src = cvGetMat( srcIm, &src_stub ));
    119     CV_CALL( dst = cvGetMat( dstIm, &dst_stub ));
    120 
    121     if( !CV_ARE_CNS_EQ( src, dst ))
    122         CV_ERROR( CV_StsUnmatchedFormats, "" );
    123 
    124     if( CV_MAT_TYPE(dst->type) != CV_8UC1 )
    125         CV_ERROR( CV_StsUnsupportedFormat, "" );
    126 
    127     if( !CV_ARE_SIZES_EQ( src, dst ) )
    128         CV_ERROR( CV_StsUnmatchedSizes, "" );
    129 
    130     switch( method )
    131     {
    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 ));
    136         break;
    137     default:
    138         CV_ERROR( CV_BADCOEF_ERR, "" );
    139     }
    140 
    141     __END__;
    142 }
    143 
    144 /* End of file. */
    145