CYFROWY BARON • PROGRAMOWANIE • Zobacz wątek - Przetwarzanie grafiki Image TImage

Przetwarzanie grafiki Image TImage

problemy z tworzeniem aplikacji graficznych oraz audio i wideo

Przetwarzanie grafiki Image TImage

Nowy postprzez kwgrkwgr » środa, 11 maja 2011, 13:47

W sieci znalazłem archiwum gdzie jest już zaimplementowanych dużo funkcji przetwarzających Image. Chciałbym zrobić prosty program typu:
Wczytuje obraz z pliku (BMP lub JPG) do komponentu TImage1 a następnie użyć jedną z tych funkcji (kody osobno niżej) i w TImage2 otrzymać wynik.
Co zrobić by to adaptować do Builder 6 lub Builder 2010 ? W jakimś innym temacie na forum nie wyszło mi dołączanie dodatkowych plików do projektu. Choć niby wg poradnika wszystko powinno być OK.

A dokładnie potrzebuje użyć:
G. Thresholding
[*] threshold_huang: Huang's fuzzy thresholding method
[*] threshold_iter: Iterative Selection thresholding method
[*] threshold_kapur: Kapur-Sahoo-Wong (Maximum Entropy) thresholding method
[*] threshold_li: Li's Minimum Cross Entropy thresholding method
[*] threshold_moment: Moment-Preserving thresholding method
[*] threshold_otsu: Otsu's thresholding method
[*] threshold_renyi: Sahoo et al.'s thresholding method
[*] threshold_shanbhag: Shanbhag's fuzzy thresholding method
[*] threshold_yen: Yen's thresholding method
[*] threshold_bernsen: Bernsen's thresholding method
[*] threshold_niblack: Niblack's thresholding method
[*] threshold_sauvola: Sauvola's thresholding method
[*] threshold_savakis: Savakis's thresholding method
[*] threshold_savakis_opt: Optimized version of Savakis's thresholding method


Kod który chciałbym dołączyć to:
KOD cpp:     UKRYJ  
/**
 * @file threshold.c
 * Routines for thresholding (binarizing) a grayscale image
 */


#include <image.h>

/**
 * @brief Thresholds a grayscale image to obtain a binary image
 *
 * @param[in] gray_img Image pointer { grayscale }
 * @param[in] threshold Threshold level { [0, MAX_GRAY] }
 *
 * @return Pointer to the binary image or NULL
 *
 * @author M. Emre Celebi
 * @date 10.15.2006
 */


Image *
threshold_img ( const Image * gray_img, const int threshold )
{
 SET_FUNC_NAME ( "threshold_img" );
 byte *gray_data;
 byte *bin_data;
 int ih;
 int num_rows, num_cols;
 int num_pixels;
 Image *bin_img;

 if ( !is_gray_img ( gray_img ) )
  {
   ERROR_RET ( "Not a grayscale image !", NULL );
  }

 if ( ( threshold < 0 ) || ( threshold > MAX_GRAY ) )
  {
   ERROR ( "Threshold ( %d ) must be in [0, %d] range !", threshold, MAX_GRAY );
   return NULL;
  }

 num_rows = get_num_rows ( gray_img );
 num_cols = get_num_cols ( gray_img );
 num_pixels = num_rows * num_cols;

 gray_data = get_img_data_1d ( gray_img );

 bin_img = alloc_img ( PIX_BIN, num_rows, num_cols );
 if ( IS_NULL ( bin_img ) )
  {
   ERROR_RET ( "Insufficient memory !", NULL );
  }

 bin_data = get_img_data_1d ( bin_img );

 /* Threshold pixels */
 for ( ih = 0; ih < num_pixels; ih++ )
  {
   bin_data[ih] = ( ( gray_data[ih] > threshold ) ? OBJECT : BACKGROUND );
  }

 return bin_img;
}
 

KOD cpp:     UKRYJ  
/**
 * @file threshold_bernsen.c
 * Routines for thresholding (binarizing) a grayscale image
 */


#include <image.h>

/**
 * @brief Implements Bernsen's thresholding method
 *
 * @param[in] in_img Image pointer { grayscale }
 * @param[in] win_size Dimension of the thresholding window { positive-odd }
 * @param[in] contrast_threshold Contrast threshold { non-negative }
 *            
 * @return Pointer to the resulting binary image or NULL
 *
 * @reco Bernsen recommends WIN_SIZE = 31 and CONTRAST_THRESHOLD = 15.
 *
 * @ref 1) Bernsen J. (1986) "Dynamic Thresholding of Grey-Level Images"
 *      Proc. of the 8th Int. Conf. on Pattern Recognition, pp. 1251-1255
 *      2) Sezgin M. and Sankur B. (2004) "Survey over Image Thresholding
 *         Techniques and Quantitative Performance Evaluation" Journal of
 *         Electronic Imaging, 13(1): 146-165
 *         http://citeseer.ist.psu.edu/sezgin04survey.html
 *
 * @author M. Emre Celebi
 * @date 07.26.2007
 */


Image *
threshold_bernsen ( const Image * in_img, const int win_size,
                    const int contrast_threshold )
{
 SET_FUNC_NAME ( "threshold_bernsen" );
 byte **in_data;
 byte **out_data;
 int num_rows, num_cols;
 int half_win;
 int win_count;                 /* number of pixels in the filtering window */
 int ir, ic;
 int iwr, iwc;
 int r_begin, r_end;            /* vertical limits of the filtering operation */
 int c_begin, c_end;            /* horizontal limits of the filtering operation */
 int wr_begin, wr_end;          /* vertical limits of the filtering window */
 int wc_begin, wc_end;          /* horizontal limits of the filtering window */
 int gray_val;
 int min_gray, max_gray;        /* min and max gray values in a particular window */
 int mid_gray;                  /* mid range of gray values in a particular window */
 int local_contrast;            /* contrast in a particular window */
 Image *out_img;

 if ( !is_gray_img ( in_img ) )
  {
   ERROR_RET ( "Not a grayscale image !", NULL );
  }

 if ( !IS_POS_ODD ( win_size ) )
  {
   ERROR ( "Window size ( %d ) must be positive and odd !", win_size );
   return NULL;
  }

 if ( contrast_threshold < 0 )
  {
   ERROR ( "Contrast threshold ( %d ) must be non-negative !",
           contrast_threshold );
   return NULL;
  }

 half_win = win_size / 2;
 win_count = win_size * win_size;

 num_rows = get_num_rows ( in_img );
 num_cols = get_num_cols ( in_img );
 in_data = get_img_data_nd ( in_img );

 out_img = alloc_img ( PIX_BIN, num_rows, num_cols );
 if ( IS_NULL ( out_img ) )
  {
   ERROR_RET ( "Insufficient memory !", NULL );
  }

 out_data = get_img_data_nd ( out_img );

 /*
    Determine the limits of the filtering operation. Pixels
    in the output image outside these limits are set to 0.
  */

 r_begin = half_win;
 r_end = num_rows - half_win;
 c_begin = half_win;
 c_end = num_cols - half_win;

 /* Initialize the vertical limits of the filtering window */
 wr_begin = 0;
 wr_end = win_size;

 /* For each image row */
 for ( ir = r_begin; ir < r_end; ir++ )
  {
   /* Initialize the horizontal limits of the filtering window */
   wc_begin = 0;
   wc_end = win_size;

   /* For each image column */
   for ( ic = c_begin; ic < c_end; ic++ )
    {
     min_gray = MAX_GRAY;
     max_gray = 0;
     /* For each window row */
     for ( iwr = wr_begin; iwr < wr_end; iwr++ )
      {
       /* For each window column */
       for ( iwc = wc_begin; iwc < wc_end; iwc++ )
        {
         /* Determine the min and max gray values */
         gray_val = in_data[iwr][iwc];
         if ( gray_val < min_gray )
          {
           min_gray = gray_val;
          }
         if ( max_gray < gray_val )
          {
           max_gray = gray_val;
          }
        }
      }

     /* Calculate the local contrast and mid range */
     local_contrast = max_gray - min_gray;
     mid_gray = 0.5 * ( min_gray + max_gray );

     /* Determine the output pixel value */
     if ( local_contrast < contrast_threshold )
      {
       /* Low contrast region */
       out_data[ir][ic] = ( mid_gray >= 128 ) ? OBJECT : BACKGROUND;
      }
     else
      {
       out_data[ir][ic] = ( in_data[ir][ic] >= mid_gray ) ? OBJECT : BACKGROUND;
      }

     /* Update the horizontal limits of the filtering window */
     wc_begin++;
     wc_end++;
    }

   /* Update the vertical limits of the filtering window */
   wr_begin++;
   wr_end++;
  }

 return out_img;
}
 

KOD cpp:     UKRYJ  
 
/**
 * @file threshold_huang.c
 * Routines for thresholding (binarizing) a grayscale image
 */


#include <image.h>

/** @cond INTERNAL_MACRO */
#define IS_IN_0_1_EXC( x ) ( ! ( ( ( x ) < 1e-06 ) || ( ( x ) > 0.999999 ) ) )

/** @endcond INTERNAL_MACRO */

/**
 * @brief Implements Huang's fuzzy thresholding method
 *
 * @param[in] img Image pointer { grayscale }
 *
 * @note Uses Shannon's entropy function (one can also use Yager's entropy function)
 * @return Threshold value or INT_MIN
 *
 * @ref Huang L.-K. and Wang M.-J.J. (1995) "Image Thresholding by Minimizing  
 *      the Measures of Fuzziness" Pattern Recognition, 28(1): 41-51
 *
 * @author M. Emre Celebi
 * @date 06.15.2007
 */


int
threshold_huang ( const Image * img )
{
 SET_FUNC_NAME ( "threshold_huang" );
 int ih, it;
 int threshold;
 int first_bin;
 int last_bin;
 int sum_pix;
 int num_pix;
 int *data;                     /* histogram data */
 double term;
 double ent;                    /* entropy */
 double min_ent;                /* min entropy */
 double mu_x;
 double *mu_0, *mu_1;
 Histo *histo;                  /* histogram */

 if ( !is_gray_img ( img ) )
  {
   ERROR_RET ( "Not a grayscale image !", INT_MIN );
  }

 /* Calculate the histogram */
 histo = create_histo ( img );
 if ( IS_NULL ( histo ) )
  {
   ERROR_RET ( "create_histo() failed !", INT_MIN );
  }

 data = get_histo_data ( histo );

 /* Determine the first non-zero bin */
 first_bin = 0;
 for ( ih = 0; ih < NUM_GRAY; ih++ )
  {
   if ( data[ih] != 0 )
    {
     first_bin = ih;
     break;
    }
  }

 /* Determine the last non-zero bin */
 last_bin = MAX_GRAY;
 for ( ih = MAX_GRAY; ih >= first_bin; ih-- )
  {
   if ( data[ih] != 0 )
    {
     last_bin = ih;
     break;
    }
  }

 /*
    Equation (4) in Ref. 1
    C = g_max - g_min
    This is the term ( 1 / C )
  */

 term = 1.0 / ( double ) ( last_bin - first_bin );

 /* Equation (2) in Ref. 1 */
 mu_0 = ( double * ) calloc ( NUM_GRAY, sizeof ( double ) );

 sum_pix = num_pix = 0;
 for ( ih = first_bin; ih < NUM_GRAY; ih++ )
  {
   sum_pix += ih * data[ih];
   num_pix += data[ih];

   /* NUM_PIX cannot be zero ! */
   mu_0[ih] = sum_pix / ( double ) num_pix;
  }

 /* Equation (3) in Ref. 1 */
 mu_1 = ( double * ) calloc ( NUM_GRAY, sizeof ( double ) );

 sum_pix = num_pix = 0;
 for ( ih = last_bin; ih > 0; ih-- )
  {
   sum_pix += ih * data[ih];
   num_pix += data[ih];

   /* NUM_PIX cannot be zero ! */
   mu_1[ih - 1] = sum_pix / ( double ) num_pix;
  }

 /* Determine the threshold that minimizes the fuzzy entropy */
 threshold = INT_MIN;
 min_ent = DBL_MAX;
 for ( it = 0; it < NUM_GRAY; it++ )
  {
   ent = 0.0;

   for ( ih = 0; ih <= it; ih++ )
    {
     /* Equation (4) in Ref. 1 */
     mu_x = 1.0 / ( 1.0 + term * fabs ( ih - mu_0[it] ) );
     if ( IS_IN_0_1_EXC ( mu_x ) )
      {
       /* Equation (6) & (8) in Ref. 1 */
       ent +=
        data[ih] * ( -mu_x * log ( mu_x ) -
                     ( 1.0 - mu_x ) * log ( 1.0 - mu_x ) );
      }
    }

   for ( ih = it + 1; ih < NUM_GRAY; ih++ )
    {
     /* Equation (4) in Ref. 1 */
     mu_x = 1.0 / ( 1.0 + term * fabs ( ih - mu_1[it] ) );
     if ( IS_IN_0_1_EXC ( mu_x ) )
      {
       /* Equation (6) & (8) in Ref. 1 */
       ent +=
        data[ih] * ( -mu_x * log ( mu_x ) -
                     ( 1.0 - mu_x ) * log ( 1.0 - mu_x ) );
      }
    }

   /* No need to divide by NUM_ROWS * NUM_COLS * LOG(2) ! */

   if ( ent < min_ent )
    {
     min_ent = ent;
     threshold = it;
    }
  }

 free_histo ( histo );
 free ( mu_0 );
 free ( mu_1 );

 return threshold;
}

#undef IS_IN_0_1_EXC
   

KOD cpp:     UKRYJ  
   

/**
 * @file threshold_iter.c
 * Routines for thresholding (binarizing) a grayscale image
 */


#include <image.h>

/**
 * @brief Implements the Iterative Selection thresholding method
 *
 * @param[in] img Image pointer { grayscale }
 *
 * @return Threshold value or INT_MIN
 *
 * @note This implementation is based on the fast version described in Ref. 2
 * @ref 1) Ridler T.W. and Calvard S. (1978) "Picture Thresholding Using an Iterative
 *         Selection Method" IEEE Trans. on System, Man and Cybernetics SMC-8(8): 630-632
 *      2) Trussell H.J. (1979) "Comments on Picture Thresholding Using an Iterative Selection
 *         Method" IEEE Trans. on Systems, Man, and Cybernetics, SMC-9(5): 311.
 *
 * @author M. Emre Celebi
 * @date 06.15.2007
 */


int
threshold_iter ( const Image * img )
{
 SET_FUNC_NAME ( "threshold_iter" );
 int ih;
 int num_back;                  /* number of background pixels at a given threshold */
 int num_obj;                   /* number of object pixels at a given threshold */
 int num_tot;                   /* total number of pixels (background + object) */
 int sum_back;                  /* sum of background pixels at a given threshold */
 int sum_obj;                   /* sum of object pixels at a given threshold */
 int sum_tot;                   /* total sum of the pixels */
 int threshold;
 int *data;                     /* histogram data */
 double old_thresh;
 double new_thresh;
 double mean_back;              /* mean of the background pixels at a given threshold */
 double mean_obj;               /* mean of the object pixels at a given threshold */
 double mean;                   /* mean gray-level in the image */
 double tolerance;              /* iteration tolerance */
 Histo *histo;                  /* histogram */

 tolerance = 0.5;

 if ( !is_gray_img ( img ) )
  {
   ERROR_RET ( "Not a grayscale image !", INT_MIN );
  }

 /* Calculate the histogram */
 histo = create_histo ( img );
 if ( IS_NULL ( histo ) )
  {
   ERROR_RET ( "create_histo() failed !", INT_MIN );
  }

 data = get_histo_data ( histo );
 num_tot = get_num_pixels ( histo );

 /* Calculate the mean gray-level */
 sum_tot = 0;
 for ( ih = 0 + 1; ih < NUM_GRAY; ih++ )
  {
   sum_tot += ih * data[ih];
  }

 mean = sum_tot / ( double ) num_tot;

 /* Initial estimate */
 new_thresh = mean;

 do
  {
   old_thresh = new_thresh;
   threshold = old_thresh + 0.5;        /* round */

   /* Calculate the means of background and object pixels */

   /* Background */
   sum_back = 0;
   num_back = 0;
   for ( ih = 0; ih <= threshold; ih++ )
    {
     sum_back += ih * data[ih];
     num_back += data[ih];
    }
   mean_back = ( num_back == 0 ? 0.0 : ( sum_back / ( double ) num_back ) );

   /* Object */
   sum_obj = sum_tot - sum_back;
   num_obj = num_tot - num_back;
   mean_obj = ( num_obj == 0 ? 0.0 : ( sum_obj / ( double ) num_obj ) );

   /* Calculate the new threshold */
   new_thresh = ( mean_back + mean_obj ) / 2.0;

   /*
      Stop the iterations when the difference between the
      new and old threshold values is less than the tolerance
    */

  }
 while ( fabs ( new_thresh - old_thresh ) > tolerance );

 free_histo ( histo );

 return threshold;
}

 


KOD cpp:     UKRYJ  
 
/**
 * @file threshold_kapur.c
 * Routines for thresholding (binarizing) a grayscale image
 */


#include <image.h>

/**
 * @brief Implements the Kapur-Sahoo-Wong (Maximum Entropy) thresholding method
 *
 * @param[in] img Image pointer { grayscale }
 *
 * @return Threshold value or INT_MIN
 *
 * @ref Kapur J.N., Sahoo P.K., and Wong A.K.C. (1985) "A New Method for
   Gray-Level Picture Thresholding Using the Entropy of the Histogram"
   Graphical Models and Image Processing, 29(3): 273-285
 *
 * @author M. Emre Celebi
 * @date 06.15.2007
 */


int
threshold_kapur ( const Image * img )
{
 SET_FUNC_NAME ( "threshold_kapur" );
 int ih, it;
 int threshold;
 int first_bin;                 /* see below */
 int last_bin;                  /* see below */
 double tot_ent;                /* total entropy */
 double max_ent;                /* max entropy */
 double ent_back;               /* entropy of the background pixels at a given threshold */
 double ent_obj;                /* entropy of the object pixels at a given threshold */
 double *data;                  /* normalized histogram data */
 double *P1;                    /* cumulative normalized histogram */
 double *P2;                    /* see below */
 Histo *norm_histo;             /* normalized histogram */

 if ( !is_gray_img ( img ) )
  {
   ERROR_RET ( "Not a grayscale image !", INT_MIN );
  }

 /* Calculate the normalized histogram */
 norm_histo = normalize_histo ( create_histo ( img ) );
 if ( IS_NULL ( norm_histo ) )
  {
   ERROR_RET ( "normalize_histo() failed !", INT_MIN );
  }

 data = get_histo_data ( norm_histo );

 /* Calculate the cumulative normalized histogram */
 P1 = accumulate_histo ( norm_histo );

 P2 = ( double * ) malloc ( NUM_GRAY * sizeof ( double ) );

 for ( ih = 0; ih < NUM_GRAY; ih++ )
  {
   P2[ih] = 1.0 - P1[ih];
  }

 /*
    Determine the first non-zero
    bin starting from the first bin
  */

 first_bin = 0;
 for ( ih = 0; ih < NUM_GRAY; ih++ )
  {
   if ( !IS_ZERO ( P1[ih] ) )
    {
     first_bin = ih;
     break;
    }
  }

 /*
    Determine the first non-one bin
    starting from the last bin
  */

 last_bin = MAX_GRAY;
 for ( ih = MAX_GRAY; ih >= first_bin; ih-- )
  {
   if ( !IS_ZERO ( P2[ih] ) )
    {
     last_bin = ih;
     break;
    }
  }

 /*
    Calculate the total entropy each gray-level
    and find the threshold that maximizes it
  */

 threshold = INT_MIN;
 max_ent = DBL_MIN;
 for ( it = first_bin; it <= last_bin; it++ )
  {
   /* Entropy of the background pixels */
   ent_back = 0.0;
   for ( ih = 0; ih <= it; ih++ )
    {
     if ( !IS_ZERO ( data[ih] ) )
      {
       ent_back -= ( data[ih] / P1[it] ) * log ( data[ih] / P1[it] );
      }
    }

   /* Entropy of the object pixels */
   ent_obj = 0.0;
   for ( ih = it + 1; ih < NUM_GRAY; ih++ )
    {
     if ( !IS_ZERO ( data[ih] ) )
      {
       ent_obj -= ( data[ih] / P2[it] ) * log ( data[ih] / P2[it] );
      }
    }

   /* Total entropy */
   tot_ent = ent_back + ent_obj;

   if ( max_ent < tot_ent )
    {
     max_ent = tot_ent;
     threshold = it;
    }
  }

 free_histo ( norm_histo );
 free ( P1 );
 free ( P2 );

 return threshold;
}
 


