1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
45 IPCVAPI(CvStatus, icvCalcContrastHist8uC1R, ( uchar** img, int step, CvSize size,
46 CvHistogram* hist, int dont_clear ))
48 IPCVAPI(CvStatus, icvCalcContrastHistMask8uC1R, ( uchar** img, int step,
49 uchar* mask, int mask_step, CvSize size,
50 CvHistogram* hist, int dont_clear ))
52 /*F///////////////////////////////////////////////////////////////////////////////////////
53 // Name: icvCalcContrastHist8uC1R
54 // Purpose: Calculating the histogram of contrast from one-channel images
58 // Notes: if dont_clear parameter is NULL then histogram clearing before
59 // calculating (all values sets to NULL)
61 static CvStatus CV_STDCALL
62 icvCalcContrastHist8uC1R( uchar** img, int step, CvSize size,
63 CvHistogram* hist, int dont_clear )
65 int i, j, t, x = 0, y = 0;
69 return CV_NULLPTR_ERR;
73 return CV_BADSIZE_ERR;
75 if( hist->type != CV_HIST_ARRAY )
76 return CV_BADFLAG_ERR;
78 for( i = 0; i < dims; i++ )
80 return CV_NULLPTR_ERR;
82 for( i = 0; i < hist->c_dims; i++ )
84 if( !hist->thresh[i] )
85 return CV_NULLPTR_ERR;
86 assert( hist->chdims[i] );
89 j = hist->dims[0] * hist->mdims[0];
91 int *n = (int *)cvAlloc( (size_t)hist->dims[0] * sizeof( int ));
93 if( hist->type == CV_HIST_ARRAY )
96 for( i = 0; i < j; i++ )
102 switch (hist->c_dims)
106 uchar *data0 = img[0];
107 int *array = (int *) hist->array;
108 int *chdims = hist->chdims[0];
110 for( i = 0; i < j; i++ )
111 array[i] = cvRound( hist->array[i] );
113 for( y = 0; y < size.height; y++, data0 += step )
115 for( x = 0; x <= size.width - 1; x += 2 )
117 int v1_r = MIN( data0[x], data0[x + 1] );
118 int v2_r = MAX( data0[x], data0[x + 1] );
120 // calculate contrast for the right-left pair
121 for( t = v1_r; t < v2_r; t++ )
123 int val0 = chdims[t + 128];
125 array[val0] += MIN( t - v1_r, v2_r - t );
129 if( y < size.height - 1 )
131 int v1_d = MIN( data0[x], data0[x + step] );
132 int v2_d = MAX( data0[x], data0[x + step] );
134 // calculate contrast for the top-down pair
135 for( t = v1_d; t < v2_d; t++ )
137 int val0 = chdims[t + 128];
139 array[val0] += MIN( t - v1_d, v2_d - t );
146 // convert int to float
147 for( i = 0; i < j; i++ )
150 hist->array[i] = (float) array[i] / n[i];
157 return CV_BADSIZE_ERR;
165 /*F///////////////////////////////////////////////////////////////////////////////////////
166 // Name: icvCalcContrastHistMask8uC1R
167 // Purpose: Calculating the mask histogram of contrast from one-channel images
171 // Notes: if dont_clear parameter is NULL then histogram clearing before
172 // calculating (all values sets to NULL)
174 static CvStatus CV_STDCALL
175 icvCalcContrastHistMask8uC1R( uchar** img, int step, uchar* mask, int mask_step,
176 CvSize size, CvHistogram * hist, int dont_clear )
178 int i, j, t, x = 0, y = 0;
182 if( !hist || !img || !mask )
183 return CV_NULLPTR_ERR;
187 return CV_BADSIZE_ERR;
189 if( hist->type != CV_HIST_ARRAY )
190 return CV_BADFLAG_ERR;
192 for( i = 0; i < dims; i++ )
194 return CV_NULLPTR_ERR;
196 for( i = 0; i < hist->c_dims; i++ )
198 if( !hist->thresh[i] )
199 return CV_NULLPTR_ERR;
200 assert( hist->chdims[i] );
203 j = hist->dims[0] * hist->mdims[0];
205 int *n = (int *)cvAlloc( (size_t) hist->dims[0] * sizeof( int ));
207 if( hist->type == CV_HIST_ARRAY )
210 for( i = 0; i < j; i++ )
216 switch (hist->c_dims)
220 uchar *data0 = img[0];
222 int *array = (int *) hist->array;
223 int *chdims = hist->chdims[0];
225 for( i = 0; i < j; i++ )
226 array[i] = cvRound( hist->array[i] );
228 for( y = 0; y < size.height; y++, data0 += step, maskp += mask_step )
230 for( x = 0; x <= size.width - 2; x++ )
236 int v1_r = MIN( data0[x], data0[x + 1] );
237 int v2_r = MAX( data0[x], data0[x + 1] );
240 // calculate contrast for the right-left pair
241 for( t = v1_r; t < v2_r; t++ )
243 int val0 = chdims[t + 128];
245 array[val0] += MIN( t - v1_r, v2_r - t );
251 if( y < size.height - 1 )
253 if( maskp[x + mask_step] )
255 int v1_d = MIN( data0[x], data0[x + step] );
256 int v2_d = MAX( data0[x], data0[x + step] );
258 // calculate contrast for the top-down pair
259 for( t = v1_d; t < v2_d; t++ )
261 int val0 = chdims[t + 128];
263 array[val0] += MIN( t - v1_d, v2_d - t );
273 // convert int to float
274 for( i = 0; i < j; i++ )
277 hist->array[i] = (float) array[i] / n[i];
284 return CV_BADSIZE_ERR;
293 CV_IMPL void cvCalcContrastHist( IplImage** img, CvHistogram* hist, int dont_clear )
295 CV_FUNCNAME( "cvCalcContrastHist" );
296 uchar* data[CV_HIST_MAX_DIM];
302 {for( int i = 0; i < hist->c_dims; i++ )
303 CV_CALL( CV_CHECK_IMAGE( img[i] ) );}
305 {for( int i = 0; i < hist->c_dims; i++ )
306 cvGetImageRawData( img[i], &data[i], &step, &roi );}
308 if(img[0]->nChannels != 1)
309 CV_ERROR( IPL_BadNumChannels, "bad channels numbers" );
311 if(img[0]->depth != IPL_DEPTH_8U)
312 CV_ERROR( IPL_BadDepth, "bad image depth" );
314 switch(img[0]->depth)
317 IPPI_CALL( icvCalcContrastHist8uC1R( data, step, roi, hist, dont_clear ) );
319 default: CV_ERROR( IPL_BadDepth, "bad image depth" );
328 cvCalcContrastHist( IplImage ** img, CvHistogram * hist, int dont_clear, IplImage * mask )
330 CV_FUNCNAME( "cvCalcContrastHist" );
331 uchar *data[CV_HIST_MAX_DIM];
332 uchar *mask_data = 0;
335 CvSize roi = { 0, 0 };
340 for( int i = 0; i < hist->c_dims; i++ )
341 CV_CALL( CV_CHECK_IMAGE( img[i] ));
345 CV_CALL( CV_CHECK_IMAGE( mask ));
346 if( mask->depth != IPL_DEPTH_8U )
347 CV_ERROR( CV_BadDepth, "bad mask depth" );
348 cvGetImageRawData( mask, &mask_data, &mask_step, 0 );
353 for( int i = 0; i < hist->c_dims; i++ )
354 cvGetImageRawData( img[i], &data[i], &step, &roi );
357 if( img[0]->nChannels != 1 )
358 CV_ERROR( CV_BadNumChannels, "bad channels numbers" );
360 if( img[0]->depth != IPL_DEPTH_8U )
361 CV_ERROR( CV_BadDepth, "bad image depth" );
364 switch (img[0]->depth)
369 IPPI_CALL( icvCalcContrastHist8uC1R( data, step, roi, hist, dont_clear ));
373 IPPI_CALL( icvCalcContrastHistMask8uC1R( data, step, mask_data,
374 mask_step, roi, hist, dont_clear ));
378 CV_ERROR( CV_BadDepth, "bad image depth" );