KOD cpp:     UKRYJ  
/**
 * @file threshold_li.c
 * Routines for thresholding (binarizing) a grayscale image
 */


#include <image.h>

static int simple_round ( double x );

/**
 * @brief Implements Li's Minimum Cross Entropy thresholding method
 *
 * @param[in] img Image pointer { grayscale }
 *
 * @return Threshold value or INT_MIN
 *
 * @note This implementation is based on the iterative version (Ref. 2) of the algorithm.
 * @ref 1) Li C.H. and Lee C.K. (1993) "Minimum Cross Entropy Thresholding"
 *         Pattern Recognition, 26(4): 617-625
 *      2) Li C.H. and Tam P.K.S. (1998) "An Iterative Algorithm for Minimum
 *         Cross Entropy Thresholding"Pattern Recognition Letters, 18(8): 771-776
 *      3) Sezgin M. and Sankur B. (2004) "Survey over Image Thresholding
 *         Techniques and Quantitative Performance Evaluation" Journal of
 *         Electronic Imaging, 13(1): 146-165
 *         http://citeseer.ist.psu.edu/sezgin04survey.html
 *
 * @author M. Emre Celebi
 * @date 06.23.2007
 */


static int
simple_round ( double x )
{
 return ( int ) ( IS_NEG ( x ) ? x - .5 : x + .5 );
}

int
threshold_li ( const Image * img )
{
 SET_FUNC_NAME ( "threshold_li" );
 int ih;
 int num_pixels;
 int sum_back;                  /* sum of the background pixels at a given threshold */
 int sum_obj;                   /* sum of the object pixels at a given threshold */
 int num_back;                  /* number of background pixels at a given threshold */
 int num_obj;                   /* number of object pixels at a given threshold */
 int threshold;
 int *data;                     /* histogram data */
 double old_thresh;
 double new_thresh;
 double mean_back;              /* mean of the background pixels at a given threshold */
 double mean_obj;               /* mean of the object pixels at a given threshold */
 double mean;                   /* mean gray-level in the image */
 double tolerance;              /* threshold tolerance */
 Histo *histo;                  /* histogram */

 tolerance = 0.5;

 if ( !is_gray_img ( img ) )
  {
   ERROR_RET ( "Not a grayscale image !", INT_MIN );
  }

 /* Calculate the normalized histogram */
 histo = create_histo ( img );
 if ( IS_NULL ( histo ) )
  {
   ERROR_RET ( "create_histo() failed !", INT_MIN );
  }

 data = get_histo_data ( histo );
 num_pixels = get_num_pixels ( histo );

 /* Calculate the mean gray-level */
 mean = 0.0;
 for ( ih = 0 + 1; ih < NUM_GRAY; ih++ )
  {
   mean += ih * data[ih];
  }

 mean /= num_pixels;

 /* Initial estimate */
 new_thresh = mean;

 do
  {
   old_thresh = new_thresh;
   threshold = old_thresh + 0.5;        /* range */

   /* Calculate the means of background and object pixels */

   /* Background */
   sum_back = 0;
   num_back = 0;
   for ( ih = 0; ih <= threshold; ih++ )
    {
     sum_back += ih * data[ih];
     num_back += data[ih];
    }
   mean_back = ( num_back == 0 ? 0.0 : ( sum_back / ( double ) num_back ) );

   /* Object */
   sum_obj = 0;
   num_obj = 0;
   for ( ih = threshold + 1; ih < NUM_GRAY; ih++ )
    {
     sum_obj += ih * data[ih];
     num_obj += data[ih];
    }
   mean_obj = ( num_obj == 0 ? 0.0 : ( sum_obj / ( double ) num_obj ) );

   /* Calculate the new threshold: Equation (7) in Ref. 2 */
   new_thresh =
    simple_round ( ( mean_back - mean_obj ) / ( log ( mean_back ) -
                                                log ( mean_obj ) ) );

   /*
      Stop the iterations when the difference between the
      new and old threshold values is less than the tolerance
    */

  }
 while ( fabs ( new_thresh - old_thresh ) > tolerance );

 free_histo ( histo );

 return threshold;
}
 

KOD cpp:     UKRYJ  
 
/**
 * @file threshold_moment.c
 * Routines for thresholding (binarizing) a grayscale image
 */


#include <image.h>

/**
 * @brief Implements the Moment-Preserving thresholding method
 *
 * @param[in] img Image pointer { grayscale }
 *
 * @return Threshold value or INT_MIN
 *
 * @ref Tsai W.H. (1985) "Moment-Preserving Thresholding: A New Approach"
 *      Graphical Models and Image Processing, 29(3): 377-393
 *
 * @author M. Emre Celebi
 * @date 06.15.2007
 */


int
threshold_moment ( const Image * img )
{
 SET_FUNC_NAME ( "threshold_moment" );
 int ih;
 int threshold;
 double m0, m1, m2, m3;         /* first 4 moments of the gray-level image */
 double cd, c0, c1, z0, z1;     /* auxiliary variables */
 double p0;                     /* fraction of the object pixels in the target binary image */
 double sum;
 double *data;                  /* normalized histogram data */
 Histo *norm_histo;             /* normalized histogram */

 if ( !is_gray_img ( img ) )
  {
   ERROR_RET ( "Not a grayscale image !", INT_MIN );
  }

 /* Calculate the normalized histogram */
 norm_histo = normalize_histo ( create_histo ( img ) );
 if ( IS_NULL ( norm_histo ) )
  {
   ERROR_RET ( "normalize_histo() failed !", INT_MIN );
  }

 data = get_histo_data ( norm_histo );

 /* Zeroth order moment */
 m0 = 1.0;

 /* Calculate the first, second, and third order moments */
 m1 = m2 = m3 = 0.0;
 for ( ih = 0; ih < NUM_GRAY; ih++ )
  {
   m1 += ih * data[ih];
   m2 += ih * ih * data[ih];
   m3 += ih * ih * ih * data[ih];
  }

 /*
    First 4 moments of the gray-level image should match the first 4 moments
    of the target binary image. This leads to 4 equalities whose solutions
    are given in the Appendix of Ref. 1
  */

 cd = m0 * m2 - m1 * m1;
 c0 = ( -m2 * m2 + m1 * m3 ) / cd;
 c1 = ( m0 * -m3 + m2 * m1 ) / cd;
 z0 = 0.5 * ( -c1 - sqrt ( c1 * c1 - 4.0 * c0 ) );
 z1 = 0.5 * ( -c1 + sqrt ( c1 * c1 - 4.0 * c0 ) );

 /* Fraction of the object pixels in the target binary image */
 p0 = ( z1 - m1 ) / ( z1 - z0 );

 /*
    The threshold is the gray-level closest  
    to the p0-tile of the normalized histogram
  */

 threshold = INT_MIN;
 sum = 0.0;
 for ( ih = 0; ih < NUM_GRAY; ih++ )
  {
   sum += data[ih];
   if ( sum > p0 )
    {
     threshold = ih;
     break;
    }
  }

 free_histo ( norm_histo );

 return threshold;
}
 


KOD cpp:     UKRYJ  
 
/**
 * @file threshold_niblack.c
 * Routines for thresholding (binarizing) a grayscale image
 */


#include <image.h>

/**
 * @brief Implements Niblack's thresholding method
 *
 * @param[in] in_img Image pointer { grayscale }
 * @param[in] win_size Dimension of the thresholding window { positive-odd }
 * @param[in] k_value Determines how much of the foreground object
 *                    edges that are taken as a part of the object
 *            
 * @return Pointer to the resulting binary image or NULL
 *
 * @reco Niblack recommends K_VALUE = -0.2 for images with black foreground
 *       objects, and K_VALUE = +0.2 for images with white foreground objects.
 *
 * @ref Niblack W. (1986) "An introduction to Digital Image Processing" Prentice-Hall
 *
 * @author M. Emre Celebi
 * @date 07.25.2007
 */


Image *
threshold_niblack ( const Image * in_img, const int win_size,
                    const double k_value )
{
 SET_FUNC_NAME ( "threshold_niblack" );
 byte **in_data;
 byte **out_data;
 int num_rows, num_cols;
 int half_win;
 int win_count;                 /* number of pixels in the filtering window */
 int ir, ic;
 int iwr, iwc;
 int r_begin, r_end;            /* vertical limits of the filtering operation */
 int c_begin, c_end;            /* horizontal limits of the filtering operation */
 int wr_begin, wr_end;          /* vertical limits of the filtering window */
 int wc_begin, wc_end;          /* horizontal limits of the filtering window */
 int gray_val;
 int sum, sum_sq;               /* temp variables used in the calculation of local mean and variances */
 int threshold;
 double local_mean;             /* gray level mean in a particular window position */
 double local_var;              /* gray level variance in a particular window position */
 Image *out_img;

 if ( !is_gray_img ( in_img ) )
  {
   ERROR_RET ( "Not a grayscale image !", NULL );
  }

 if ( !IS_POS_ODD ( win_size ) )
  {
   ERROR ( "Window size ( %d ) must be positive and odd !", win_size );
   return NULL;
  }

 half_win = win_size / 2;
 win_count = win_size * win_size;

 num_rows = get_num_rows ( in_img );
 num_cols = get_num_cols ( in_img );
 in_data = get_img_data_nd ( in_img );

 out_img = alloc_img ( PIX_BIN, num_rows, num_cols );
 if ( IS_NULL ( out_img ) )
  {
   ERROR_RET ( "Insufficient memory !", NULL );
  }

 out_data = get_img_data_nd ( out_img );

 /*
    Determine the limits of the filtering operation. Pixels
    in the output image outside these limits are set to 0.
  */

 r_begin = half_win;
 r_end = num_rows - half_win;
 c_begin = half_win;
 c_end = num_cols - half_win;

 /* Initialize the vertical limits of the filtering window */
 wr_begin = 0;
 wr_end = win_size;

 /* For each image row */
 for ( ir = r_begin; ir < r_end; ir++ )
  {
   /* Initialize the horizontal limits of the filtering window */
   wc_begin = 0;
   wc_end = win_size;

   /* For each image column */
   for ( ic = c_begin; ic < c_end; ic++ )
    {
     sum = sum_sq = 0;
     /* For each window row */
     for ( iwr = wr_begin; iwr < wr_end; iwr++ )
      {
       /* For each window column */
       for ( iwc = wc_begin; iwc < wc_end; iwc++ )
        {
         sum += ( gray_val = in_data[iwr][iwc] );
         sum_sq += gray_val * gray_val;
        }
      }

     /* Calculate the local mean and variance */
     local_mean = sum / ( double ) win_count;
     local_var = ( sum_sq / ( double ) win_count ) - local_mean * local_mean;

     /* Calculate local threshold */
     threshold = local_mean + k_value * sqrt ( local_var );

     /* Determine the output pixel value */
     out_data[ir][ic] =
      ( ( in_data[ir][ic] > threshold ) ? OBJECT : BACKGROUND );

     /* Update the horizontal limits of the filtering window */
     wc_begin++;
     wc_end++;
    }

   /* Update the vertical limits of the filtering window */
   wr_begin++;
   wr_end++;
  }

 return out_img;
}
 


KOD cpp:     UKRYJ  
/**
 * @file threshold_otsu.c
 * Routines for thresholding (binarizing) a grayscale image
 */


#include <image.h>

/**
 * @brief Implements Otsu's thresholding method
 *
 * @param[in] img Image pointer { grayscale }
 *
 * @return Threshold value or INT_MIN
 *
 * @ref Otsu N. (1979) "A Threshold Selection Method from Gray Level Histograms"
 *      IEEE Trans. on Systems, Man and Cybernetics, 9(1): 62-66
 *
 * @author M. Emre Celebi
 * @date 06.15.2007
 */


int
threshold_otsu ( const Image * img )
{
 SET_FUNC_NAME ( "threshold_otsu" );
 int ih;
 int threshold;
 int first_bin;                 /* see below */
 int last_bin;                  /* see below */
 double total_mean;             /* mean gray-level for the whole image */
 double bcv;                    /* between-class variance */
 double max_bcv;                /* max BCV */
 double *cnh;                   /* cumulative normalized histogram */
 double *mean;                  /* mean gray-level */
 double *data;                  /* normalized histogram data */
 Histo *norm_histo;             /* normalized histogram */

 if ( !is_gray_img ( img ) )
  {
   ERROR_RET ( "Not a grayscale image !", INT_MIN );
  }

 /* Calculate the normalized histogram */
 norm_histo = normalize_histo ( create_histo ( img ) );
 if ( IS_NULL ( norm_histo ) )
  {
   ERROR_RET ( "normalize_histo() failed !", INT_MIN );
  }

 data = get_histo_data ( norm_histo );

 /* Calculate the cumulative normalized histogram */
 cnh = accumulate_histo ( norm_histo );

 mean = ( double * ) malloc ( NUM_GRAY * sizeof ( double ) );

 mean[0] = 0.0;
 for ( ih = 0 + 1; ih < NUM_GRAY; ih++ )
  {
   mean[ih] = mean[ih - 1] + ih * data[ih];
  }

 total_mean = mean[MAX_GRAY];

 /*
    Determine the first non-zero
    bin starting from the first bin
  */

 first_bin = 0;
 for ( ih = 0; ih < NUM_GRAY; ih++ )
  {
   if ( !IS_ZERO ( cnh[ih] ) )
    {
     first_bin = ih;
     break;
    }
  }

 /*
    Determine the first non-one bin
    starting from the last bin
  */

 last_bin = MAX_GRAY;
 for ( ih = MAX_GRAY; ih >= first_bin; ih-- )
  {
   if ( !IS_ZERO ( 1.0 - cnh[ih] ) )
    {
     last_bin = ih;
     break;
    }
  }

 /*
    Calculate the BCV at each gray-level and
    find the threshold that maximizes it
  */

 threshold = INT_MIN;
 max_bcv = 0.0;
 for ( ih = first_bin; ih <= last_bin; ih++ )
  {
   bcv = total_mean * cnh[ih] - mean[ih];
   bcv *= bcv / ( cnh[ih] * ( 1.0 - cnh[ih] ) );

   if ( max_bcv < bcv )
    {
     max_bcv = bcv;
     threshold = ih;
    }
  }

 free_histo ( norm_histo );
 free ( cnh );
 free ( mean );

 return threshold;
}
 


KOD cpp:     UKRYJ  
/**
 * @file threshold_renyi.c
 * Routines for thresholding (binarizing) a grayscale image
 */


#include <image.h>

/** @cond INTERNAL_MACRO */
#define SWAP_INT( a, b ) { tmp_var = ( a ); ( a ) = ( b ); ( b ) = tmp_var; }

/** @endcond INTERNAL_MACRO */

/**
 * @brief Implements Sahoo et al.'s thresholding method
 *
 * @param[in] img Image pointer { grayscale }
 *
 * @return Threshold value or INT_MIN
 *
 * @ref Sahoo P.K., Wilkins C., and Yeager J. (1997) "Threshold Selection
 *      Using Renyi's Entropy" Pattern Recognition, 30(1): 71-84
 *
 * @author M. Emre Celebi
 * @date 06.23.2007
 */


int
threshold_renyi ( const Image * img )
{
 SET_FUNC_NAME ( "threshold_renyi" );
 int ih, it;
 int threshold;
 int opt_threshold;
 int first_bin;                 /* see below */
 int last_bin;                  /* see below */
 int tmp_var;
 int t_star1, t_star2, t_star3;
 int beta1, beta2, beta3;
 double alpha;                  /* alpha parameter of the method */
 double term;
 double tot_ent;                /* total entropy */
 double max_ent;                /* max entropy */
 double ent_back;               /* entropy of the background pixels at a given threshold */
 double ent_obj;                /* entropy of the object pixels at a given threshold */
 double omega;
 double *data;                  /* normalized histogram data */
 double *P1;                    /* cumulative normalized histogram */
 double *P2;                    /* see below */
 Histo *norm_histo;             /* normalized histogram */

 if ( !is_gray_img ( img ) )
  {
   ERROR_RET ( "Not a grayscale image !", INT_MIN );
  }

 /* Calculate the normalized histogram */
 norm_histo = normalize_histo ( create_histo ( img ) );
 if ( IS_NULL ( norm_histo ) )
  {
   ERROR_RET ( "normalize_histo() failed !", INT_MIN );
  }

 data = get_histo_data ( norm_histo );

 /* Calculate the cumulative normalized histogram */
 P1 = accumulate_histo ( norm_histo );

 P2 = ( double * ) malloc ( NUM_GRAY * sizeof ( double ) );

 for ( ih = 0; ih < NUM_GRAY; ih++ )
  {
   P2[ih] = 1.0 - P1[ih];
  }

 /*
    Determine the first non-zero
    bin starting from the first bin
  */

 first_bin = 0;
 for ( ih = 0; ih < NUM_GRAY; ih++ )
  {
   if ( !IS_ZERO ( P1[ih] ) )
    {
     first_bin = ih;
     break;
    }
  }

 /*
    Determine the first non-one bin
    starting from the last bin
  */

 last_bin = MAX_GRAY;
 for ( ih = MAX_GRAY; ih >= first_bin; ih-- )
  {
   if ( !IS_ZERO ( P2[ih] ) )
    {
     last_bin = ih;
     break;
    }
  }

 /* Maximum Entropy Thresholding - BEGIN */
 /* ALPHA = 1.0 */
 /*
    Calculate the total entropy at each gray-level
    and find the threshold that maximizes it
  */

 threshold = INT_MIN;
 max_ent = 0.0;
 for ( it = first_bin; it <= last_bin; it++ )
  {
   /* Entropy of the background pixels */
   ent_back = 0.0;
   for ( ih = 0; ih <= it; ih++ )
    {
     if ( !IS_ZERO ( data[ih] ) )
      {
       ent_back -= ( data[ih] / P1[it] ) * log ( data[ih] / P1[it] );
      }
    }

   /* Entropy of the object pixels */
   ent_obj = 0.0;
   for ( ih = it + 1; ih < 256; ih++ )
    {
     if ( !IS_ZERO ( data[ih] ) )
      {
       ent_obj -= ( data[ih] / P2[it] ) * log ( data[ih] / P2[it] );
      }
    }

   /* Total entropy */
   tot_ent = ent_back + ent_obj;

   if ( tot_ent > max_ent )
    {
     max_ent = tot_ent;
     threshold = it;
    }
  }

 t_star2 = threshold;
 /* Maximum Entropy Thresholding - END */

 threshold = INT_MIN;
 max_ent = 0.0;
 alpha = 0.5;
 term = 1.0 / ( 1.0 - alpha );
 for ( it = first_bin; it <= last_bin; it++ )
  {
   /* Entropy of the background pixels */
   ent_back = 0.0;
   for ( ih = 0; ih <= it; ih++ )
    {
     ent_back += sqrt ( data[ih] / P1[it] );
    }

   /* Entropy of the object pixels */
   ent_obj = 0.0;
   for ( ih = it + 1; ih < 256; ih++ )
    {
     ent_obj += sqrt ( data[ih] / P2[it] );
    }

   /* Total entropy */
   tot_ent = term * SAFE_LOG ( ent_back * ent_obj );

   if ( tot_ent > max_ent )
    {
     max_ent = tot_ent;
     threshold = it;
    }
  }

 t_star1 = threshold;

 threshold = INT_MIN;
 max_ent = 0.0;
 alpha = 2.0;
 term = 1.0 / ( 1.0 - alpha );
 for ( it = first_bin; it <= last_bin; it++ )
  {
   /* Entropy of the background pixels */
   ent_back = 0.0;
   for ( ih = 0; ih <= it; ih++ )
    {
     ent_back += ( data[ih] * data[ih] ) / ( P1[it] * P1[it] );
    }

   /* Entropy of the object pixels */
   ent_obj = 0.0;
   for ( ih = it + 1; ih < 256; ih++ )
    {
     ent_obj += ( data[ih] * data[ih] ) / ( P2[it] * P2[it] );
    }

   /* Total entropy */
   tot_ent = term * SAFE_LOG ( ent_back * ent_obj );

   if ( tot_ent > max_ent )
    {
     max_ent = tot_ent;
     threshold = it;
    }
  }

 t_star3 = threshold;

 /* Sort t_star values */
 if ( t_star2 < t_star1 )
  {
   SWAP_INT ( t_star1, t_star2 );
  }
 if ( t_star3 < t_star2 )
  {
   SWAP_INT ( t_star2, t_star3 );
  }
 if ( t_star2 < t_star1 )
  {
   SWAP_INT ( t_star1, t_star2 );
  }

 /* Adjust beta values */
 if ( abs ( t_star1 - t_star2 ) <= 5 )
  {
   if ( abs ( t_star2 - t_star3 ) <= 5 )
    {
     beta1 = 1;
     beta2 = 2;
     beta3 = 1;
    }
   else
    {
     beta1 = 0;
     beta2 = 1;
     beta3 = 3;
    }
  }
 else
  {
   if ( abs ( t_star2 - t_star3 ) <= 5 )
    {
     beta1 = 3;
     beta2 = 1;
     beta3 = 0;
    }
   else
    {
     beta1 = 1;
     beta2 = 2;
     beta3 = 1;
    }
  }

 /* Determine the optimal threshold value */
 omega = P1[t_star3] - P1[t_star1];
 opt_threshold = t_star1 * ( P1[t_star1] + 0.25 * omega * beta1 )
  + 0.25 * t_star2 * omega * beta2
  + t_star3 * ( P2[t_star3] + 0.25 * omega * beta3 );

 free_histo ( norm_histo );
 free ( P1 );
 free ( P2 );

 return opt_threshold;
}

#undef SWAP_INT
 


KOD cpp:     UKRYJ  
/**
 * @file threshold_sauvola.c
 * Routines for thresholding (binarizing) a grayscale image
 */


#include <image.h>

/**
 * @brief Implements Sauvola's thresholding method
 *
 * @param[in] in_img Image pointer { grayscale }
 * @param[in] win_size Dimension of the thresholding window { positive-odd }
 * @param[in] k_value Determines how much of the foreground object
 *                    edges that are taken as a part of the object
 * @param[in] R_value Dynamic range of standard deviation
 *            
 * @return Pointer to the resulting binary image or NULL
 *
 * @reco Sauvola recommends K_VALUE = 0.5 and R_VALUE = 128.
 * @note This is a modification of Niblack's thresholding method.
 *
 * @ref Sauvola J. and Pietaksinen M. (2000) "Adaptive Document Image Binarization"
 *      Pattern Recognition, 33(2): 225-236
 *      http://www.ee.oulu.fi/mvg/publications/ ... .php?ID=24
 *
 * @author M. Emre Celebi
 * @date 07.26.2007
 */


Image *
threshold_sauvola ( const Image * in_img, const int win_size,
                    const double k_value, const double R_value )
{
 SET_FUNC_NAME ( "threshold_sauvola" );
 byte **in_data;
 byte **out_data;
 int num_rows, num_cols;
 int half_win;
 int win_count;                 /* number of pixels in the filtering window */
 int ir, ic;
 int iwr, iwc;
 int r_begin, r_end;            /* vertical limits of the filtering operation */
 int c_begin, c_end;            /* horizontal limits of the filtering operation */
 int wr_begin, wr_end;          /* vertical limits of the filtering window */
 int wc_begin, wc_end;          /* horizontal limits of the filtering window */
 int gray_val;
 int sum, sum_sq;               /* temp variables used in the calculation of local mean and variances */
 int threshold;
 double local_mean;             /* gray level mean in a particular window position */
 double local_var;              /* gray level variance in a particular window position */
 Image *out_img;

 if ( !is_gray_img ( in_img ) )
  {
   ERROR_RET ( "Not a grayscale image !", NULL );
  }

 if ( !IS_POS_ODD ( win_size ) )
  {
   ERROR ( "Window size ( %d ) must be positive and odd !", win_size );
   return NULL;
  }

 half_win = win_size / 2;
 win_count = win_size * win_size;

 num_rows = get_num_rows ( in_img );
 num_cols = get_num_cols ( in_img );
 in_data = get_img_data_nd ( in_img );

 out_img = alloc_img ( PIX_BIN, num_rows, num_cols );
 if ( IS_NULL ( out_img ) )
  {
   ERROR_RET ( "Insufficient memory !", NULL );
  }

 out_data = get_img_data_nd ( out_img );

 /*
    Determine the limits of the filtering operation. Pixels
    in the output image outside these limits are set to 0.
  */

 r_begin = half_win;
 r_end = num_rows - half_win;
 c_begin = half_win;
 c_end = num_cols - half_win;

 /* Initialize the vertical limits of the filtering window */
 wr_begin = 0;
 wr_end = win_size;

 /* For each image row */
 for ( ir = r_begin; ir < r_end; ir++ )
  {
   /* Initialize the horizontal limits of the filtering window */
   wc_begin = 0;
   wc_end = win_size;

   /* For each image column */
   for ( ic = c_begin; ic < c_end; ic++ )
    {
     sum = sum_sq = 0;
     /* For each window row */
     for ( iwr = wr_begin; iwr < wr_end; iwr++ )
      {
       /* For each window column */
       for ( iwc = wc_begin; iwc < wc_end; iwc++ )
        {
         sum += ( gray_val = in_data[iwr][iwc] );
         sum_sq += gray_val * gray_val;
        }
      }

     /* Calculate the local mean and variance */
     local_mean = sum / ( double ) win_count;
     local_var = ( sum_sq / ( double ) win_count ) - local_mean * local_mean;

     /* Calculate local threshold */
     threshold =
      local_mean * ( 1.0 +
                     k_value * ( ( sqrt ( local_var ) / R_value ) - 1.0 ) );

     /* Determine the output pixel value */
     out_data[ir][ic] =
      ( ( in_data[ir][ic] > threshold ) ? OBJECT : BACKGROUND );

     /* Update the horizontal limits of the filtering window */
     wc_begin++;
     wc_end++;
    }

   /* Update the vertical limits of the filtering window */
   wr_begin++;
   wr_end++;
  }

 return out_img;
}
 

KOD cpp:     UKRYJ  
/**
 * @file threshold_savakis.c
 * Routines for thresholding (binarizing) a grayscale image
 */


#include <image.h>

/**
 * @brief Implements Savakis's thresholding method
 *
 * @param[in] in_img Image pointer { grayscale }
 * @param[in] win_size Dimension of the thresholding window { positive-odd }
 * @param[in] global_thresholding_method Global thresholding method to be used
 *            
 * @return Pointer to the resulting binary image or NULL
 *
 * @ref 1) Savakis A.E. (1998) "Adaptive Document Image Thresholding Using Foreground
 *         and Background Clustering" Proc. of the IEEE Int. Conf. on Image Processing,
 *         3: 785-789
 *         http://www.ce.rit.edu/~savakis/papers/I ... avakis.pdf
 *      2) Duc D.A., Du T.L.H., and Duan T.D. (2004) "Optimizing Speed for Adaptive Local
 *         Thresholding Algorithm Using Dynamic Programming" 2004 Int. Conf. on Electronics,
 *         Information, and Communications (ICEIC’04), 1: 438-441
 *         http://www.fit.hcmuns.edu.vn/~daduc/files/iceic04.pdf
 *
 * @author M. Emre Celebi
 * @date 07.26.2007
 */


Image *
threshold_savakis ( const Image * in_img, const int win_size,
                    int ( *global_thresholding_method ) ( const Image * ) )
{
 SET_FUNC_NAME ( "threshold_savakis" );
 byte **in_data;
 byte **out_data;
 int num_rows, num_cols;
 int half_win;
 int win_count;                 /* number of pixels in the filtering window */
 int ir, ic;
 int iwr, iwc;
 int r_begin, r_end;            /* vertical limits of the filtering operation */
 int c_begin, c_end;            /* horizontal limits of the filtering operation */
 int wr_begin, wr_end;          /* vertical limits of the filtering window */
 int wc_begin, wc_end;          /* horizontal limits of the filtering window */
 int gray_val;
 int num_back;                  /* number of background pixels in a particular window */
 int num_obj;                   /* number of object pixels in a particular window */
 int sum_back;                  /* sum of background pixels in a particular window */
 int sum_obj;                   /* sum of object pixels in a particular window */
 int global_threshold;
 int local_threshold;
 double mean_back;              /* mean of the background pixels in a particular window */
 double mean_obj;               /* mean of the object pixels in a particular window */
 Image *out_img;

 if ( !is_gray_img ( in_img ) )
  {
   ERROR_RET ( "Not a grayscale image !", NULL );
  }

 if ( !IS_POS_ODD ( win_size ) )
  {
   ERROR ( "Window size ( %d ) must be positive and odd !", win_size );
   return NULL;
  }

 half_win = win_size / 2;
 win_count = win_size * win_size;

 num_rows = get_num_rows ( in_img );
 num_cols = get_num_cols ( in_img );
 in_data = get_img_data_nd ( in_img );

 out_img = alloc_img ( PIX_BIN, num_rows, num_cols );
 if ( IS_NULL ( out_img ) )
  {
   ERROR_RET ( "Insufficient memory !", NULL );
  }

 out_data = get_img_data_nd ( out_img );

 /* Calculate the global threshold */
 global_threshold = global_thresholding_method ( in_img );

 /*
    Determine the limits of the filtering operation. Pixels
    in the output image outside these limits are set to 0.
  */

 r_begin = half_win;
 r_end = num_rows - half_win;
 c_begin = half_win;
 c_end = num_cols - half_win;

 /* Initialize the vertical limits of the filtering window */
 wr_begin = 0;
 wr_end = win_size;

 /* For each image row */
 for ( ir = r_begin; ir < r_end; ir++ )
  {
   /* Initialize the horizontal limits of the filtering window */
   wc_begin = 0;
   wc_end = win_size;

   /* For each image column */
   for ( ic = c_begin; ic < c_end; ic++ )
    {
     sum_back = 0;
     num_back = 0;
     sum_obj = 0;
     num_obj = 0;
     /* For each window row */
     for ( iwr = wr_begin; iwr < wr_end; iwr++ )
      {
       /* For each window column */
       for ( iwc = wc_begin; iwc < wc_end; iwc++ )
        {
         gray_val = in_data[iwr][iwc];

         /* Decide the cluster to which this pixel belongs */
         if ( gray_val <= global_threshold )
          {
           sum_back += gray_val;
           num_back++;
          }
         else
          {
           sum_obj += gray_val;
           num_obj++;
          }
        }
      }

     /* Calculate cluster means */
     mean_back = ( num_back == 0 ? 0.0 : ( sum_back / ( double ) num_back ) );
     mean_obj = ( num_obj == 0 ? 0.0 : ( sum_obj / ( double ) num_obj ) );

     /* Calculate the local threshold */
     local_threshold = 0.5 * ( mean_back + mean_obj );

     /* Determine the output pixel value */
     out_data[ir][ic] =
      ( ( in_data[ir][ic] > local_threshold ) ? OBJECT : BACKGROUND );

     /* Update the horizontal limits of the filtering window */
     wc_begin++;
     wc_end++;
    }

   /* Update the vertical limits of the filtering window */
   wr_begin++;
   wr_end++;
  }

 return out_img;
}
 

KOD cpp:     UKRYJ  
/**
 * @file threshold_savakis_opt.c
 * Routines for thresholding (binarizing) a grayscale image
 */


#include <image.h>

/**
 * @brief Implements Savakis's thresholding method
 *
 * @param[in] in_img Image pointer { grayscale }
 * @param[in] win_size Dimension of the thresholding window { positive-odd }
 * @param[in] global_thresholding_method Global thresholding method to be used
 *            
 * @return Pointer to the resulting binary image or NULL
 *
 * @ref 1) Savakis A.E. (1998) "Adaptive Document Image Thresholding Using Foreground
 *         and Background Clustering" Proc. of the IEEE Int. Conf. on Image Processing,
 *         3: 785-789
 *         http://www.ce.rit.edu/~savakis/papers/I ... avakis.pdf
 *      2) Duc D.A., Du T.L.H., and Duan T.D. (2004) "Optimizing Speed for Adaptive Local
 *         Thresholding Algorithm Using Dynamic Programming" 2004 Int. Conf. on Electronics,
 *         Information, and Communications (ICEIC’04), 1: 438-441
 *         http://www.fit.hcmuns.edu.vn/~daduc/files/iceic04.pdf
 *
 * @author M. Emre Celebi
 * @date 07.26.2007
 */


Image *
threshold_savakis_opt ( const Image * in_img, const int win_size,
                        int ( *global_thresholding_method ) ( const Image * ) )
{
 SET_FUNC_NAME ( "threshold_savakis_opt" );
 byte **in_data;
 byte **out_data;
 int num_rows, num_cols;
 int half_win;
 int win_count;                 /* number of pixels in the filtering window */
 int ir, ic;
 int r_begin, r_end;            /* vertical limits of the filtering operation */
 int c_begin, c_end;            /* horizontal limits of the filtering operation */
 int gray_val;
 int num_back;                  /* number of background pixels in a particular window */
 int num_obj;                   /* number of object pixels in a particular window */
 int sum_back;                  /* sum of background pixels in a particular window */
 int sum_obj;                   /* sum of object pixels in a particular window */
 int global_threshold;
 int local_threshold;
 int top, bottom, right, left;
 int **set_back;
 int **set_obj;
 int **card_back;
 int **card_obj;
 double mean_back;              /* mean of the background pixels in a particular window */
 double mean_obj;               /* mean of the object pixels in a particular window */
 Image *out_img;

 if ( !is_gray_img ( in_img ) )
  {
   ERROR_RET ( "Not a grayscale image !", NULL );
  }

 if ( !IS_POS_ODD ( win_size ) )
  {
   ERROR ( "Window size ( %d ) must be positive and odd !", win_size );
   return NULL;
  }

 half_win = win_size / 2;
 win_count = win_size * win_size;

 num_rows = get_num_rows ( in_img );
 num_cols = get_num_cols ( in_img );
 in_data = get_img_data_nd ( in_img );

 out_img = alloc_img ( PIX_BIN, num_rows, num_cols );
 if ( IS_NULL ( out_img ) )
  {
   ERROR_RET ( "Insufficient memory !", NULL );
  }

 out_data = get_img_data_nd ( out_img );

 /* Calculate the global threshold */
 global_threshold = global_thresholding_method ( in_img );

 set_back = alloc_nd ( sizeof ( int ), 2, num_rows, num_cols );
 card_back = alloc_nd ( sizeof ( int ), 2, num_rows, num_cols );
 set_obj = alloc_nd ( sizeof ( int ), 2, num_rows, num_cols );
 card_obj = alloc_nd ( sizeof ( int ), 2, num_rows, num_cols );

 if ( IS_NULL ( set_obj ) || IS_NULL ( card_obj ) || IS_NULL ( set_back ) ||
      IS_NULL ( card_back ) )
  {
   ERROR_RET ( "Insufficient memory !", NULL );
  }

 for ( ir = 1; ir < num_rows; ir++ )
  {
   for ( ic = 1; ic < num_cols; ic++ )
    {
     gray_val = in_data[ir][ic];

     if ( gray_val <= global_threshold )
      {
       set_back[ir][ic] = set_back[ir][ic - 1] + set_back[ir - 1][ic]
        - set_back[ir - 1][ic - 1] + in_data[ir][ic];
       card_back[ir][ic] = card_back[ir][ic - 1] + card_back[ir - 1][ic]
        - card_back[ir - 1][ic - 1] + 1;
       set_obj[ir][ic] = set_obj[ir][ic - 1] + set_obj[ir - 1][ic]
        - set_obj[ir - 1][ic - 1];
       card_obj[ir][ic] = card_obj[ir][ic - 1] + card_obj[ir - 1][ic]
        - card_obj[ir - 1][ic - 1];
      }
     else
      {
       set_back[ir][ic] = set_back[ir][ic - 1] + set_back[ir - 1][ic]
        - set_back[ir - 1][ic - 1];
       card_back[ir][ic] = card_back[ir][ic - 1] + card_back[ir - 1][ic]
        - card_back[ir - 1][ic - 1];
       set_obj[ir][ic] = set_obj[ir][ic - 1] + set_obj[ir - 1][ic]
        - set_obj[ir - 1][ic - 1] + in_data[ir][ic];
       card_obj[ir][ic] = card_obj[ir][ic - 1] + card_obj[ir - 1][ic]
        - card_obj[ir - 1][ic - 1] + 1;
      }
    }
  }

 /*
    Determine the limits of the filtering operation. Pixels
    in the output image outside these limits are set to 0.
  */

 r_begin = half_win + 1;
 r_end = num_rows - half_win;
 c_begin = half_win + 1;
 c_end = num_cols - half_win;

 for ( ir = r_begin; ir < r_end; ir++ )
  {
   for ( ic = c_begin; ic < c_end; ic++ )
    {
     left = ic - half_win;
     top = ir - half_win;
     right = ic + half_win;
     bottom = ir + half_win;

     sum_back = set_back[bottom][right] - set_back[bottom][left - 1]
      - set_back[top - 1][right] + set_back[top - 1][left - 1];
     num_back = card_back[bottom][right] - card_back[bottom][left - 1]
      - card_back[top - 1][right] + card_back[top - 1][left - 1];
     sum_obj = set_obj[bottom][right] - set_obj[bottom][left - 1]
      - set_obj[top - 1][right] + set_obj[top - 1][left - 1];
     num_obj = card_obj[bottom][right] - card_obj[bottom][left - 1]
      - card_obj[top - 1][right] + card_obj[top - 1][left - 1];

     /* Calculate cluster means */
     mean_back = ( num_back == 0 ? 0.0 : ( sum_back / ( double ) num_back ) );
     mean_obj = ( num_obj == 0 ? 0.0 : ( sum_obj / ( double ) num_obj ) );

     /* Calculate the local threshold */
     local_threshold = 0.5 * ( mean_back + mean_obj );

     /* Determine the output pixel value */
     out_data[ir][ic] =
      ( ( in_data[ir][ic] > local_threshold ) ? OBJECT : BACKGROUND );
    }
  }

 free_nd ( set_back, 2 );
 free_nd ( card_back, 2 );
 free_nd ( set_obj, 2 );
 free_nd ( card_obj, 2 );

 return out_img;
}
 


KOD cpp:     UKRYJ  
/**
 * @file threshold_shanbhag.c
 * Routines for thresholding (binarizing) a grayscale image
 */


#include <image.h>

/**
 * @brief Implements Shanbhag's fuzzy thresholding method
 *
 * @param[in] img Image pointer { grayscale }
 *
 * @return Threshold value or INT_MIN
 *
 * @ref Shanbag A.G. (1994) "Utilization of Information Measure as a Means of
 *      Image Thresholding" Graphical Models and Image Processing, 56(5): 414-419
 *
 * @author M. Emre Celebi
 * @date 06.15.2007
 */


int
threshold_shanbhag ( const Image * img )
{
 SET_FUNC_NAME ( "threshold_shanbhag" );
 int ih, it;
 int threshold;
 int first_bin;                 /* see below */
 int last_bin;                  /* see below */
 double term;
 double tot_ent;                /* total fuzzy entropy */
 double min_ent;                /* min fuzzy entropy */
 double ent_back;               /* entropy of the background pixels at a given threshold */
 double ent_obj;                /* entropy of the object pixels at a given threshold */
 double *data;                  /* normalized histogram data */
 double *P1;                    /* cumulative normalized histogram */
 double *P2;                    /* see below */
 Histo *norm_histo;             /* normalized histogram */

 if ( !is_gray_img ( img ) )
  {
   ERROR_RET ( "Not a grayscale image !", INT_MIN );
  }

 /* Calculate the normalized histogram */
 norm_histo = normalize_histo ( create_histo ( img ) );
 if ( IS_NULL ( norm_histo ) )
  {
   ERROR_RET ( "normalize_histo() failed !", INT_MIN );
  }

 data = get_histo_data ( norm_histo );

 /* Calculate the cumulative normalized histogram */
 P1 = accumulate_histo ( norm_histo );

 P2 = ( double * ) malloc ( NUM_GRAY * sizeof ( double ) );

 for ( ih = 0; ih < NUM_GRAY; ih++ )
  {
   P2[ih] = 1.0 - P1[ih];
  }

 /*
    Determine the first non-zero
    bin starting from the first bin
  */

 first_bin = 0;
 for ( ih = 0; ih < NUM_GRAY; ih++ )
  {
   if ( !IS_ZERO ( P1[ih] ) )
    {
     first_bin = ih;
     break;
    }
  }

 /*
    Determine the first non-one bin
    starting from the last bin
  */

 last_bin = MAX_GRAY;
 for ( ih = MAX_GRAY; ih >= first_bin; ih-- )
  {
   if ( !IS_ZERO ( P2[ih] ) )
    {
     last_bin = ih;
     break;
    }
  }

 /*
    Calculate the total fuzzy entropy at each gray-level
    and find the threshold that minimizes it
  */

 threshold = INT_MIN;
 min_ent = DBL_MAX;
 for ( it = first_bin; it <= last_bin; it++ )
  {
   /* Entropy of the background pixels */
   ent_back = 0.0;
   term = 0.5 / P1[it];
   for ( ih = 0 + 1; ih <= it; ih++ )
    {
     ent_back -= data[ih] * log ( 1.0 - term * P1[ih - 1] );
    }
   ent_back *= term;

   /* Entropy of the object pixels */
   ent_obj = 0.0;
   term = 0.5 / P2[it];
   for ( ih = it + 1; ih < NUM_GRAY; ih++ )
    {
     ent_obj -= data[ih] * log ( 1.0 - term * P2[ih] );
    }
   ent_obj *= term;

   /* Total fuzzy entropy */
   tot_ent = fabs ( ent_back - ent_obj );

   if ( tot_ent < min_ent )
    {
     min_ent = tot_ent;
     threshold = it;
    }
  }

 free_histo ( norm_histo );
 free ( P1 );
 free ( P2 );

 return threshold;
}
 

KOD cpp:     UKRYJ  
/**
 * @file threshold_yen.c
 * Routines for thresholding (binarizing) a grayscale image
 */


#include <image.h>

/**
 * @brief Implements Yen's thresholding method
 *
 * @param[in] img Image pointer { grayscale }
 *
 * @return Threshold value or INT_MIN
 *
 * @ref 1) Yen J.C., Chang F.J., and Chang S. (1995) "A New Criterion
 *      for Automatic Multilevel Thresholding" IEEE Trans. on Image
 *      Processing, 4(3): 370-378
 *      2) Sezgin M. and Sankur B. (2004) "Survey over Image Thresholding
 *      Techniques and Quantitative Performance Evaluation" Journal of
 *      Electronic Imaging, 13(1): 146-165
 *      http://citeseer.ist.psu.edu/sezgin04survey.html
 *
 * @author M. Emre Celebi
 * @date 06.23.2007
 */


int
threshold_yen ( const Image * img )
{
 SET_FUNC_NAME ( "threshold_yen" );
 int ih, it;
 int threshold;
 double crit;
 double max_crit;
 double *data;                  /* normalized histogram data */
 double *P1;                    /* cumulative normalized histogram */
 double *P1_sq;                 /* cumulative normalized histogram */
 double *P2_sq;                 /* see below */
 Histo *norm_histo;             /* normalized histogram */

 if ( !is_gray_img ( img ) )
  {
   ERROR_RET ( "Not a grayscale image !", INT_MIN );
  }

 /* Calculate the normalized histogram */
 norm_histo = normalize_histo ( create_histo ( img ) );
 if ( IS_NULL ( norm_histo ) )
  {
   ERROR_RET ( "normalize_histo() failed !", INT_MIN );
  }

 data = get_histo_data ( norm_histo );

 /* Calculate the cumulative normalized histogram */
 P1 = accumulate_histo ( norm_histo );

 P1_sq = ( double * ) malloc ( NUM_GRAY * sizeof ( double ) );

 P1_sq[0] = data[0] * data[0];
 for ( ih = 0 + 1; ih < NUM_GRAY; ih++ )
  {
   P1_sq[ih] = P1_sq[ih - 1] + data[ih] * data[ih];
  }

 P2_sq = ( double * ) malloc ( NUM_GRAY * sizeof ( double ) );

 P2_sq[MAX_GRAY] = 0.0;
 for ( ih = MAX_GRAY - 1; ih >= 0; ih-- )
  {
   P2_sq[ih] = P2_sq[ih + 1] + data[ih + 1] * data[ih + 1];
  }

 /* Find the threshold that maximizes the criterion */
 threshold = INT_MIN;
 max_crit = DBL_MIN;
 for ( it = 0; it < NUM_GRAY; it++ )
  {
   crit =
    -1.0 * SAFE_LOG ( P1_sq[it] * P2_sq[it] ) +
    2 * SAFE_LOG ( P1[it] * ( 1.0 - P1[it] ) );
   if ( crit > max_crit )
    {
     max_crit = crit;
     threshold = it;
    }
  }

 free_histo ( norm_histo );
 free ( P1 );
 free ( P1_sq );
 free ( P2_sq );

 return threshold;
}
 


KOD cpp:     UKRYJ  
#ifndef IMAGE_H
#define IMAGE_H
#include <ctype.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/* DEFINE CONSTANTS */

#define MAX_LINE_LEN 80     /**< Max. line length */

#define NEW_LINE '\n'       /**< New line character */

#define NUM_GRAY 256        /**< Number of gray levels in an 8-bit gray-scale image */

#define MAX_GRAY 255        /**< Max. gray level in an 8-bit gray-scale image */

#define OBJECT 1

#define BACKGROUND 0

#define PI ( 3.1415926535897932384626433832795 )

#define TWO_PI ( 6.283185307179586476925286766559 )

#define PI_OVER_3 ( 1.0471975511965977461542144610932 )

#define FIVE_PI_OVER_3 ( 5.2359877559829887307710723054658 )

#define SQRT3 ( 1.7320508075688772935274463415059 )

/* MACROS */

/**
 * @brief Sets the function name
 */


#define SET_FUNC_NAME( f_name ) static const char func_name[] = f_name

/**
 * @brief Determines whether or not a given pointer is NULL
 */


#define IS_NULL( x ) ( ( x ) == NULL )

/**
 * @brief Determines whether or not a given number is in the range of [0,255]
 * @warning This is not a safe macro
 */


#define IS_BYTE( x ) ( ( 0 <= ( x ) ) && ( ( x ) < 256 ) )

/**
 * @brief Determines whether or not a given floating-point number is close to 0.0
 */


#define IS_ZERO( x ) ( fabs ( ( x ) ) < DBL_EPSILON )

/**
 * @brief Determines whether or not a given floating-point number is positive
 */


#define IS_POS( x ) ( ( x ) > DBL_EPSILON )

/**
 * @brief Determines whether or not a given floating-point number is negative
 */


#define IS_NEG( x ) ( ( x ) < -DBL_EPSILON )

#define IS_ODD( x ) ( ( x ) % 2 )
#define IS_EVEN( x ) ( !IS_ODD ( ( x ) ) )
#define IS_POS_ODD( x ) ( ( ( x ) > 0 ) && IS_ODD ( x ) )

/**
 * @brief Determines whether or not a given number is in the range of [0,1]
 * @warning This is not a safe macro
 */

#define IS_IN_0_1( x ) ( ! ( IS_NEG ( ( x )  ) || IS_POS ( ( x ) - 1.0 ) ) )

#define SAFE_LOG( x ) ( ( ( x ) > 0.0 ) ? log ( ( x ) ) : ( 0.0 ) )

#define IS_VALID_OBJ( x ) ( !IS_NULL ( ( x ) ) && ( x )->type )

#define GET_NUM_ROWS( x ) ( ( x )->num_rows )

#define GET_NUM_COLS( x ) ( ( x )->num_cols )

#define L1_DIST_3D( x1, y1, z1, x2, y2, z2 )\
 ( abs ( ( x1 ) - ( x2 ) ) + abs ( ( y1 ) - ( y2 ) )  + abs ( ( z1 ) - ( z2 ) ) )


#define L2_NORM_3D_SQR( x, y, z )\
 ( ( x ) * ( x ) + ( y ) * ( y ) + ( z ) * ( z ) )


#define L2_NORM_3D( x, y, z )\
 sqrt ( L2_NORM_3D_SQR ( ( x ), ( y ), ( z ) ) )


#define L2_NORM_2D_SQR( x, y )\
 ( ( x ) * ( x ) + ( y ) * ( y ) )


#define L2_NORM_2D( x, y )\
 sqrt ( L2_NORM_2D_SQR ( ( x ), ( y ) ) )


#define L2_DIST_3D_SQR( x1, y1, z1, x2, y2, z2 )\
 ( ( ( x1 ) - ( x2 ) ) * ( ( x1 ) - ( x2 ) )\
 + ( ( y1 ) - ( y2 ) ) * ( ( y1 ) - ( y2 ) )\
 + ( ( z1 ) - ( z2 ) ) * ( ( z1 ) - ( z2 ) ) )


#define L2_DIST_3D( x1, y1, z1, x2, y2, z2 )\
 sqrt ( L2_DIST_3D_SQR ( ( x1 ), ( y1 ), ( z1 ), ( x2 ), ( y2 ), ( z2 ) ) )


#define L2_DIST_2D_SQR( x1, y1, x2, y2 )\
 ( ( ( x1 ) - ( x2 ) ) * ( ( x1 ) - ( x2 ) )\
 + ( ( y1 ) - ( y2 ) ) * ( ( y1 ) - ( y2 ) ) )\

#define L2_DIST_2D( x1, y1, x2, y2 )\
 sqrt ( L2_DIST_2D_SQR( ( x1 ), ( y1 ), ( x2 ), ( y2 ) ) )\

#define ROUND( X )      ( ( X ) < 0.0 ? ( X ) - 0.5 : ( X ) + 0.5 )

#define DEG_TO_RAD( D ) ( 0.017453292519943295769236907684886 * ( D ) )

#define CLAMP_BYTE( X ) ( ( X ) < 0 ? 0 : ( ( X ) > MAX_GRAY ) ? MAX_GRAY : ( X ) )

#define MIN_2( A, B ) ( ( A ) < ( B ) ? ( A ) : ( B ) )

#define MAX_2( A, B ) ( ( A ) < ( B ) ? ( B ) : ( A ) )

#define MIN_3( A, B, C ) MIN_2 ( ( A ), MIN_2 ( ( B ), ( C ) ) )

#define MAX_3( A, B, C ) MAX_2 ( ( A ), MAX_2 ( ( B ), ( C ) ) )

/** @cond INTERNAL_MACRO */
#define SWAP_INT( a, b ) { tmp_var = ( a ); ( a ) = ( b ); ( b ) = tmp_var; }

/** @endcond INTERNAL_MACRO */

/** @cond INTERNAL_MACRO */
#define SORT_INT( a, b ) { if ( ( a ) > ( b ) ) SWAP_INT ( a, b ); }

/** @endcond INTERNAL_MACRO */

#define DIST_FUNC L2_DIST_3D
#define DIST_FUNC_SQR L2_DIST_3D_SQR
#define ACOS acos

/**
 * @brief If DEBUG_ON is defined, prints file name and line number and then
 *        calls the variable-argument debug function; otherwise does nothing
 */


#ifdef DEBUG_ON
# define DEBUG fprintf ( stderr, "Debug in %s: ", func_name ), debug
#else
# define DEBUG do_nothing
#endif

/**
 * @brief Prints file name and line number and then
 *        calls the variable-argument warning function
 */


#define WARNING fprintf ( stderr, "Warning in %s: ", func_name ), warning

/**
 * @brief Prints file name and line number and then
 *        calls the variable-argument error function
 */


#define ERROR fprintf ( stderr, "Error in %s: ", func_name ), error

/**
 * @brief Prints file name and line number and then
 *        calls the variable-argument fatal function
 */


#define FATAL fprintf ( stderr, "Fatal error in %s: ", func_name ), fatal

/**
 * @brief Prints file name, line number, and the given error message;
 *        conditionally aborts the program and then returns the value of
 *        the given error number
 */


#define ERROR_RET( err_msg, err_no ) \
         do \
          { \
           fprintf ( stderr, "Error in %s: %s\n", func_name, ( err_msg ) ); \
           fflush ( stderr ); \
           if ( get_err_mode ( ) ) abort ( ); \
           return ( err_no ) ; \
          } while ( 0 )


#define MALLOC_STRUCT( x ) ( ( x * ) malloc ( sizeof ( x ) ) )

#define CALLOC_STRUCT( x ) ( ( x * ) calloc ( 1, sizeof ( x ) ) )

#define NEG_ONE_POW( x ) ( IS_EVEN ( ( x ) ) ? ( 1 ) : ( -1 ) )

/* ENUMERATIONS/TYPEDEFS */

enum
{

 E_SUCCESS = 0,  /**< success */

 E_FAILURE,      /**< failure */

 E_INVARG,       /**< invalid argument */

 E_INVOBJ,       /**< invalid object */

 E_INVBPP,       /**< invalid pixel depth */

 E_NULL,         /**< null pointer */

 E_NOMEM,        /**< insufficient memory */

 E_DIVZERO,      /**< divide by zero */

 E_UNIMPL,       /**< feature not implemented */

 E_UNFMT,        /**< unknown file format */

 E_FOPEN,        /**< file open error */

 E_FREAD,        /**< file read error */

 E_FEOF          /**< end of file reached */

}; /**< Error Codes */

typedef unsigned char byte; /**< Byte Type */

typedef enum
{

 false = 0,

 true = 1

} Bool; /**< Boolean Type */

#define IS_BOOL( x ) ( ( ( x ) == false ) || ( ( x ) == true ) )

typedef enum
{

 PIX_INVALID = 0,  /**< invalid */

 PIX_BIN,          /**< binary */

 PIX_GRAY,         /**< gray-scale */

 PIX_RGB,          /**< RGB color */

 PIX_INT_1B,       /**< single-band integer */

 PIX_INT_3B,       /**< 3-band integer */

 PIX_DBL_1B,       /**< single-band double */

 PIX_DBL_3B        /**< 3-band double */

} PixelType; /**< Pixel Type Enumeration */

typedef enum
{

 FMT_UNKNOWN = 0, /**< Unknown */

 FMT_BMP,            /**< BMP */

 FMT_GIF,            /**< GIF */

 FMT_JPG,            /**< JPG */

 FMT_PBMA,           /**< Plain/ASCII PBM */

 FMT_PBM,            /**< Raw/Binary PBM */

 FMT_PCX,            /**< PCX */

 FMT_PGMA,           /**< Plain/ASCII PGM */

 FMT_PGM,            /**< Raw/Binary PGM */

 FMT_PNG,            /**< PNG */

 FMT_PPMA,           /**< Plain/ASCII PPM */

 FMT_PPM,            /**< Raw/Binary PPM */

 FMT_PSD,            /**< PSD */

 FMT_RAS,            /**< RAS */

 FMT_TGA,            /**< TGA */

 FMT_TIFF            /**< TIFF */

} ImageFormat; /**< Image Format Enumeration */

typedef struct
{

 PixelType type;     /**< Pixel Type */

 int num_bands;      /**< Number of Bands */

 int num_rows;       /**< Number of Rows */

 int num_cols;       /**< Number of Columns */

 int max_pix_val;    /**< Max. Pixel Value (Defined only for PIX_BIN, PIX_GRAY, and PIX_RGB) */

 int num_cc;         /**< Number of Connected Components (Defined only for PIX_INT_1B) */

 union
 {

  byte *byte_data;       /**< For PIX_BIN, PIX_GRAY, PIX_RGB */

  int *int_data;         /**< For PIX_INT_1B, PIX_INT_3B */

  double *double_data;   /**< For PIX_DBL_1B, PIX_DBL_3B */

 } data_1d;  /**< 1-dimensional contiguous pixel array */

 union
 {

  byte **byte_data_1b;        /**< For PIX_BIN, PIX_GRAY */

  byte ***byte_data_3b;       /**< For PIX_RGB */

  int **int_data_1b;          /**< For PIX_INT_1B */

  int ***int_data_3b;         /**< For PIX_INT_3B */

  double **double_data_1b;    /**< For PIX_DBL_1B */

  double ***double_data_3b;   /**< For PIX_DBL_3B */

 } data_nd;  /**< 2 or 3-dimensional pixel array */

} Image; /**< Image Structure */

typedef enum
{

 HISTO_INVALID = 0,     /**< invalid */

 HISTO_INT,     /**< unnormalized */

 HISTO_DBL      /**< normalized */

} HistoType; /**< Histogram Type Enumeration */

typedef struct
{

 HistoType type;

 int num_bins;        /**< Number of Bins */

 int num_pixels;      /**< Number of Pixels in the Source Image */

 union
 {

  int *int_data;         /**< For HISTO_INT */

  double *double_data;   /**< For HISTO_DBL */

 } data;   /**< 1-dimensional contiguous histogram data array */

} Histo; /**< Histogram Structure */

typedef enum
{

 EM_INVALID = 0, /**< invalid */

 EM_MAE,     /**< Mean Absolute Error */

 EM_MSE,     /**< Mean Squared Error */

 EM_RMSE,    /**< Root Mean Squared Error */

 EM_PSNR,    /**< Peak Signal-to-Noise Ratio */

 EM_NMSE,    /**< Normalized Mean Squared Error */

 EM_NCD      /**< Normalized Color Distance */

} ErrorMeasure; /**< Error Measure Enumeration */

typedef enum
{

 GEN_INVALID = 0,    /**< invalid */

 GEN_VALID           /**< valid */

} GenericType; /**< Generic Type Enumeration */

typedef struct
{

 GenericType type;

 int length;                    /* length (# points) of the chain */
 int *row;                      /* row coordinates */
 int *col;                      /* column coordinates */
 int *dir;                      /* Freeman chaincode directions */

} Chain; /**< Chain Code Structure */

typedef struct
{

 GenericType type;

 int num_chains;                /* number of chains (objects) in the list */
 int max_length;                /* max. length of an object chain */
 int num_rows;                  /* # rows in the parent image */
 int num_cols;                  /* # columns in the parent image */
 Chain **chain;                 /* chain code pointers */

} ChainList; /**< Chain Code List Structure */

typedef struct
{

 int min_row;                   /* minimum row coordinate */
 int max_row;                   /* maximum row coordinate */
 int min_col;                   /* minimum column coordinate */
 int max_col;                   /* maximum column coordinate */

} Box; /**< Bounding-Box Structure */

typedef enum
{

 CMAP_INVALID = 0,

 CMAP_HSV,

 CMAP_INFRARED,

 CMAP_JET

} ColorMap; /**< Color Map Enumeration */

typedef struct
{
 double row;
 double col;
} Point;

typedef struct
{

 GenericType type;

 int num_pts;                   /* number of points in the list */
 Point **point;                 /* Point pointers */

} PointList; /**< 2-Dimensional Point List Structure */

typedef struct
{
 double maj_axis_len;
 double min_axis_len;
 double aspect_ratio;
 double eccentricity;
 double orientation;
} EllipseFeatures;

typedef struct
{
 double mean;
 double stdev;
 double entropy;
 double roughness;
} RadialDistFeatures;

typedef struct
{
 double F1;
 double F2;
 double F3;
 double F4;
} CSMoments;

typedef struct
{
 double mean;
 double variance;
 double skewness;
 double kurtosis;
} ChordLenStats;

typedef struct
{
 double m00;
 double m01;
 double m10;
 double m11;
 double m02;
 double m20;
 double m12;
 double m21;
 double m03;
 double m30;
} GeoMoments;

typedef struct
{
 double phi1;
 double phi2;
 double phi3;
 double phi4;
 double phi5;
 double phi6;
 double phi7;
} HuMoments;

typedef struct
{
 double I1;
 double I2;
 double I3;
 double I4;
} AffineMoments;

typedef struct
{
 double real;
 double imag;
} Complex;

typedef struct
{
 GenericType type;

 int max_order;
 int radius;
 int num_terms;
 Complex ***data;

} ZernikeBasis;

typedef enum
{

 STRL_INVALID = 0,  /**< invalid */

 STRL_LINE,        /**< flat line */

 STRL_RECT,        /**< flat rectangle */

 STRL_DISK,        /**< flat disk */

 STRL_FLAT,        /**< arbitrary flat */

 STRL_NONFLAT      /**< arbitrary non-flat */

} StrelType; /**< Structuring Element Type Enumeration */

typedef struct
{

 StrelType type;

 int num_rows;

 int num_cols;

 int origin_row;

 int origin_col;

 byte *data_1d;

 byte **data_2d;

} Strel;

typedef enum
{

 SEQ_INVALID = 0,  /**< invalid */

 SEQ_OC,           /**< */

 SEQ_CO,           /**< */

 SEQ_OCO,          /**< */

 SEQ_COC           /**< */

} ASFSeq; /**< Alternating Sequential Filter Sequence */

typedef enum
{
 TYPE_UNKNOWN /* unknown type */ ,
 UCHAR /* unsigned char */ ,
 SCHAR /* signed char */ ,
 USHORT /* unsigned short int */ ,
 SSHORT /* signed short int */ ,
 INT /* signed int */ ,
 ULINT /* unsigned long int */ ,
 FLOAT /* float */ ,
 DOUBLE                         /* double */
} bufferType;

typedef enum
{
 UNKNOWN_FILTER = 0 /* unknown filter type */ ,
 ALPHA_DERICHE = 1 /* Deriche's filter (exponential (- alpha |X|)) */ ,
 GAUSSIAN_DERICHE = 2           /* gaussian approximation (Deriche's coefficients) */
} recursiveFilterType;

/* FUNCTION PROTOTYPES */

/* add_noise.c */
Image *add_gaussian_noise ( const Image * in_img, const double mean,
                            const double stdev );
Image *add_saltpepper_noise ( const Image * in_img, const double prob );
Image *add_speckle_noise ( const Image * in_img, const double stdev );
Image *add_uncorr_impulsive_noise ( const Image * in_img, const double prob,
                                    const Bool is_uniform );
Image *add_corr_impulsive_noise ( const Image * in_img,
                                  const double overall_prob,
                                  const double red_prob,
                                  const double green_prob,
                                  const double blue_prob,
                                  const Bool is_uniform );
double calc_error ( const ErrorMeasure err_meas, const Image * orig_img,
                    const Image * est_img );

/* alloc_nd.c */
void *alloc_nd ( const size_t elem_size, const int dim_count, ... );
void free_nd ( void *base_ptr, const int dim_count );

/* arith.c */
Image *add_img ( const Image * in_img_a, const Image * in_img_b );
Image *sub_img ( const Image * in_img_a, const Image * in_img_b );
Image *mult_img ( const Image * in_img_a, const Image * in_img_b );
Image *abs_diff_img ( const Image * in_img_a, const Image * in_img_b );
Image *union_img ( const Image * in_img_a, const Image * in_img_b );
Image *intersect_img ( const Image * in_img_a, const Image * in_img_b );
Image *add_img_scalar ( const Image * in_img, const double scalar );
Image *sub_img_scalar ( const Image * in_img, const double scalar );
Image *mult_img_scalar ( const Image * in_img, const double scalar );
Image *div_img_scalar ( const Image * in_img, const double scalar );

/* basic_shape_desc.c */
int count_obj_pixels ( const Image * img, const int label );
double calc_obj_area ( const Image * img, const int label );
double calc_aspect_ratio ( const Image * img, const int label );
Point *calc_obj_centroid ( const Image * img, const int label );
Image *crop_object ( const Image * in_img, const int label );
Image *circular_scale ( const Image * in_img, const int label,
                        const int radius );
double calc_triangularity ( const Image * img, const int label );
double calc_ellipticity ( const Image * img, const int label );
EllipseFeatures *calc_ellipse_feats ( const Image * img, const int label );
double calc_equi_diameter ( const Image * img, const int label );
double calc_elliptic_var ( const Image * img, const int label,
                           const PointList * cont );
double calc_circularity ( const Image * img, const int label,
                          const PointList * cont );
RadialDistFeatures *calc_radial_dist_feats ( const Image * img, const int label,
                                             const PointList * cont );
ChordLenStats *calc_chord_len_stats ( const PointList * cont );

/* bmp_header.c */
int read_bmp_header ( FILE * file_ptr, PixelType * pix_type, int *num_rows,
                      int *num_cols, Bool * is_bottom_up );
void write_bmp_header ( const PixelType pix_type, const int num_rows,
                        const int num_cols, FILE * file_ptr );

/* bmp_io.c */
Image *read_bmp1_data ( const int num_rows, const int num_cols,
                        const Bool is_bottom_up, FILE * file_ptr );
int write_bmp1 ( const Image * img, FILE * file_ptr );
Image *read_bmp8_data ( const int num_rows, const int num_cols,
                        const Bool is_bottom_up, FILE * file_ptr );
int write_bmp8 ( const Image * img, FILE * file_ptr );
Image *read_bmp24_data ( const int num_rows, const int num_cols,
                         const Bool is_bottom_up, FILE * file_ptr );
int write_bmp24 ( const Image * img, FILE * file_ptr );

/* ccv_enhance.c */
Image *ccv_enhance ( const Image * in_img, const double sharp_factor );

/* colorspaces.c */
Image *rgb_to_nrgb ( const Image * in_img, double undefined );
Image *rgb_to_ohta ( const Image * in_img );
Image *ohta_to_rgb ( const Image * in_img );
Image *rgb_to_hsi ( const Image * in_img, double undefined );
Image *rgb_to_hsi_kender ( const Image * in_img, double undefined );
Image *hsi_to_rgb ( const Image * in_img );
Image *rgb_to_hsv ( const Image * in_img, double undefined );
Image *hsv_to_rgb ( const Image * in_img );
Image *rgb_to_hsl ( const Image * in_img, double undefined );
Image *hsl_to_rgb ( const Image * in_img );
Image *rgb_to_lab ( const Image * in_img );
Image *lab_to_rgb ( const Image * in_img );
Image *rgb_to_luv ( const Image * in_img );
Image *luv_to_rgb ( const Image * in_img );
Image *rgb_to_sct ( const Image * in_img, double undefined );
Image *sct_to_rgb ( const Image * in_img );
Image *lab_to_lch ( const Image * in_img, double undefined );
Image *lch_to_lab ( const Image * in_img );
Image *rgb_to_l1l2l3 ( const Image * in_img, double undefined );

/* convex_hull.c */
PointList *calc_2d_convex_hull ( const PointList * inp_point_list );
double calc_max_diameter ( const PointList * hull );
double calc_compactness ( const Image * img, const int label,
                          const PointList * hull );
double calc_solidity ( const Image * in_img, const int label,
                       const PointList * hull );

/* cs_moments.c */
CSMoments *calc_cs_moments ( const Image * img, const int label,
                             const PointList * cont );

/* draw_line.c */
void draw_line ( const Point * point1, const Point * point2,
                 const int pix_value, Image * img );

/* edge_advanced.c */
Image *detect_edge_canny ( const Image * in_img, const double sigma,
                           const double low, const double high );
Image *detect_edge_deriche ( const Image * in_img, const double alpha,
                             const double low, const double high );

/* edge_basic.c */
Image *detect_edge_roberts ( const Image * in_img );
Image *detect_edge_prewitt ( const Image * in_img );
Image *detect_edge_sobel ( const Image * in_img );
Image *detect_edge_log ( const Image * in_img, const double sigma );

/* ellipticity.c */
double calc_elliptic_var ( const Image * img, const int label,
                           const PointList * cont );

/* error.c */
Bool get_err_mode ( void );
void set_err_mode ( const Bool err_mode );
Bool get_warn_mode ( void );
void set_warn_mode ( const Bool warn_mode );
void do_nothing ( const char *format, ... );
void debug ( const char *format, ... );
void warning ( const char *format, ... );
void error ( const char *format, ... );
void fatal ( const char *format, ... );
const char *error_str ( const int err_no );

/* expand_histo.c */
Image *expand_histo ( const Image * in_img, const double percent_clip );

/* fill_region.c */
int calc_box_area ( const Box * box );
int get_box_height ( const Box * box );
int get_box_width ( const Box * box );
Box *alloc_box ( void );
void free_box ( Box * box );
Box *calc_box ( const Image * img, const int label );
void fill_region ( const int seed_row, const int seed_col, const int label,
                   Image * img );

/* filter_abvdf.c */
Image *filter_abvdf ( const Image * in_img, const int win_size,
                      const int k_value, const double tolerance );

/* filter_acwddf.c */
Image *filter_acwddf ( const Image * in_img, const int win_size,
                       const int lambda, const double tolerance );

/* filter_acwvdf.c */
Image *filter_acwvdf ( const Image * in_img, const int win_size,
                       const int lambda, const double tolerance );

/* filter_acwvmf.c */
Image *filter_acwvmf ( const Image * in_img, const int win_size,
                       const int lambda, const double tolerance );

/* filter_adap_smooth.c */
Image *filter_adap_smooth ( const Image * in_img, const double k_value,
                            const int num_iters );

/* filter_ahdf.c */
Image *filter_ahdf ( const Image * in_img, const int win_size );

/* filter_amnfe.c */
Image *filter_amnfe ( const Image * in_img, const int win_size,
                      const double k_value );

/* filter_amnfg.c */
Image *filter_amnfg ( const Image * in_img, const int win_size,
                      const double k_value );

/* filter_annf.c */
Image *filter_annf ( const Image * in_img, const int win_size );

/* filter_annmf.c */
Image *filter_annmf ( const Image * in_img, const int win_size );

/* filter_asbvdf_mean.c */
Image *filter_asbvdf_mean ( const Image * in_img, const int win_size );

/* filter_asbvdf_rank.c */
Image *filter_asbvdf_rank ( const Image * in_img, const int win_size );

/* filter_asddf_mean.c */
Image *filter_asddf_mean ( const Image * in_img, const int win_size );

/* filter_asddf_rank.c */
Image *filter_asddf_rank ( const Image * in_img, const int win_size );

/* filter_asvmf_mean.c */
Image *filter_asvmf_mean ( const Image * in_img, const int win_size );

/* filter_asvmf_rank.c */
Image *filter_asvmf_rank ( const Image * in_img, const int win_size );

/* filter_atvmf.c */
Image *filter_atvmf ( const Image * in_img, const int win_size,
                      const int k_value );

/* filter_avmf.c */
Image *filter_avmf ( const Image * in_img, const int win_size,
                     const int k_value, const double tolerance );

/* filter_bilateral.c */
Image *filter_bilateral ( const Image * in_img, const double range_sigma,
                          const double spatial_sigma );

/* filter_bvdf.c */
Image *filter_bvdf ( const Image * in_img, const int win_size );

/* filter_cbrf.c */
Image *filter_cbrf ( const Image * in_img, const int win_size );

/* filter_cfm.c */
Image *filter_cfm ( const Image * in_img, const int win_size,
                    const double C_value, const double t_value );

/* filter_ddf.c */
Image *filter_ddf ( const Image * in_img, const int win_size );

/* filter_ebvdf.c */
Image *filter_ebvdf ( const Image * in_img, const int win_size );

/* filter_eddf.c */
Image *filter_eddf ( const Image * in_img, const int win_size );

/* filter_evmf.c */
Image *filter_evmf ( const Image * in_img, const int win_size );

/* filter_exvmf.c */
Image *filter_exvmf ( const Image * in_img, const int win_size );

/* filter_fddrhf.c */
Image *filter_fddrhf ( const Image * in_img, const double weight,
                       const double h_value, const double k_value,
                       const double beta, const double gamma );

/* filter_ffnrf.c */
Image *filter_ffnrf ( const Image * in_img, const int win_size,
                      const int K_value, const double alpha );

/* filter_fmvmf.c */
Image *filter_fmvmf ( const Image * in_img, const int win_size,
                      const double beta );

/* filter_fmvdf.c */
Image *filter_fmvdf ( const Image * in_img, const int win_size,
                      const double K_value, const double alpha );

/* filter_fmddf.c */
Image *filter_fmddf ( const Image * in_img, const int win_size,
                      const double K1_value, const double K2_value,
                      const double alpha );

/* filter_fovdf.c */
Image *filter_fovdf ( const Image * in_img, const int win_size,
                      const double beta, const double gamma );

/* filter_fovmf.c */
Image *filter_fovmf ( const Image * in_img, const int win_size,
                      const double beta, const double gamma );

/* filter_fpgf.c */
Image *filter_fpgf ( const Image * in_img, const int win_size,
                     const int pg_size, const double dist_thresh );

/* filter_fvdf.c */
Image *filter_fvdf ( const Image * in_img, const int win_size,
                     const double beta, const double gamma );

/* filter_fvdrhf.c */
Image *filter_fvdrhf ( const Image * in_img, const double weight,
                       const double h_value, const double k_value,
                       const double beta, const double gamma );

/* filter_fvmf.c */
Image *filter_fvmf ( const Image * in_img, const int win_size,
                     const double beta, const double gamma );

/* filter_fvmrhf.c */
Image *filter_fvmrhf ( const Image * in_img, const double weight,
                       const double h_value, const double k_value,
                       const double beta, const double gamma );

/* filter_gaussian.c */
Image *filter_gaussian ( const Image * in_img, const double stdev );
Image *filter_ani_gaussian ( const Image * in_img, const double sigma_v,
                             const double sigma_u, const double phi,
                             const int order_v, const int order_u );

/* filter_giws.c */
Image *filter_giws ( const Image * in_img, const int win_size );

/* filter_gvdf.c */
Image *filter_gvdf ( const Image * in_img, const int win_size );

/* filter_hdf.c */
Image *filter_hdf ( const Image * in_img, const int win_size );

/* filter_kuwahara.c */
Image *filter_kuwahara ( const Image * in_img, const int k_value );

/* filter_kvmf.c */
Image *filter_kvmf ( const Image * in_img, const int win_size );

/* filter_lce.c */
Image *filter_lce ( const Image * in_img, const int win_size );

/* filter_lee.c */
Image *filter_lee ( const Image * in_img, const int win_size,
                    const double stdev );

/* filter_lum.c */
Image *filter_lum ( const Image * in_img, const int win_size,
                    const int k_value );

/* filter_mad.c */
Image *filter_mad ( const Image * in_img, const int win_size, const int a_value,
                    const int b_value );

/* filter_majority.c */
Image *filter_majority ( const Image * in_img, const int win_size );

/* filter_mcv.c */
Image *filter_mcv ( const Image * in_img, const int win_size );

/* filter_mcwvmf.c */
Image *filter_mcwvmf ( const Image * in_img, const int win_size,
                       const double weight );

/* filter_mean.c */
Image *filter_mean ( const Image * in_img, const int win_size );

/* filter_median.c */
Image *filter_classical_median ( const Image * in_img, const int win_size );
Image *filter_running_median ( const Image * in_img, const int win_size );
Image *filter_3x3_median ( const Image * in_img );
Image *filter_const_time_median ( const Image * in_img, const int win_size );

/* filter_mlv.c */
Image *filter_mlv ( const Image * in_img, const int win_size );

/* filter_mmf.c */
Image *filter_mmf ( const Image * in_img, const int win_size );

/* filter_mpv.c */
Image *filter_mpv ( const Image * in_img, const int win_size );

/* filter_perona_malik.c */
Image *filter_perona_malik ( const Image * in_img, const double k_value,
                             const double lambda, const int num_iters );

/* filter_pgf.c */
Image *filter_pgf ( const Image * in_img, const int win_size,
                    const double alpha );

/* filter_pvof.c */
Image *filter_pvof ( const Image * in_img, const int win_size );

/* filter_robust_aniso.c */
Image *filter_robust_aniso ( const Image * in_img, const int num_iters );

/* filter_rodsvmf.c */
Image *filter_rodsvmf ( const Image * in_img, const int win_size,
                        const double dist_thresh );

/* filter_sbvdf_mean.c */
Image *filter_sbvdf_mean ( const Image * in_img, const int win_size,
                           const double lambda );

/* filter_sbvdf_rank.c */
Image *filter_sbvdf_rank ( const Image * in_img, const int win_size,
                           const double lambda );

/* filter_sddf_mean.c */
Image *filter_sddf_mean ( const Image * in_img, const int win_size,
                          const double lambda );

/* filter_sddf_rank.c */
Image *filter_sddf_rank ( const Image * in_img, const int win_size,
                          const double lambda );

/* filter_sdrom.c */
Image *filter_sdrom ( const Image * in_img, const int t0, const int t1,
                      const int t2, const int t3 );

/* filter_sep_median */
Image *filter_sep_median ( const Image * in_img, const int win_size );

/* filter_sigma.c */
Image *filter_sigma ( const Image * in_img, const int win_size,
                      const int k_value );

/* filter_snn.c */
Image *filter_snn ( const Image * in_img );

/* filter_susan.c */
Image *filter_susan ( const Image * in_img, const int bright_thresh,
                      const double dist_thresh );

/* filter_svmf_mean.c */
Image *filter_svmf_mean ( const Image * in_img, const int win_size,
                          const double lambda );

/* filter_svmf_rank.c */
Image *filter_svmf_rank ( const Image * in_img, const int win_size,
                          const double lambda );

/* filter_vmf.c */
Image *filter_vmf ( const Image * in_img, const int win_size );

/* filter_vmrhf.c */
Image *filter_vmrhf ( const Image * in_img, const double weight,
                      const double h_value, const double k_value );

/* filter_vsdromf.c */
Image *filter_vsdromf ( const Image * in_img, const double t0, const double t1,
                        const double t2, const double t3 );

/* gen_fourier_desc.c */
double *calc_gen_fourier_desc ( const Image * in_img, const int label,
                                const int rad_res, const int ang_res,
                                const int radius );

/* graph_segment.c */
Image *graph_segment ( const Image * in_img, const double sigma,
                       const double k_value, const int min_size,
                       const Bool fast );

/* gray_morpho.c */
Image *morpho_gradient ( const Image * in_img, const Strel * se );
Image *h_minima_transform ( const Image * in_img, const Strel * se,
                            const int contrast );
Image *h_maxima_transform ( const Image * in_img, const Strel * se,
                            const int contrast );
Image *regional_minima ( const Image * in_img, const Strel * se );
Image *regional_maxima ( const Image * in_img, const Strel * se );
Image *extended_minima_transform ( const Image * in_img, const Strel * se,
                                   const int contrast );
Image *extended_maxima_transform ( const Image * in_img, const Strel * se,
                                   const int contrast );

/* histogram.c */
HistoType get_histo_type ( const Histo * histo );
Bool is_int_histo ( const Histo * histo );
Bool is_double_histo ( const Histo * histo );
int get_num_bins ( const Histo * histo );
int get_num_pixels ( const Histo * histo );
void *get_histo_data ( const Histo * histo );
Histo *alloc_histo ( const HistoType type, const int num_bins );
void free_histo ( Histo * histo );
Histo *create_histo ( const Image * img );
Histo *normalize_histo ( const Histo * in_histo );
void *accumulate_histo ( const Histo * histo );
Image *histo_to_img ( const Histo * histo, const int num_rows,
                      const int bin_spacing );
Histo *smooth_histo_local ( const Histo * in_histo, const int mask_size );
Histo *smooth_histo_gauss ( const Histo * in_histo, const double sigma );
Image *hyperbolize_histo ( const Image * in_img, const double c_value );
Image *equalize_histo ( const Image * in_img );

/* image.c */
Bool is_bin_img ( const Image * img );
Bool is_gray_img ( const Image * img );
Bool is_rgb_img ( const Image * img );
Bool is_byte_img ( const Image * img );
Bool is_label_img ( const Image * img );
Bool is_bin_or_label_img ( const Image * img );
Bool is_bin_or_gray_img ( const Image * img );
Bool is_dbl_3b_img ( const Image * img );
Bool is_dbl_img ( const Image * img );
Bool is_int_img ( const Image * img );
PixelType get_pix_type ( const Image * img );
int get_num_bands ( const Image * img );
int get_num_rows ( const Image * img );
int get_num_cols ( const Image * img );
int get_num_cc ( const Image * img );
void *get_img_data_1d ( const Image * img );
void *get_img_data_nd ( const Image * img );
Image *alloc_img ( const PixelType pix_type, const int num_rows,
                   const int num_cols );
void free_img ( Image * img );
int count_colors ( const Image * img );
Image *rgb_to_gray ( const Image * rgb_img );
Image *negate_img ( const Image * in_img );
Bool img_dims_agree ( const Image * img_a, const Image * img_b );
Bool img_types_agree ( const Image * img_a, const Image * img_b );
Bool is_equal_img ( const Image * in_img_a, const Image * in_img_b );
Image *dbl_to_byte_img ( const Image * in_img );
Image *byte_to_dbl_img ( const Image * in_img );
void get_rgb_bands ( const Image * rgb_img, Image ** red_img,
                     Image ** green_img, Image ** blue_img );
Image *combine_rgb_bands ( const Image * red_img, const Image * green_img,
                           const Image * blue_img );
Image *clone_img ( const Image * in_img );
Image *alloc_const_img ( const PixelType pix_type, const int num_rows,
                         const int num_cols, const double value );

/* image_format.c */
int get_img_format ( FILE * file_ptr, ImageFormat * img_format );
const char *img_format_str ( const ImageFormat img_format );

/* image_io.c */
Image *read_img ( const char *file_name );
int write_img ( const Image * img, const char *file_name,
                const ImageFormat img_format );

/* invariant_moments.c */
GeoMoments *calc_geo_moments ( const Image * img, const int label );
GeoMoments *calc_central_moments ( const Image * img, const int label );
GeoMoments *calc_norm_central_moments ( const Image * img, const int label );
HuMoments *calc_hu_moments ( const Image * img, const int label );
AffineMoments *calc_affine_moments ( const Image * img, const int label );

/* jpeg_io.c */
Image *read_jpeg_data ( FILE * file_ptr );

/* label_cc.c */
Image *label_cc ( const Image * in_img, const int connectivity );
int *get_cc_areas ( const Image * lab_img );
Image *remove_small_cc ( const Image * lab_img, const int area_thresh );
Image *retain_largest_cc ( const Image * lab_img );
Image *label_to_bin ( const Image * lab_img );
Bool obj_touch_walls ( const Image * img );

/* label_cc_hybrid.c */
Image *label_cc_hybrid ( const Image * in_img, const int connectivity );

/* label_cc_trace.c */
Image *label_cc_trace ( const Image * in_img, const int connectivity );

/* match_histo.c */
Image *match_histo ( const Image * in_img, const Histo * target_histo );

/* morpho.c */
Image *close_img ( const Image * in_img, const Strel * se );
Image *dilate_img ( const Image * in_img, const Strel * se );
Image *erode_img ( const Image * in_img, const Strel * se );
Image *open_img ( const Image * in_img, const Strel * se );
Image *tophat_img ( const Image * in_img, const Strel * se );
Image *bothat_img ( const Image * in_img, const Strel * se );
Image *cond_dilate_img ( const Image * in_img, const Image * cond_img,
                         const Strel * se, const int num_iters );
Image *cond_erode_img ( const Image * in_img, const Image * cond_img,
                        const Strel * se, const int num_iters );
Image *inf_rec_img ( const Image * marker_img, const Image * cond_img,
                     const Strel * se );
Image *sup_rec_img ( const Image * marker_img, const Image * cond_img,
                     const Strel * se );
Image *open_rec_img ( const Image * in_img, const Strel * se_ero,
                      const Strel * se_conn );
Image *close_rec_img ( const Image * in_img, const Strel * se_dil,
                       const Strel * se_conn );
Image *open_rec_tophat_img ( const Image * in_img, const Strel * se_ero,
                             const Strel * se_conn );
Image *close_rec_tophat_img ( const Image * in_img, const Strel * se_dil,
                              const Strel * se_conn );
Image *filter_asf ( const Image * in_img, const Strel * se, const ASFSeq seq,
                    const int num_iters );
Image *fill_holes ( const Image * in_img, const Strel * se );
Image *clear_border ( const Image * in_img, const Strel * se );

/* pnm_header.c */
int read_pbmb_header ( FILE * file_ptr, int *num_rows, int *num_cols );
void write_pbmb_header ( const int num_rows, const int num_cols,
                         FILE * file_ptr );
int read_pgmb_header ( FILE * file_ptr, int *num_rows, int *num_cols,
                       int *max_gray );
void write_pgmb_header ( const int num_rows, const int num_cols,
                         const int max_gray, FILE * file_ptr );
int read_ppmb_header ( FILE * file_ptr, int *num_rows, int *num_cols,
                       int *max_rgb );
void write_ppmb_header ( const int num_rows, const int num_cols,
                         const int max_rgb, FILE * file_ptr );

/* point.c */
Point *alloc_point ( const double row_val, const double col_val );
void free_point ( Point * point );
PointList *alloc_point_list ( const int num_pts );
void free_point_list ( PointList * point_list );
int get_num_pts ( const PointList * point_list );
Point *calc_cont_centroid ( const PointList * cont );
Image *point_list_to_img ( const PointList * point_list, const int num_rows,
                           const int num_cols );
PointList *chain_to_point_list ( const Chain * chain );

/* pnm_io.c */
Image *read_pbmb_data ( const int num_rows, const int num_cols,
                        FILE * file_ptr );
int write_pbmb ( const Image * img, FILE * file_ptr );
Image *read_pgmb_data ( const int num_rows, const int num_cols,
                        FILE * file_ptr );
int write_pgmb ( const Image * img, FILE * file_ptr );
Image *read_ppmb_data ( const int num_rows, const int num_cols,
                        FILE * file_ptr );
int write_ppmb ( const Image * img, FILE * file_ptr );

/* pseudo_color.c */
Image *pseudo_color ( const Image * in_img, const ColorMap color_map );

/* quant_neural.c */
Image *quant_neural ( const Image * in_img, const int num_colors,
                      const int sampling_factor );

/* quant_wan.c */
Image *quant_wan ( const Image * in_img, const int num_colors,
                   const int num_bits );

/* quant_wu.c */
Image *quant_wu ( const Image * in_img, const int num_colors );

/* rectangularity.c */
PointList *calc_min_area_rect ( const Image * img, const int label,
                                const PointList * cont, double *area );
double calc_rectangularity ( const Image * img, const int label,
                             const PointList * cont );

/* rotate.c */
Image *rotate_img ( const Image * in_img, const double angle );

/* scale.c */
Image *scale_img ( const Image * in_img, const int num_rows,
                   const int num_cols );

/* srm.c */
Image *srm ( const Image * in_img, const double Q_value,
             const double size_factor );

/* strel.c */
Bool se_origin_at_center ( const Strel * se );
void set_strel_origin ( const int origin_row, const int origin_col,
                        Strel * se );
void print_strel ( const Strel * se );
Strel *make_line_strel ( const int length, const double angle );
Strel *make_rect_strel ( const int num_rows, const int num_cols );
Strel *make_disk_strel ( const int radius );
Strel *make_flat_strel ( byte ** data, const int num_rows, const int num_cols,
                         const int origin_row, const int origin_col );

/* threshold.c */
Image *threshold_img ( const Image * gray_img, const int threshold );

/* threshold_bernsen.c */
Image *threshold_bernsen ( const Image * in_img, const int win_size,
                           const int contrast_threshold );

/* threshold_huang.c */
int threshold_huang ( const Image * img );

/* threshold_iter.c */
int threshold_iter ( const Image * img );

/* threshold_kapur.c */
int threshold_kapur ( const Image * img );

/* threshold_li.c */
int threshold_li ( const Image * img );

/* threshold_moment.c */
int threshold_moment ( const Image * img );

/* threshold_niblack.c */
Image *threshold_niblack ( const Image * in_img, const int win_size,
                           const double k_value );

/* threshold_otsu.c */
int threshold_otsu ( const Image * img );

/* threshold_renyi.c */
int threshold_renyi ( const Image * img );

/* threshold_sauvola.c */
Image *threshold_sauvola ( const Image * in_img, const int win_size,
                           const double k_value, const double R_value );

/* threshold_savakis.c */
Image *threshold_savakis ( const Image * in_img, const int win_size,
                           int ( *global_thresholding_method ) ( const Image
                                                                 * ) );

/* threshold_savakis_opt.c */
Image *threshold_savakis_opt ( const Image * in_img, const int win_size,
                               int ( *global_thresholding_method ) ( const Image
                                                                     * ) );

/* threshold_shanbhag.c */
int threshold_shanbhag ( const Image * img );

/* threshold_yen.c */
int threshold_yen ( const Image * img );

/* trace_contour.c */
Chain *alloc_chain ( const int max_length );
void free_chain ( Chain * chain );
int get_chain_len ( const Chain * chain );
int *get_chain_rows ( const Chain * chain );
int *get_chain_cols ( const Chain * chain );
int *get_chain_dir ( const Chain * chain );
ChainList *alloc_chain_list ( const int num_chains, const int max_length,
                              const int num_rows, const int num_cols );
void free_chain_list ( ChainList * chain_list );
Chain *get_chain ( const ChainList * chain_list, const int chain_index );
int get_num_chains ( const ChainList * chain_list );
Image *chain_list_to_img ( const ChainList * chain_list );
Chain *trace_contour ( const Image * lab_img, const int label,
                       const int max_length );
ChainList *trace_contours ( const Image * lab_img, const int max_length );

/* util.c */
clock_t start_timer ( void );
double stop_timer ( const clock_t start_time );
double *gauss_1d ( const double sigma, int *mask_size );
int select_kth_smallest ( const int num_elems, const int k, int *data );
int find_median ( const int num_elems, int *data );
void sort_int ( const int num_elems, int *data );

/* zernike_moments.c */
void free_zernike_basis ( ZernikeBasis * basis );
ZernikeBasis *calc_zernike_basis ( const int max_order, const int radius );
double *calc_zernike_moments ( const Image * in_img, const int label,
                               const ZernikeBasis * basis );

/* math.h */
double cbrt ( double );

#endif
 
Po załadowaniu tego pliku są błedy a bez tego pliku domaga się by go załadować.


Reszta kodów w załączniku. Może komuś się przydadzą przy operacjach na mapach bitowych.

Fourier description



Free and open-source image processing and analysis library

Fourier is a portable image processing and analysis library written in ANSI C. Fourier implements operations such as color space conversions, enhancement, morphology, edge detection, thresholding, segmentation, noise removal, and visual feature extraction.

Here are some key features of "Fourier":

· Portability (written in strict ANSI C)
· Efficiency (optimized for speed without sacrificing from clarity or deviating from the ANSI standard)
· Flexibility (two modes of pixel access: linear and multidimensional)
· Adaptability (easy to isolate a particular function and use it in other software)
· Robustness (run-time checks for validating objects to reduce crashes due to programmer errors)
· Thread-safe error-handling (two modes, one for novices and the other for advanced users)
· Extensive documentation with Doxygen (references to the relevant literature)


KOD cpp:     UKRYJ  
#ifndef IMAGE_H
#define IMAGE_H
#include <ctype.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/* DEFINE CONSTANTS */

#define MAX_LINE_LEN 80     /**< Max. line length */

#define NEW_LINE '\n'       /**< New line character */

#define NUM_GRAY 256        /**< Number of gray levels in an 8-bit gray-scale image */

#define MAX_GRAY 255        /**< Max. gray level in an 8-bit gray-scale image */

#define OBJECT 1

#define BACKGROUND 0

#define PI ( 3.1415926535897932384626433832795 )

#define TWO_PI ( 6.283185307179586476925286766559 )

#define PI_OVER_3 ( 1.0471975511965977461542144610932 )

#define FIVE_PI_OVER_3 ( 5.2359877559829887307710723054658 )

#define SQRT3 ( 1.7320508075688772935274463415059 )

/* MACROS */

/**
 * @brief Sets the function name
 */


#define SET_FUNC_NAME( f_name ) static const char func_name[] = f_name

/**
 * @brief Determines whether or not a given pointer is NULL
 */


#define IS_NULL( x ) ( ( x ) == NULL )

/**
 * @brief Determines whether or not a given number is in the range of [0,255]
 * @warning This is not a safe macro
 */


#define IS_BYTE( x ) ( ( 0 <= ( x ) ) && ( ( x ) < 256 ) )

/**
 * @brief Determines whether or not a given floating-point number is close to 0.0
 */


#define IS_ZERO( x ) ( fabs ( ( x ) ) < DBL_EPSILON )

/**
 * @brief Determines whether or not a given floating-point number is positive
 */


#define IS_POS( x ) ( ( x ) > DBL_EPSILON )

/**
 * @brief Determines whether or not a given floating-point number is negative
 */


#define IS_NEG( x ) ( ( x ) < -DBL_EPSILON )

#define IS_ODD( x ) ( ( x ) % 2 )
#define IS_EVEN( x ) ( !IS_ODD ( ( x ) ) )
#define IS_POS_ODD( x ) ( ( ( x ) > 0 ) && IS_ODD ( x ) )

/**
 * @brief Determines whether or not a given number is in the range of [0,1]
 * @warning This is not a safe macro
 */

#define IS_IN_0_1( x ) ( ! ( IS_NEG ( ( x )  ) || IS_POS ( ( x ) - 1.0 ) ) )

#define SAFE_LOG( x ) ( ( ( x ) > 0.0 ) ? log ( ( x ) ) : ( 0.0 ) )

#define IS_VALID_OBJ( x ) ( !IS_NULL ( ( x ) ) && ( x )->type )

#define GET_NUM_ROWS( x ) ( ( x )->num_rows )

#define GET_NUM_COLS( x ) ( ( x )->num_cols )

#define L1_DIST_3D( x1, y1, z1, x2, y2, z2 )\
 ( abs ( ( x1 ) - ( x2 ) ) + abs ( ( y1 ) - ( y2 ) )  + abs ( ( z1 ) - ( z2 ) ) )


#define L2_NORM_3D_SQR( x, y, z )\
 ( ( x ) * ( x ) + ( y ) * ( y ) + ( z ) * ( z ) )


#define L2_NORM_3D( x, y, z )\
 sqrt ( L2_NORM_3D_SQR ( ( x ), ( y ), ( z ) ) )


#define L2_NORM_2D_SQR( x, y )\
 ( ( x ) * ( x ) + ( y ) * ( y ) )


#define L2_NORM_2D( x, y )\
 sqrt ( L2_NORM_2D_SQR ( ( x ), ( y ) ) )


#define L2_DIST_3D_SQR( x1, y1, z1, x2, y2, z2 )\
 ( ( ( x1 ) - ( x2 ) ) * ( ( x1 ) - ( x2 ) )\
 + ( ( y1 ) - ( y2 ) ) * ( ( y1 ) - ( y2 ) )\
 + ( ( z1 ) - ( z2 ) ) * ( ( z1 ) - ( z2 ) ) )


#define L2_DIST_3D( x1, y1, z1, x2, y2, z2 )\
 sqrt ( L2_DIST_3D_SQR ( ( x1 ), ( y1 ), ( z1 ), ( x2 ), ( y2 ), ( z2 ) ) )


#define L2_DIST_2D_SQR( x1, y1, x2, y2 )\
 ( ( ( x1 ) - ( x2 ) ) * ( ( x1 ) - ( x2 ) )\
 + ( ( y1 ) - ( y2 ) ) * ( ( y1 ) - ( y2 ) ) )\

#define L2_DIST_2D( x1, y1, x2, y2 )\
 sqrt ( L2_DIST_2D_SQR( ( x1 ), ( y1 ), ( x2 ), ( y2 ) ) )\

#define ROUND( X )      ( ( X ) < 0.0 ? ( X ) - 0.5 : ( X ) + 0.5 )

#define DEG_TO_RAD( D ) ( 0.017453292519943295769236907684886 * ( D ) )

#define CLAMP_BYTE( X ) ( ( X ) < 0 ? 0 : ( ( X ) > MAX_GRAY ) ? MAX_GRAY : ( X ) )

#define MIN_2( A, B ) ( ( A ) < ( B ) ? ( A ) : ( B ) )

#define MAX_2( A, B ) ( ( A ) < ( B ) ? ( B ) : ( A ) )

#define MIN_3( A, B, C ) MIN_2 ( ( A ), MIN_2 ( ( B ), ( C ) ) )

#define MAX_3( A, B, C ) MAX_2 ( ( A ), MAX_2 ( ( B ), ( C ) ) )

/** @cond INTERNAL_MACRO */
#define SWAP_INT( a, b ) { tmp_var = ( a ); ( a ) = ( b ); ( b ) = tmp_var; }

/** @endcond INTERNAL_MACRO */

/** @cond INTERNAL_MACRO */
#define SORT_INT( a, b ) { if ( ( a ) > ( b ) ) SWAP_INT ( a, b ); }

/** @endcond INTERNAL_MACRO */

#define DIST_FUNC L2_DIST_3D
#define DIST_FUNC_SQR L2_DIST_3D_SQR
#define ACOS acos

/**
 * @brief If DEBUG_ON is defined, prints file name and line number and then
 *        calls the variable-argument debug function; otherwise does nothing
 */


#ifdef DEBUG_ON
# define DEBUG fprintf ( stderr, "Debug in %s: ", func_name ), debug
#else
# define DEBUG do_nothing
#endif

/**
 * @brief Prints file name and line number and then
 *        calls the variable-argument warning function
 */


#define WARNING fprintf ( stderr, "Warning in %s: ", func_name ), warning

/**
 * @brief Prints file name and line number and then
 *        calls the variable-argument error function
 */


#define ERROR fprintf ( stderr, "Error in %s: ", func_name ), error

/**
 * @brief Prints file name and line number and then
 *        calls the variable-argument fatal function
 */


#define FATAL fprintf ( stderr, "Fatal error in %s: ", func_name ), fatal

/**
 * @brief Prints file name, line number, and the given error message;
 *        conditionally aborts the program and then returns the value of
 *        the given error number
 */


#define ERROR_RET( err_msg, err_no ) \
         do \
          { \
           fprintf ( stderr, "Error in %s: %s\n", func_name, ( err_msg ) ); \
           fflush ( stderr ); \
           if ( get_err_mode ( ) ) abort ( ); \
           return ( err_no ) ; \
          } while ( 0 )


#define MALLOC_STRUCT( x ) ( ( x * ) malloc ( sizeof ( x ) ) )

#define CALLOC_STRUCT( x ) ( ( x * ) calloc ( 1, sizeof ( x ) ) )

#define NEG_ONE_POW( x ) ( IS_EVEN ( ( x ) ) ? ( 1 ) : ( -1 ) )

/* ENUMERATIONS/TYPEDEFS */

enum
{

 E_SUCCESS = 0,  /**< success */

 E_FAILURE,      /**< failure */

 E_INVARG,       /**< invalid argument */

 E_INVOBJ,       /**< invalid object */

 E_INVBPP,       /**< invalid pixel depth */

 E_NULL,         /**< null pointer */

 E_NOMEM,        /**< insufficient memory */

 E_DIVZERO,      /**< divide by zero */

 E_UNIMPL,       /**< feature not implemented */

 E_UNFMT,        /**< unknown file format */

 E_FOPEN,        /**< file open error */

 E_FREAD,        /**< file read error */

 E_FEOF          /**< end of file reached */

}; /**< Error Codes */

typedef unsigned char byte; /**< Byte Type */

typedef enum
{

 false = 0,

 true = 1

} Bool; /**< Boolean Type */

#define IS_BOOL( x ) ( ( ( x ) == false ) || ( ( x ) == true ) )

typedef enum
{

 PIX_INVALID = 0,  /**< invalid */

 PIX_BIN,          /**< binary */

 PIX_GRAY,         /**< gray-scale */

 PIX_RGB,          /**< RGB color */

 PIX_INT_1B,       /**< single-band integer */

 PIX_INT_3B,       /**< 3-band integer */

 PIX_DBL_1B,       /**< single-band double */

 PIX_DBL_3B        /**< 3-band double */

} PixelType; /**< Pixel Type Enumeration */

typedef enum
{

 FMT_UNKNOWN = 0, /**< Unknown */

 FMT_BMP,            /**< BMP */

 FMT_GIF,            /**< GIF */

 FMT_JPG,            /**< JPG */

 FMT_PBMA,           /**< Plain/ASCII PBM */

 FMT_PBM,            /**< Raw/Binary PBM */

 FMT_PCX,            /**< PCX */

 FMT_PGMA,           /**< Plain/ASCII PGM */

 FMT_PGM,            /**< Raw/Binary PGM */

 FMT_PNG,            /**< PNG */

 FMT_PPMA,           /**< Plain/ASCII PPM */

 FMT_PPM,            /**< Raw/Binary PPM */

 FMT_PSD,            /**< PSD */

 FMT_RAS,            /**< RAS */

 FMT_TGA,            /**< TGA */

 FMT_TIFF            /**< TIFF */

} ImageFormat; /**< Image Format Enumeration */

typedef struct
{

 PixelType type;     /**< Pixel Type */

 int num_bands;      /**< Number of Bands */

 int num_rows;       /**< Number of Rows */

 int num_cols;       /**< Number of Columns */

 int max_pix_val;    /**< Max. Pixel Value (Defined only for PIX_BIN, PIX_GRAY, and PIX_RGB) */

 int num_cc;         /**< Number of Connected Components (Defined only for PIX_INT_1B) */

 union
 {

  byte *byte_data;       /**< For PIX_BIN, PIX_GRAY, PIX_RGB */

  int *int_data;         /**< For PIX_INT_1B, PIX_INT_3B */

  double *double_data;   /**< For PIX_DBL_1B, PIX_DBL_3B */

 } data_1d;  /**< 1-dimensional contiguous pixel array */

 union
 {

  byte **byte_data_1b;        /**< For PIX_BIN, PIX_GRAY */

  byte ***byte_data_3b;       /**< For PIX_RGB */

  int **int_data_1b;          /**< For PIX_INT_1B */

  int ***int_data_3b;         /**< For PIX_INT_3B */

  double **double_data_1b;    /**< For PIX_DBL_1B */

  double ***double_data_3b;   /**< For PIX_DBL_3B */

 } data_nd;  /**< 2 or 3-dimensional pixel array */

} Image; /**< Image Structure */

typedef enum
{

 HISTO_INVALID = 0,     /**< invalid */

 HISTO_INT,     /**< unnormalized */

 HISTO_DBL      /**< normalized */

} HistoType; /**< Histogram Type Enumeration */

typedef struct
{

 HistoType type;

 int num_bins;        /**< Number of Bins */

 int num_pixels;      /**< Number of Pixels in the Source Image */

 union
 {

  int *int_data;         /**< For HISTO_INT */

  double *double_data;   /**< For HISTO_DBL */

 } data;   /**< 1-dimensional contiguous histogram data array */

} Histo; /**< Histogram Structure */

typedef enum
{

 EM_INVALID = 0, /**< invalid */

 EM_MAE,     /**< Mean Absolute Error */

 EM_MSE,     /**< Mean Squared Error */

 EM_RMSE,    /**< Root Mean Squared Error */

 EM_PSNR,    /**< Peak Signal-to-Noise Ratio */

 EM_NMSE,    /**< Normalized Mean Squared Error */

 EM_NCD      /**< Normalized Color Distance */

} ErrorMeasure; /**< Error Measure Enumeration */

typedef enum
{

 GEN_INVALID = 0,    /**< invalid */

 GEN_VALID           /**< valid */

} GenericType; /**< Generic Type Enumeration */

typedef struct
{

 GenericType type;

 int length;                    /* length (# points) of the chain */
 int *row;                      /* row coordinates */
 int *col;                      /* column coordinates */
 int *dir;                      /* Freeman chaincode directions */

} Chain; /**< Chain Code Structure */

typedef struct
{

 GenericType type;

 int num_chains;                /* number of chains (objects) in the list */
 int max_length;                /* max. length of an object chain */
 int num_rows;                  /* # rows in the parent image */
 int num_cols;                  /* # columns in the parent image */
 Chain **chain;                 /* chain code pointers */

} ChainList; /**< Chain Code List Structure */

typedef struct
{

 int min_row;                   /* minimum row coordinate */
 int max_row;                   /* maximum row coordinate */
 int min_col;                   /* minimum column coordinate */
 int max_col;                   /* maximum column coordinate */

} Box; /**< Bounding-Box Structure */

typedef enum
{

 CMAP_INVALID = 0,

 CMAP_HSV,

 CMAP_INFRARED,

 CMAP_JET

} ColorMap; /**< Color Map Enumeration */

typedef struct
{
 double row;
 double col;
} Point;

typedef struct
{

 GenericType type;

 int num_pts;                   /* number of points in the list */
 Point **point;                 /* Point pointers */

} PointList; /**< 2-Dimensional Point List Structure */

typedef struct
{
 double maj_axis_len;
 double min_axis_len;
 double aspect_ratio;
 double eccentricity;
 double orientation;
} EllipseFeatures;

typedef struct
{
 double mean;
 double stdev;
 double entropy;
 double roughness;
} RadialDistFeatures;

typedef struct
{
 double F1;
 double F2;
 double F3;
 double F4;
} CSMoments;

typedef struct
{
 double mean;
 double variance;
 double skewness;
 double kurtosis;
} ChordLenStats;

typedef struct
{
 double m00;
 double m01;
 double m10;
 double m11;
 double m02;
 double m20;
 double m12;
 double m21;
 double m03;
 double m30;
} GeoMoments;

typedef struct
{
 double phi1;
 double phi2;
 double phi3;
 double phi4;
 double phi5;
 double phi6;
 double phi7;
} HuMoments;

typedef struct
{
 double I1;
 double I2;
 double I3;
 double I4;
} AffineMoments;

typedef struct
{
 double real;
 double imag;
} Complex;

typedef struct
{
 GenericType type;

 int max_order;
 int radius;
 int num_terms;
 Complex ***data;

} ZernikeBasis;

typedef enum
{

 STRL_INVALID = 0,  /**< invalid */

 STRL_LINE,        /**< flat line */

 STRL_RECT,        /**< flat rectangle */

 STRL_DISK,        /**< flat disk */

 STRL_FLAT,        /**< arbitrary flat */

 STRL_NONFLAT      /**< arbitrary non-flat */

} StrelType; /**< Structuring Element Type Enumeration */

typedef struct
{

 StrelType type;

 int num_rows;

 int num_cols;

 int origin_row;

 int origin_col;

 byte *data_1d;

 byte **data_2d;

} Strel;

typedef enum
{

 SEQ_INVALID = 0,  /**< invalid */

 SEQ_OC,           /**< */

 SEQ_CO,           /**< */

 SEQ_OCO,          /**< */

 SEQ_COC           /**< */

} ASFSeq; /**< Alternating Sequential Filter Sequence */

typedef enum
{
 TYPE_UNKNOWN /* unknown type */ ,
 UCHAR /* unsigned char */ ,
 SCHAR /* signed char */ ,
 USHORT /* unsigned short int */ ,
 SSHORT /* signed short int */ ,
 INT /* signed int */ ,
 ULINT /* unsigned long int */ ,
 FLOAT /* float */ ,
 DOUBLE                         /* double */
} bufferType;

typedef enum
{
 UNKNOWN_FILTER = 0 /* unknown filter type */ ,
 ALPHA_DERICHE = 1 /* Deriche's filter (exponential (- alpha |X|)) */ ,
 GAUSSIAN_DERICHE = 2           /* gaussian approximation (Deriche's coefficients) */
} recursiveFilterType;

/* FUNCTION PROTOTYPES */

/* add_noise.c */
Image *add_gaussian_noise ( const Image * in_img, const double mean,
                            const double stdev );
Image *add_saltpepper_noise ( const Image * in_img, const double prob );
Image *add_speckle_noise ( const Image * in_img, const double stdev );
Image *add_uncorr_impulsive_noise ( const Image * in_img, const double prob,
                                    const Bool is_uniform );
Image *add_corr_impulsive_noise ( const Image * in_img,
                                  const double overall_prob,
                                  const double red_prob,
                                  const double green_prob,
                                  const double blue_prob,
                                  const Bool is_uniform );
double calc_error ( const ErrorMeasure err_meas, const Image * orig_img,
                    const Image * est_img );

/* alloc_nd.c */
void *alloc_nd ( const size_t elem_size, const int dim_count, ... );
void free_nd ( void *base_ptr, const int dim_count );

/* arith.c */
Image *add_img ( const Image * in_img_a, const Image * in_img_b );
Image *sub_img ( const Image * in_img_a, const Image * in_img_b );
Image *mult_img ( const Image * in_img_a, const Image * in_img_b );
Image *abs_diff_img ( const Image * in_img_a, const Image * in_img_b );
Image *union_img ( const Image * in_img_a, const Image * in_img_b );
Image *intersect_img ( const Image * in_img_a, const Image * in_img_b );
Image *add_img_scalar ( const Image * in_img, const double scalar );
Image *sub_img_scalar ( const Image * in_img, const double scalar );
Image *mult_img_scalar ( const Image * in_img, const double scalar );
Image *div_img_scalar ( const Image * in_img, const double scalar );

/* basic_shape_desc.c */
int count_obj_pixels ( const Image * img, const int label );
double calc_obj_area ( const Image * img, const int label );
double calc_aspect_ratio ( const Image * img, const int label );
Point *calc_obj_centroid ( const Image * img, const int label );
Image *crop_object ( const Image * in_img, const int label );
Image *circular_scale ( const Image * in_img, const int label,
                        const int radius );
double calc_triangularity ( const Image * img, const int label );
double calc_ellipticity ( const Image * img, const int label );
EllipseFeatures *calc_ellipse_feats ( const Image * img, const int label );
double calc_equi_diameter ( const Image * img, const int label );
double calc_elliptic_var ( const Image * img, const int label,
                           const PointList * cont );
double calc_circularity ( const Image * img, const int label,
                          const PointList * cont );
RadialDistFeatures *calc_radial_dist_feats ( const Image * img, const int label,
                                             const PointList * cont );
ChordLenStats *calc_chord_len_stats ( const PointList * cont );

/* bmp_header.c */
int read_bmp_header ( FILE * file_ptr, PixelType * pix_type, int *num_rows,
                      int *num_cols, Bool * is_bottom_up );
void write_bmp_header ( const PixelType pix_type, const int num_rows,
                        const int num_cols, FILE * file_ptr );

/* bmp_io.c */
Image *read_bmp1_data ( const int num_rows, const int num_cols,
                        const Bool is_bottom_up, FILE * file_ptr );
int write_bmp1 ( const Image * img, FILE * file_ptr );
Image *read_bmp8_data ( const int num_rows, const int num_cols,
                        const Bool is_bottom_up, FILE * file_ptr );
int write_bmp8 ( const Image * img, FILE * file_ptr );
Image *read_bmp24_data ( const int num_rows, const int num_cols,
                         const Bool is_bottom_up, FILE * file_ptr );
int write_bmp24 ( const Image * img, FILE * file_ptr );

/* ccv_enhance.c */
Image *ccv_enhance ( const Image * in_img, const double sharp_factor );

/* colorspaces.c */
Image *rgb_to_nrgb ( const Image * in_img, double undefined );
Image *rgb_to_ohta ( const Image * in_img );
Image *ohta_to_rgb ( const Image * in_img );
Image *rgb_to_hsi ( const Image * in_img, double undefined );
Image *rgb_to_hsi_kender ( const Image * in_img, double undefined );
Image *hsi_to_rgb ( const Image * in_img );
Image *rgb_to_hsv ( const Image * in_img, double undefined );
Image *hsv_to_rgb ( const Image * in_img );
Image *rgb_to_hsl ( const Image * in_img, double undefined );
Image *hsl_to_rgb ( const Image * in_img );
Image *rgb_to_lab ( const Image * in_img );
Image *lab_to_rgb ( const Image * in_img );
Image *rgb_to_luv ( const Image * in_img );
Image *luv_to_rgb ( const Image * in_img );
Image *rgb_to_sct ( const Image * in_img, double undefined );
Image *sct_to_rgb ( const Image * in_img );
Image *lab_to_lch ( const Image * in_img, double undefined );
Image *lch_to_lab ( const Image * in_img );
Image *rgb_to_l1l2l3 ( const Image * in_img, double undefined );

/* convex_hull.c */
PointList *calc_2d_convex_hull ( const PointList * inp_point_list );
double calc_max_diameter ( const PointList * hull );
double calc_compactness ( const Image * img, const int label,
                          const PointList * hull );
double calc_solidity ( const Image * in_img, const int label,
                       const PointList * hull );

/* cs_moments.c */
CSMoments *calc_cs_moments ( const Image * img, const int label,
                             const PointList * cont );

/* draw_line.c */
void draw_line ( const Point * point1, const Point * point2,
                 const int pix_value, Image * img );

/* edge_advanced.c */
Image *detect_edge_canny ( const Image * in_img, const double sigma,
                           const double low, const double high );
Image *detect_edge_deriche ( const Image * in_img, const double alpha,
                             const double low, const double high );

/* edge_basic.c */
Image *detect_edge_roberts ( const Image * in_img );
Image *detect_edge_prewitt ( const Image * in_img );
Image *detect_edge_sobel ( const Image * in_img );
Image *detect_edge_log ( const Image * in_img, const double sigma );

/* ellipticity.c */
double calc_elliptic_var ( const Image * img, const int label,
                           const PointList * cont );

/* error.c */
Bool get_err_mode ( void );
void set_err_mode ( const Bool err_mode );
Bool get_warn_mode ( void );
void set_warn_mode ( const Bool warn_mode );
void do_nothing ( const char *format, ... );
void debug ( const char *format, ... );
void warning ( const char *format, ... );
void error ( const char *format, ... );
void fatal ( const char *format, ... );
const char *error_str ( const int err_no );

/* expand_histo.c */
Image *expand_histo ( const Image * in_img, const double percent_clip );

/* fill_region.c */
int calc_box_area ( const Box * box );
int get_box_height ( const Box * box );
int get_box_width ( const Box * box );
Box *alloc_box ( void );
void free_box ( Box * box );
Box *calc_box ( const Image * img, const int label );
void fill_region ( const int seed_row, const int seed_col, const int label,
                   Image * img );

/* filter_abvdf.c */
Image *filter_abvdf ( const Image * in_img, const int win_size,
                      const int k_value, const double tolerance );

/* filter_acwddf.c */
Image *filter_acwddf ( const Image * in_img, const int win_size,
                       const int lambda, const double tolerance );

/* filter_acwvdf.c */
Image *filter_acwvdf ( const Image * in_img, const int win_size,
                       const int lambda, const double tolerance );

/* filter_acwvmf.c */
Image *filter_acwvmf ( const Image * in_img, const int win_size,
                       const int lambda, const double tolerance );

/* filter_adap_smooth.c */
Image *filter_adap_smooth ( const Image * in_img, const double k_value,
                            const int num_iters );

/* filter_ahdf.c */
Image *filter_ahdf ( const Image * in_img, const int win_size );

/* filter_amnfe.c */
Image *filter_amnfe ( const Image * in_img, const int win_size,
                      const double k_value );

/* filter_amnfg.c */
Image *filter_amnfg ( const Image * in_img, const int win_size,
                      const double k_value );

/* filter_annf.c */
Image *filter_annf ( const Image * in_img, const int win_size );

/* filter_annmf.c */
Image *filter_annmf ( const Image * in_img, const int win_size );

/* filter_asbvdf_mean.c */
Image *filter_asbvdf_mean ( const Image * in_img, const int win_size );

/* filter_asbvdf_rank.c */
Image *filter_asbvdf_rank ( const Image * in_img, const int win_size );

/* filter_asddf_mean.c */
Image *filter_asddf_mean ( const Image * in_img, const int win_size );

/* filter_asddf_rank.c */
Image *filter_asddf_rank ( const Image * in_img, const int win_size );

/* filter_asvmf_mean.c */
Image *filter_asvmf_mean ( const Image * in_img, const int win_size );

/* filter_asvmf_rank.c */
Image *filter_asvmf_rank ( const Image * in_img, const int win_size );

/* filter_atvmf.c */
Image *filter_atvmf ( const Image * in_img, const int win_size,
                      const int k_value );

/* filter_avmf.c */
Image *filter_avmf ( const Image * in_img, const int win_size,
                     const int k_value, const double tolerance );

/* filter_bilateral.c */
Image *filter_bilateral ( const Image * in_img, const double range_sigma,
                          const double spatial_sigma );

/* filter_bvdf.c */
Image *filter_bvdf ( const Image * in_img, const int win_size );

/* filter_cbrf.c */
Image *filter_cbrf ( const Image * in_img, const int win_size );

/* filter_cfm.c */
Image *filter_cfm ( const Image * in_img, const int win_size,
                    const double C_value, const double t_value );

/* filter_ddf.c */
Image *filter_ddf ( const Image * in_img, const int win_size );

/* filter_ebvdf.c */
Image *filter_ebvdf ( const Image * in_img, const int win_size );

/* filter_eddf.c */
Image *filter_eddf ( const Image * in_img, const int win_size );

/* filter_evmf.c */
Image *filter_evmf ( const Image * in_img, const int win_size );

/* filter_exvmf.c */
Image *filter_exvmf ( const Image * in_img, const int win_size );

/* filter_fddrhf.c */
Image *filter_fddrhf ( const Image * in_img, const double weight,
                       const double h_value, const double k_value,
                       const double beta, const double gamma );

/* filter_ffnrf.c */
Image *filter_ffnrf ( const Image * in_img, const int win_size,
                      const int K_value, const double alpha );

/* filter_fmvmf.c */
Image *filter_fmvmf ( const Image * in_img, const int win_size,
                      const double beta );

/* filter_fmvdf.c */
Image *filter_fmvdf ( const Image * in_img, const int win_size,
                      const double K_value, const double alpha );

/* filter_fmddf.c */
Image *filter_fmddf ( const Image * in_img, const int win_size,
                      const double K1_value, const double K2_value,
                      const double alpha );

/* filter_fovdf.c */
Image *filter_fovdf ( const Image * in_img, const int win_size,
                      const double beta, const double gamma );

/* filter_fovmf.c */
Image *filter_fovmf ( const Image * in_img, const int win_size,
                      const double beta, const double gamma );

/* filter_fpgf.c */
Image *filter_fpgf ( const Image * in_img, const int win_size,
                     const int pg_size, const double dist_thresh );

/* filter_fvdf.c */
Image *filter_fvdf ( const Image * in_img, const int win_size,
                     const double beta, const double gamma );

/* filter_fvdrhf.c */
Image *filter_fvdrhf ( const Image * in_img, const double weight,
                       const double h_value, const double k_value,
                       const double beta, const double gamma );

/* filter_fvmf.c */
Image *filter_fvmf ( const Image * in_img, const int win_size,
                     const double beta, const double gamma );

/* filter_fvmrhf.c */
Image *filter_fvmrhf ( const Image * in_img, const double weight,
                       const double h_value, const double k_value,
                       const double beta, const double gamma );

/* filter_gaussian.c */
Image *filter_gaussian ( const Image * in_img, const double stdev );
Image *filter_ani_gaussian ( const Image * in_img, const double sigma_v,
                             const double sigma_u, const double phi,
                             const int order_v, const int order_u );

/* filter_giws.c */
Image *filter_giws ( const Image * in_img, const int win_size );

/* filter_gvdf.c */
Image *filter_gvdf ( const Image * in_img, const int win_size );

/* filter_hdf.c */
Image *filter_hdf ( const Image * in_img, const int win_size );

/* filter_kuwahara.c */
Image *filter_kuwahara ( const Image * in_img, const int k_value );

/* filter_kvmf.c */
Image *filter_kvmf ( const Image * in_img, const int win_size );

/* filter_lce.c */
Image *filter_lce ( const Image * in_img, const int win_size );

/* filter_lee.c */
Image *filter_lee ( const Image * in_img, const int win_size,
                    const double stdev );

/* filter_lum.c */
Image *filter_lum ( const Image * in_img, const int win_size,
                    const int k_value );

/* filter_mad.c */
Image *filter_mad ( const Image * in_img, const int win_size, const int a_value,
                    const int b_value );

/* filter_majority.c */
Image *filter_majority ( const Image * in_img, const int win_size );

/* filter_mcv.c */
Image *filter_mcv ( const Image * in_img, const int win_size );

/* filter_mcwvmf.c */
Image *filter_mcwvmf ( const Image * in_img, const int win_size,
                       const double weight );

/* filter_mean.c */
Image *filter_mean ( const Image * in_img, const int win_size );

/* filter_median.c */
Image *filter_classical_median ( const Image * in_img, const int win_size );
Image *filter_running_median ( const Image * in_img, const int win_size );
Image *filter_3x3_median ( const Image * in_img );
Image *filter_const_time_median ( const Image * in_img, const int win_size );

/* filter_mlv.c */
Image *filter_mlv ( const Image * in_img, const int win_size );

/* filter_mmf.c */
Image *filter_mmf ( const Image * in_img, const int win_size );

/* filter_mpv.c */
Image *filter_mpv ( const Image * in_img, const int win_size );

/* filter_perona_malik.c */
Image *filter_perona_malik ( const Image * in_img, const double k_value,
                             const double lambda, const int num_iters );

/* filter_pgf.c */
Image *filter_pgf ( const Image * in_img, const int win_size,
                    const double alpha );

/* filter_pvof.c */
Image *filter_pvof ( const Image * in_img, const int win_size );

/* filter_robust_aniso.c */
Image *filter_robust_aniso ( const Image * in_img, const int num_iters );

/* filter_rodsvmf.c */
Image *filter_rodsvmf ( const Image * in_img, const int win_size,
                        const double dist_thresh );

/* filter_sbvdf_mean.c */
Image *filter_sbvdf_mean ( const Image * in_img, const int win_size,
                           const double lambda );

/* filter_sbvdf_rank.c */
Image *filter_sbvdf_rank ( const Image * in_img, const int win_size,
                           const double lambda );

/* filter_sddf_mean.c */
Image *filter_sddf_mean ( const Image * in_img, const int win_size,
                          const double lambda );

/* filter_sddf_rank.c */
Image *filter_sddf_rank ( const Image * in_img, const int win_size,
                          const double lambda );

/* filter_sdrom.c */
Image *filter_sdrom ( const Image * in_img, const int t0, const int t1,
                      const int t2, const int t3 );

/* filter_sep_median */
Image *filter_sep_median ( const Image * in_img, const int win_size );

/* filter_sigma.c */
Image *filter_sigma ( const Image * in_img, const int win_size,
                      const int k_value );

/* filter_snn.c */
Image *filter_snn ( const Image * in_img );

/* filter_susan.c */
Image *filter_susan ( const Image * in_img, const int bright_thresh,
                      const double dist_thresh );

/* filter_svmf_mean.c */
Image *filter_svmf_mean ( const Image * in_img, const int win_size,
                          const double lambda );

/* filter_svmf_rank.c */
Image *filter_svmf_rank ( const Image * in_img, const int win_size,
                          const double lambda );

/* filter_vmf.c */
Image *filter_vmf ( const Image * in_img, const int win_size );

/* filter_vmrhf.c */
Image *filter_vmrhf ( const Image * in_img, const double weight,
                      const double h_value, const double k_value );

/* filter_vsdromf.c */
Image *filter_vsdromf ( const Image * in_img, const double t0, const double t1,
                        const double t2, const double t3 );

/* gen_fourier_desc.c */
double *calc_gen_fourier_desc ( const Image * in_img, const int label,
                                const int rad_res, const int ang_res,
                                const int radius );

/* graph_segment.c */
Image *graph_segment ( const Image * in_img, const double sigma,
                       const double k_value, const int min_size,
                       const Bool fast );

/* gray_morpho.c */
Image *morpho_gradient ( const Image * in_img, const Strel * se );
Image *h_minima_transform ( const Image * in_img, const Strel * se,
                            const int contrast );
Image *h_maxima_transform ( const Image * in_img, const Strel * se,
                            const int contrast );
Image *regional_minima ( const Image * in_img, const Strel * se );
Image *regional_maxima ( const Image * in_img, const Strel * se );
Image *extended_minima_transform ( const Image * in_img, const Strel * se,
                                   const int contrast );
Image *extended_maxima_transform ( const Image * in_img, const Strel * se,
                                   const int contrast );

/* histogram.c */
HistoType get_histo_type ( const Histo * histo );
Bool is_int_histo ( const Histo * histo );
Bool is_double_histo ( const Histo * histo );
int get_num_bins ( const Histo * histo );
int get_num_pixels ( const Histo * histo );
void *get_histo_data ( const Histo * histo );
Histo *alloc_histo ( const HistoType type, const int num_bins );
void free_histo ( Histo * histo );
Histo *create_histo ( const Image * img );
Histo *normalize_histo ( const Histo * in_histo );
void *accumulate_histo ( const Histo * histo );
Image *histo_to_img ( const Histo * histo, const int num_rows,
                      const int bin_spacing );
Histo *smooth_histo_local ( const Histo * in_histo, const int mask_size );
Histo *smooth_histo_gauss ( const Histo * in_histo, const double sigma );
Image *hyperbolize_histo ( const Image * in_img, const double c_value );
Image *equalize_histo ( const Image * in_img );

/* image.c */
Bool is_bin_img ( const Image * img );
Bool is_gray_img ( const Image * img );
Bool is_rgb_img ( const Image * img );
Bool is_byte_img ( const Image * img );
Bool is_label_img ( const Image * img );
Bool is_bin_or_label_img ( const Image * img );
Bool is_bin_or_gray_img ( const Image * img );
Bool is_dbl_3b_img ( const Image * img );
Bool is_dbl_img ( const Image * img );
Bool is_int_img ( const Image * img );
PixelType get_pix_type ( const Image * img );
int get_num_bands ( const Image * img );
int get_num_rows ( const Image * img );
int get_num_cols ( const Image * img );
int get_num_cc ( const Image * img );
void *get_img_data_1d ( const Image * img );
void *get_img_data_nd ( const Image * img );
Image *alloc_img ( const PixelType pix_type, const int num_rows,
                   const int num_cols );
void free_img ( Image * img );
int count_colors ( const Image * img );
Image *rgb_to_gray ( const Image * rgb_img );
Image *negate_img ( const Image * in_img );
Bool img_dims_agree ( const Image * img_a, const Image * img_b );
Bool img_types_agree ( const Image * img_a, const Image * img_b );
Bool is_equal_img ( const Image * in_img_a, const Image * in_img_b );
Image *dbl_to_byte_img ( const Image * in_img );
Image *byte_to_dbl_img ( const Image * in_img );
void get_rgb_bands ( const Image * rgb_img, Image ** red_img,
                     Image ** green_img, Image ** blue_img );
Image *combine_rgb_bands ( const Image * red_img, const Image * green_img,
                           const Image * blue_img );
Image *clone_img ( const Image * in_img );
Image *alloc_const_img ( const PixelType pix_type, const int num_rows,
                         const int num_cols, const double value );

/* image_format.c */
int get_img_format ( FILE * file_ptr, ImageFormat * img_format );
const char *img_format_str ( const ImageFormat img_format );

/* image_io.c */
Image *read_img ( const char *file_name );
int write_img ( const Image * img, const char *file_name,
                const ImageFormat img_format );

/* invariant_moments.c */
GeoMoments *calc_geo_moments ( const Image * img, const int label );
GeoMoments *calc_central_moments ( const Image * img, const int label );
GeoMoments *calc_norm_central_moments ( const Image * img, const int label );
HuMoments *calc_hu_moments ( const Image * img, const int label );
AffineMoments *calc_affine_moments ( const Image * img, const int label );

/* jpeg_io.c */
Image *read_jpeg_data ( FILE * file_ptr );

/* label_cc.c */
Image *label_cc ( const Image * in_img, const int connectivity );
int *get_cc_areas ( const Image * lab_img );
Image *remove_small_cc ( const Image * lab_img, const int area_thresh );
Image *retain_largest_cc ( const Image * lab_img );
Image *label_to_bin ( const Image * lab_img );
Bool obj_touch_walls ( const Image * img );

/* label_cc_hybrid.c */
Image *label_cc_hybrid ( const Image * in_img, const int connectivity );

/* label_cc_trace.c */
Image *label_cc_trace ( const Image * in_img, const int connectivity );

/* match_histo.c */
Image *match_histo ( const Image * in_img, const Histo * target_histo );

/* morpho.c */
Image *close_img ( const Image * in_img, const Strel * se );
Image *dilate_img ( const Image * in_img, const Strel * se );
Image *erode_img ( const Image * in_img, const Strel * se );
Image *open_img ( const Image * in_img, const Strel * se );
Image *tophat_img ( const Image * in_img, const Strel * se );
Image *bothat_img ( const Image * in_img, const Strel * se );
Image *cond_dilate_img ( const Image * in_img, const Image * cond_img,
                         const Strel * se, const int num_iters );
Image *cond_erode_img ( const Image * in_img, const Image * cond_img,
                        const Strel * se, const int num_iters );
Image *inf_rec_img ( const Image * marker_img, const Image * cond_img,
                     const Strel * se );
Image *sup_rec_img ( const Image * marker_img, const Image * cond_img,
                     const Strel * se );
Image *open_rec_img ( const Image * in_img, const Strel * se_ero,
                      const Strel * se_conn );
Image *close_rec_img ( const Image * in_img, const Strel * se_dil,
                       const Strel * se_conn );
Image *open_rec_tophat_img ( const Image * in_img, const Strel * se_ero,
                             const Strel * se_conn );
Image *close_rec_tophat_img ( const Image * in_img, const Strel * se_dil,
                              const Strel * se_conn );
Image *filter_asf ( const Image * in_img, const Strel * se, const ASFSeq seq,
                    const int num_iters );
Image *fill_holes ( const Image * in_img, const Strel * se );
Image *clear_border ( const Image * in_img, const Strel * se );

/* pnm_header.c */
int read_pbmb_header ( FILE * file_ptr, int *num_rows, int *num_cols );
void write_pbmb_header ( const int num_rows, const int num_cols,
                         FILE * file_ptr );
int read_pgmb_header ( FILE * file_ptr, int *num_rows, int *num_cols,
                       int *max_gray );
void write_pgmb_header ( const int num_rows, const int num_cols,
                         const int max_gray, FILE * file_ptr );
int read_ppmb_header ( FILE * file_ptr, int *num_rows, int *num_cols,
                       int *max_rgb );
void write_ppmb_header ( const int num_rows, const int num_cols,
                         const int max_rgb, FILE * file_ptr );

/* point.c */
Point *alloc_point ( const double row_val, const double col_val );
void free_point ( Point * point );
PointList *alloc_point_list ( const int num_pts );
void free_point_list ( PointList * point_list );
int get_num_pts ( const PointList * point_list );
Point *calc_cont_centroid ( const PointList * cont );
Image *point_list_to_img ( const PointList * point_list, const int num_rows,
                           const int num_cols );
PointList *chain_to_point_list ( const Chain * chain );

/* pnm_io.c */
Image *read_pbmb_data ( const int num_rows, const int num_cols,
                        FILE * file_ptr );
int write_pbmb ( const Image * img, FILE * file_ptr );
Image *read_pgmb_data ( const int num_rows, const int num_cols,
                        FILE * file_ptr );
int write_pgmb ( const Image * img, FILE * file_ptr );
Image *read_ppmb_data ( const int num_rows, const int num_cols,
                        FILE * file_ptr );
int write_ppmb ( const Image * img, FILE * file_ptr );

/* pseudo_color.c */
Image *pseudo_color ( const Image * in_img, const ColorMap color_map );

/* quant_neural.c */
Image *quant_neural ( const Image * in_img, const int num_colors,
                      const int sampling_factor );

/* quant_wan.c */
Image *quant_wan ( const Image * in_img, const int num_colors,
                   const int num_bits );

/* quant_wu.c */
Image *quant_wu ( const Image * in_img, const int num_colors );

/* rectangularity.c */
PointList *calc_min_area_rect ( const Image * img, const int label,
                                const PointList * cont, double *area );
double calc_rectangularity ( const Image * img, const int label,
                             const PointList * cont );

/* rotate.c */
Image *rotate_img ( const Image * in_img, const double angle );

/* scale.c */
Image *scale_img ( const Image * in_img, const int num_rows,
                   const int num_cols );

/* srm.c */
Image *srm ( const Image * in_img, const double Q_value,
             const double size_factor );

/* strel.c */
Bool se_origin_at_center ( const Strel * se );
void set_strel_origin ( const int origin_row, const int origin_col,
                        Strel * se );
void print_strel ( const Strel * se );
Strel *make_line_strel ( const int length, const double angle );
Strel *make_rect_strel ( const int num_rows, const int num_cols );
Strel *make_disk_strel ( const int radius );
Strel *make_flat_strel ( byte ** data, const int num_rows, const int num_cols,
                         const int origin_row, const int origin_col );

/* threshold.c */
Image *threshold_img ( const Image * gray_img, const int threshold );

/* threshold_bernsen.c */
Image *threshold_bernsen ( const Image * in_img, const int win_size,
                           const int contrast_threshold );

/* threshold_huang.c */
int threshold_huang ( const Image * img );

/* threshold_iter.c */
int threshold_iter ( const Image * img );

/* threshold_kapur.c */
int threshold_kapur ( const Image * img );

/* threshold_li.c */
int threshold_li ( const Image * img );

/* threshold_moment.c */
int threshold_moment ( const Image * img );

/* threshold_niblack.c */
Image *threshold_niblack ( const Image * in_img, const int win_size,
                           const double k_value );

/* threshold_otsu.c */
int threshold_otsu ( const Image * img );

/* threshold_renyi.c */
int threshold_renyi ( const Image * img );

/* threshold_sauvola.c */
Image *threshold_sauvola ( const Image * in_img, const int win_size,
                           const double k_value, const double R_value );

/* threshold_savakis.c */
Image *threshold_savakis ( const Image * in_img, const int win_size,
                           int ( *global_thresholding_method ) ( const Image
                                                                 * ) );

/* threshold_savakis_opt.c */
Image *threshold_savakis_opt ( const Image * in_img, const int win_size,
                               int ( *global_thresholding_method ) ( const Image
                                                                     * ) );

/* threshold_shanbhag.c */
int threshold_shanbhag ( const Image * img );

/* threshold_yen.c */
int threshold_yen ( const Image * img );

/* trace_contour.c */
Chain *alloc_chain ( const int max_length );
void free_chain ( Chain * chain );
int get_chain_len ( const Chain * chain );
int *get_chain_rows ( const Chain * chain );
int *get_chain_cols ( const Chain * chain );
int *get_chain_dir ( const Chain * chain );
ChainList *alloc_chain_list ( const int num_chains, const int max_length,
                              const int num_rows, const int num_cols );
void free_chain_list ( ChainList * chain_list );
Chain *get_chain ( const ChainList * chain_list, const int chain_index );
int get_num_chains ( const ChainList * chain_list );
Image *chain_list_to_img ( const ChainList * chain_list );
Chain *trace_contour ( const Image * lab_img, const int label,
                       const int max_length );
ChainList *trace_contours ( const Image * lab_img, const int max_length );

/* util.c */
clock_t start_timer ( void );
double stop_timer ( const clock_t start_time );
double *gauss_1d ( const double sigma, int *mask_size );
int select_kth_smallest ( const int num_elems, const int k, int *data );
int find_median ( const int num_elems, int *data );
void sort_int ( const int num_elems, int *data );

/* zernike_moments.c */
void free_zernike_basis ( ZernikeBasis * basis );
ZernikeBasis *calc_zernike_basis ( const int max_order, const int radius );
double *calc_zernike_moments ( const Image * in_img, const int label,
                               const ZernikeBasis * basis );

/* math.h */
double cbrt ( double );

#endif


Po załadowaniu "image.h"

[C++ Error] image.h(243): E2184 Enum syntax error
[C++ Error] image.h(247): E2040 Declaration terminated incorrectly
[C++ Error] image.h(247): E2190 Unexpected }
[C++ Error] image.h(247): E2190 Unexpected }
[C++ Error] image.h(247): E2303 Type name expected
[C++ Error] image.h(388): E2184 Enum syntax error
[C++ Error] image.h(402): E2040 Declaration terminated incorrectly
[C++ Error] image.h(402): E2190 Unexpected }
[C++ Error] image.h(402): E2190 Unexpected }
[C++ Error] image.h(402): E2303 Type name expected
[C++ Error] image.h(639): E2293 ) expected
[C++ Error] image.h(645): E2293 ) expected
[C++ Error] image.h(646): E2293 ) expected
[C++ Error] image.h(686): E2108 Improper use of typedef 'FILE'
[C++ Error] image.h(686): E2451 Undefined symbol 'file_ptr'
[C++ Error] image.h(686): E2293 ) expected
[C++ Error] image.h(693): E2293 ) expected
[C++ Error] image.h(696): E2293 ) expected
[C++ Error] image.h(699): E2293 ) expected
[C++ Error] image.h(759): E2238 Multiple declaration for 'Bool'
[C++ Error] image.h(247): E2344 Earlier declaration of 'Bool'
[C++ Error] image.h(759): E2141 Declaration syntax error
[C++ Error] image.h(760): E2293 ) expected
[C++ Error] image.h(761): E2238 Multiple declaration for 'Bool'
[C++ Error] image.h(759): E2344 Earlier declaration of 'Bool'
[C++ Error] image.h(759): E2228 Too many error or warning messages



Czy da to się jakoś dołączyć do projektu tak by działało? Czy będę musiał po kolei wszystko przekopiowywać? Jak przesłać do tych funkcji obraz z TImage1 oraz jak potem wczytać wynik do TImage2 w przypadku funkcji które zwracają takie obiekty.
Jeśli jest taka możliwość to proszę jako odp dać Plik RAR z proj Builder6 lub Builder2010 wraz z ałączonymi plikami. Jeśli da się tak zrobić.
Dzięki.
Nie masz wystarczających uprawnień, aby zobaczyć pliki załączone do tego postu.
Avatar użytkownika
kwgrkwgr
Bladawiec
Bladawiec
 
Posty: 26
Dołączył(a): środa, 13 kwietnia 2011, 16:37
Podziękował : 0
Otrzymał podziękowań: 0
System operacyjny: WinXP Win7
Kompilator: C++ Builder 6;
C++ Builder 2010;
MS Visual 2008 C#;
Gadu Gadu: 9692051
    Windows XPOpera

Re: Przetwarzanie grafiki Image TImage

Nowy postprzez Cyfrowy Baron » środa, 11 maja 2011, 17:57

kwgrkwgr napisał(a):W sieci znalazłem archiwum gdzie jest już zaimplementowanych dużo funkcji przetwarzających Image.


Właśnie ! Image, a nie TImage. To zupełnie różne biblioteki.

kwgrkwgr napisał(a):Co zrobić by to adaptować do Builder 6 lub Builder 2010 ?


Napisać od nowa dla biblioteki TImage, ale TImage nie operuje na grafice, a tylko ją wyświetla. TImage operuje na TCanvas i Graphics::TBitmap.
Avatar użytkownika
Cyfrowy Baron
Administrator
Administrator
 
Posty: 4716
Dołączył(a): niedziela, 13 lipca 2008, 15:17
Podziękował : 12
Otrzymał podziękowań: 442
System operacyjny: Windows 7 x64 SP1
Kompilator: Embarcadero RAD Studio XE2
C++ Builder XE2 Update 4
SKYPE: cyfbar
Gadu Gadu: 0
    Windows XPFirefox


  • Podobne tematy
    Odpowiedzi
    Wyświetlone
    Ostatni post

Powrót do Aplikacje multimedialne, graficzne

Kto przegląda forum

Użytkownicy przeglądający ten dział: Brak zalogowanych użytkowników i 14 gości

cron