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 cvKMeans2( const CvArr* samples_arr, int cluster_count,
46 CvArr* labels_arr, CvTermCriteria termcrit )
49 CvMat* old_centers = 0;
52 CV_FUNCNAME( "cvKMeans2" );
56 CvMat samples_stub, labels_stub;
57 CvMat* samples = (CvMat*)samples_arr;
58 CvMat* labels = (CvMat*)labels_arr;
60 CvRNG rng = CvRNG(-1);
61 int i, j, k, sample_count, dims;
65 if( !CV_IS_MAT( samples ))
66 CV_CALL( samples = cvGetMat( samples, &samples_stub ));
68 if( !CV_IS_MAT( labels ))
69 CV_CALL( labels = cvGetMat( labels, &labels_stub ));
71 if( cluster_count < 1 )
72 CV_ERROR( CV_StsOutOfRange, "Number of clusters should be positive" );
74 if( CV_MAT_DEPTH(samples->type) != CV_32F || CV_MAT_TYPE(labels->type) != CV_32SC1 )
75 CV_ERROR( CV_StsUnsupportedFormat,
76 "samples should be floating-point matrix, cluster_idx - integer vector" );
78 if( labels->rows != 1 && (labels->cols != 1 || !CV_IS_MAT_CONT(labels->type)) ||
79 labels->rows + labels->cols - 1 != samples->rows )
80 CV_ERROR( CV_StsUnmatchedSizes,
81 "cluster_idx should be 1D vector of the same number of elements as samples' number of rows" );
83 CV_CALL( termcrit = cvCheckTermCriteria( termcrit, 1e-6, 100 ));
85 termcrit.epsilon *= termcrit.epsilon;
86 sample_count = samples->rows;
88 if( cluster_count > sample_count )
89 cluster_count = sample_count;
91 dims = samples->cols*CV_MAT_CN(samples->type);
92 ids_delta = labels->step ? labels->step/(int)sizeof(int) : 1;
94 CV_CALL( centers = cvCreateMat( cluster_count, dims, CV_64FC1 ));
95 CV_CALL( old_centers = cvCreateMat( cluster_count, dims, CV_64FC1 ));
96 CV_CALL( counters = cvCreateMat( 1, cluster_count, CV_32SC1 ));
99 for( i = 0; i < sample_count; i++ )
100 labels->data.i[i] = cvRandInt(&rng) % cluster_count;
102 counters->cols = cluster_count; // cut down counters
103 max_dist = termcrit.epsilon*2;
105 for( iter = 0; iter < termcrit.max_iter; iter++ )
111 for( i = 0; i < sample_count; i++ )
113 float* s = (float*)(samples->data.ptr + i*samples->step);
114 k = labels->data.i[i*ids_delta];
115 double* c = (double*)(centers->data.ptr + k*centers->step);
116 for( j = 0; j <= dims - 4; j += 4 )
118 double t0 = c[j] + s[j];
119 double t1 = c[j+1] + s[j+1];
124 t0 = c[j+2] + s[j+2];
125 t1 = c[j+3] + s[j+3];
130 for( ; j < dims; j++ )
132 counters->data.i[k]++;
138 for( k = 0; k < cluster_count; k++ )
140 double* c = (double*)(centers->data.ptr + k*centers->step);
141 if( counters->data.i[k] != 0 )
143 double scale = 1./counters->data.i[k];
144 for( j = 0; j < dims; j++ )
149 i = cvRandInt( &rng ) % sample_count;
150 float* s = (float*)(samples->data.ptr + i*samples->step);
151 for( j = 0; j < dims; j++ )
158 double* c_o = (double*)(old_centers->data.ptr + k*old_centers->step);
159 for( j = 0; j < dims; j++ )
161 double t = c[j] - c_o[j];
164 if( max_dist < dist )
170 for( i = 0; i < sample_count; i++ )
172 float* s = (float*)(samples->data.ptr + i*samples->step);
174 double min_dist = DBL_MAX;
176 for( k = 0; k < cluster_count; k++ )
178 double* c = (double*)(centers->data.ptr + k*centers->step);
182 for( ; j <= dims - 4; j += 4 )
184 double t0 = c[j] - s[j];
185 double t1 = c[j+1] - s[j+1];
186 dist += t0*t0 + t1*t1;
187 t0 = c[j+2] - s[j+2];
188 t1 = c[j+3] - s[j+3];
189 dist += t0*t0 + t1*t1;
192 for( ; j < dims; j++ )
194 double t = c[j] - s[j];
198 if( min_dist > dist )
205 labels->data.i[i*ids_delta] = k_best;
208 if( max_dist < termcrit.epsilon )
211 CV_SWAP( centers, old_centers, temp );
215 for( i = 0; i < sample_count; i++ )
216 counters->data.i[labels->data.i[i]]++;
218 // ensure that we do not have empty clusters
219 for( k = 0; k < cluster_count; k++ )
220 if( counters->data.i[k] == 0 )
223 i = cvRandInt(&rng) % sample_count;
224 j = labels->data.i[i];
225 if( counters->data.i[j] > 1 )
227 labels->data.i[i] = k;
228 counters->data.i[j]--;
229 counters->data.i[k]++;
236 cvReleaseMat( ¢ers );
237 cvReleaseMat( &old_centers );
238 cvReleaseMat( &counters );
243 Finds real roots of cubic, quadratic or linear equation.
244 The original code has been taken from Ken Turkowski web page
245 (http://www.worldserver.com/turk/opensource/) and adopted for OpenCV.
246 Here is the copyright notice.
248 -----------------------------------------------------------------------
249 Copyright (C) 1978-1999 Ken Turkowski. <turk@computer.org>
254 Even though I have reviewed this software, I make no warranty
255 or representation, either express or implied, with respect to this
256 software, its quality, accuracy, merchantability, or fitness for a
257 particular purpose. As a result, this software is provided "as is,"
258 and you, its user, are assuming the entire risk as to its quality
261 This code may be used and freely distributed as long as it includes
262 this copyright notice and the above warranty information.
263 -----------------------------------------------------------------------
266 cvSolveCubic( const CvMat* coeffs, CvMat* roots )
270 CV_FUNCNAME( "cvSolveCubic" );
274 double a0 = 1., a1, a2, a3;
275 double x0 = 0., x1 = 0., x2 = 0.;
276 int step = 1, coeff_count;
278 if( !CV_IS_MAT(coeffs) )
279 CV_ERROR( !coeffs ? CV_StsNullPtr : CV_StsBadArg, "Input parameter is not a valid matrix" );
281 if( !CV_IS_MAT(roots) )
282 CV_ERROR( !roots ? CV_StsNullPtr : CV_StsBadArg, "Output parameter is not a valid matrix" );
284 if( CV_MAT_TYPE(coeffs->type) != CV_32FC1 && CV_MAT_TYPE(coeffs->type) != CV_64FC1 ||
285 CV_MAT_TYPE(roots->type) != CV_32FC1 && CV_MAT_TYPE(roots->type) != CV_64FC1 )
286 CV_ERROR( CV_StsUnsupportedFormat,
287 "Both matrices should be floating-point (single or double precision)" );
289 coeff_count = coeffs->rows + coeffs->cols - 1;
291 if( coeffs->rows != 1 && coeffs->cols != 1 || coeff_count != 3 && coeff_count != 4 )
292 CV_ERROR( CV_StsBadSize,
293 "The matrix of coefficients must be 1-dimensional vector of 3 or 4 elements" );
295 if( roots->rows != 1 && roots->cols != 1 ||
296 roots->rows + roots->cols - 1 != 3 )
297 CV_ERROR( CV_StsBadSize,
298 "The matrix of roots must be 1-dimensional vector of 3 elements" );
300 if( CV_MAT_TYPE(coeffs->type) == CV_32FC1 )
302 const float* c = coeffs->data.fl;
303 if( coeffs->rows > 1 )
304 step = coeffs->step/sizeof(c[0]);
305 if( coeff_count == 4 )
306 a0 = c[0], c += step;
313 const double* c = coeffs->data.db;
314 if( coeffs->rows > 1 )
315 step = coeffs->step/sizeof(c[0]);
316 if( coeff_count == 4 )
317 a0 = c[0], c += step;
328 n = a3 == 0 ? -1 : 0;
338 // quadratic equation
339 double d = a2*a2 - 4*a1*a3;
343 double q = (-a2 + (a2 < 0 ? -d : d)) * 0.5;
357 double Q = (a1 * a1 - 3 * a2) * (1./9);
358 double R = (2 * a1 * a1 * a1 - 9 * a1 * a2 + 27 * a3) * (1./54);
359 double Qcubed = Q * Q * Q;
360 double d = Qcubed - R * R;
364 double theta = acos(R / sqrt(Qcubed));
365 double sqrtQ = sqrt(Q);
366 double t0 = -2 * sqrtQ;
367 double t1 = theta * (1./3);
368 double t2 = a1 * (1./3);
369 x0 = t0 * cos(t1) - t2;
370 x1 = t0 * cos(t1 + (2.*CV_PI/3)) - t2;
371 x2 = t0 * cos(t1 + (4.*CV_PI/3)) - t2;
378 e = pow(d + fabs(R), 0.333333333333);
381 x0 = (e + Q / e) - a1 * (1./3);
388 if( CV_MAT_TYPE(roots->type) == CV_32FC1 )
390 float* r = roots->data.fl;
391 if( roots->rows > 1 )
392 step = roots->step/sizeof(r[0]);
395 r[step*2] = (float)x2;
399 double* r = roots->data.db;
400 if( roots->rows > 1 )
401 step = roots->step/sizeof(r[0]);
414 Finds real and complex roots of polynomials of any degree with real
415 coefficients. The original code has been taken from Ken Turkowski's web
416 page (http://www.worldserver.com/turk/opensource/) and adopted for OpenCV.
417 Here is the copyright notice.
419 -----------------------------------------------------------------------
420 Copyright (C) 1981-1999 Ken Turkowski. <turk@computer.org>
425 Even though I have reviewed this software, I make no warranty
426 or representation, either express or implied, with respect to this
427 software, its quality, accuracy, merchantability, or fitness for a
428 particular purpose. As a result, this software is provided "as is,"
429 and you, its user, are assuming the entire risk as to its quality
432 This code may be used and freely distributed as long as it includes
433 this copyright notice and the above warranty information.
437 /*******************************************************************************
438 * FindPolynomialRoots
440 * The Bairstow and Newton correction formulae are used for a simultaneous
441 * linear and quadratic iterated synthetic division. The coefficients of
442 * a polynomial of degree n are given as a[i] (i=0,i,..., n) where a[0] is
443 * the constant term. The coefficients are scaled by dividing them by
444 * their geometric mean. The Bairstow or Newton iteration method will
445 * nearly always converge to the number of figures carried, fig, either to
446 * root values or to their reciprocals. If the simultaneous Newton and
447 * Bairstow iteration fails to converge on root values or their
448 * reciprocals in maxiter iterations, the convergence requirement will be
449 * successively reduced by one decimal figure. This program anticipates
450 * and protects against loss of significance in the quadratic synthetic
451 * division. (Refer to "On Programming the Numerical Solution of
452 * Polynomial Equations," by K. W. Ellenberger, Commun. ACM 3 (Dec. 1960),
453 * 644-647.) The real and imaginary part of each root is stated as u[i]
454 * and v[i], respectively.
456 * ACM algorithm #30 - Numerical Solution of the Polynomial Equation
458 * Missle Division, North American Aviation, Downey, California
459 * Converted to C, modified, optimized, and structured by
461 * CADLINC, Inc., Palo Alto, California
462 *******************************************************************************/
467 static void icvFindPolynomialRoots(const T *a, T *u, int n, int maxiter, int fig) {
470 T h[MAXN + 3], b[MAXN + 3], c[MAXN + 3], d[MAXN + 3], e[MAXN + 3];
472 T K, ps, qs, pt, qt, s, rev, r;
476 // Zero elements with negative indices
477 b[2 + -1] = b[2 + -2] =
478 c[2 + -1] = c[2 + -2] =
479 d[2 + -1] = d[2 + -2] =
480 e[2 + -1] = e[2 + -2] =
481 h[2 + -1] = h[2 + -2] = 0.0;
483 // Copy polynomial coefficients to working storage
484 for (j = n; j >= 0; j--)
485 h[2 + j] = *a++; // Note reversal of coefficients
488 K = pow(10.0, (double)(fig)); // Relative accuracy
490 for (; h[2 + n] == 0.0; n--) { // Look for zero high-order coeff.
499 ps = qs = pt = qt = s = 0.0;
501 K = pow(10.0, (double)(fig));
504 r = -h[2 + 1] / h[2 + 0];
508 for (j = n; j >= 0; j--) // Find geometric mean of coeff's
510 s += log(fabs(h[2 + j]));
511 s = exp(s / (n + 1));
513 for (j = n; j >= 0; j--) // Normalize coeff's by mean
516 if (fabs(h[2 + 1] / h[2 + 0]) < fabs(h[2 + n - 1] / h[2 + n])) {
519 for (j = (n - 1) / 2; j >= 0; j--) {
521 h[2 + j] = h[2 + n - j];
529 if (h[2 + n - 2] == 0.0) {
533 q = h[2 + n] / h[2 + n - 2];
534 p = (h[2 + n - 1] - q * h[2 + n - 3]) / h[2 + n - 2];
541 for (i = maxiter; i > 0; i--) {
543 for (j = 0; j <= n; j++) { // BAIRSTOW
544 b[2 + j] = h[2 + j] - p * b[2 + j - 1] - q * b[2 + j - 2];
545 c[2 + j] = b[2 + j] - p * c[2 + j - 1] - q * c[2 + j - 2];
547 if ((h[2 + n - 1] != 0.0) && (b[2 + n - 1] != 0.0)) {
548 if (fabs(h[2 + n - 1] / b[2 + n - 1]) >= K) {
549 b[2 + n] = h[2 + n] - q * b[2 + n - 2];
553 if (K < fabs(h[2 + n] / b[2 + n]))
557 for (j = 0; j <= n; j++) { // NEWTON
558 d[2 + j] = h[2 + j] + r * d[2 + j - 1]; // Calculate polynomial at r
559 e[2 + j] = d[2 + j] + r * e[2 + j - 1]; // Calculate derivative at r
563 if (K < fabs(h[2 + n] / d[2 + n]))
566 c[2 + n - 1] = -p * c[2 + n - 2] - q * c[2 + n - 3];
567 s = c[2 + n - 2] * c[2 + n - 2] - c[2 + n - 1] * c[2 + n - 3];
572 p += (b[2 + n - 1] * c[2 + n - 2] - b[2 + n] * c[2 + n - 3]) / s;
573 q += (-b[2 + n - 1] * c[2 + n - 1] + b[2 + n] * c[2 + n - 2]) / s;
575 if (e[2 + n - 1] == 0.0)
576 r -= 1.0; // Minimum step
578 r -= d[2 + n] / e[2 + n - 1]; // Newton's iteration
596 for (j = n; j >= 0; j--) { // Polynomial deflation by lin-nomial
597 if ((d[2 + j] != 0.0) && (fabs(h[2 + j] / d[2 + j]) < K))
614 if (0.0 < (q - (p * p / 4.0))) { // Two complex roots
615 s = sqrt(q - (p * p / 4.0));
620 } else { // Two real roots
621 s = sqrt(((p * p / 4.0)) - q);
623 *u++ = qq = -p / 2.0 + s;
625 *u++ = qq = -p / 2.0 - s;
631 for (j = n; j >= 0; j--) { // Polynomial deflation by quadratic
632 if ((b[2 + j] != 0.0) && (fabs(h[2 + j] / b[2 + j]) < K))
642 void cvSolvePoly(const CvMat* a, CvMat *r, int maxiter, int fig) {
643 int m = a->rows * a->cols;
644 int n = r->rows * r->cols;
647 CV_FUNCNAME("cvSolvePoly");
649 if (CV_MAT_TYPE(a->type) != CV_32FC1 &&
650 CV_MAT_TYPE(a->type) != CV_64FC1)
651 CV_ERROR(CV_StsUnsupportedFormat, "coeffs must be either CV_32FC1 or CV_64FC1");
652 if (CV_MAT_TYPE(r->type) != CV_32FC2 &&
653 CV_MAT_TYPE(r->type) != CV_64FC2)
654 CV_ERROR(CV_StsUnsupportedFormat, "roots must be either CV_32FC2 or CV_64FC2");
655 if (CV_MAT_DEPTH(a->type) != CV_MAT_DEPTH(r->type))
656 CV_ERROR(CV_StsUnmatchedFormats, "coeffs and roots must have same depth");
659 CV_ERROR(CV_StsUnmatchedFormats, "must have n + 1 coefficients");
661 switch (CV_MAT_DEPTH(a->type)) {
663 icvFindPolynomialRoots(a->data.fl, r->data.fl, n, maxiter, fig);
666 icvFindPolynomialRoots(a->data.db, r->data.db, n, maxiter, fig);
674 CV_IMPL void cvNormalize( const CvArr* src, CvArr* dst,
675 double a, double b, int norm_type, const CvArr* mask )
679 CV_FUNCNAME( "cvNormalize" );
685 if( norm_type == CV_MINMAX )
687 double smin = 0, smax = 0;
688 double dmin = MIN( a, b ), dmax = MAX( a, b );
689 cvMinMaxLoc( src, &smin, &smax, 0, 0, mask );
690 scale = (dmax - dmin)*(smax - smin > DBL_EPSILON ? 1./(smax - smin) : 0);
691 shift = dmin - smin*scale;
693 else if( norm_type == CV_L2 || norm_type == CV_L1 || norm_type == CV_C )
695 CvMat *s = (CvMat*)src, *d = (CvMat*)dst;
697 if( CV_IS_MAT(s) && CV_IS_MAT(d) && CV_IS_MAT_CONT(s->type & d->type) &&
698 CV_ARE_TYPES_EQ(s,d) && CV_ARE_SIZES_EQ(s,d) && !mask &&
699 s->cols*s->rows <= CV_MAX_INLINE_MAT_OP_SIZE*CV_MAX_INLINE_MAT_OP_SIZE )
701 int i, len = s->cols*s->rows;
704 if( CV_MAT_TYPE(s->type) == CV_32FC1 )
706 const float* sptr = s->data.fl;
707 float* dptr = d->data.fl;
709 if( norm_type == CV_L2 )
711 for( i = 0; i < len; i++ )
718 else if( norm_type == CV_L1 )
719 for( i = 0; i < len; i++ )
721 v = fabs((double)sptr[i]);
725 for( i = 0; i < len; i++ )
727 v = fabs((double)sptr[i]);
731 norm = norm > DBL_EPSILON ? 1./norm : 0.;
732 for( i = 0; i < len; i++ )
733 dptr[i] = (float)(sptr[i]*norm);
737 if( CV_MAT_TYPE(s->type) == CV_64FC1 )
739 const double* sptr = s->data.db;
740 double* dptr = d->data.db;
742 if( norm_type == CV_L2 )
744 for( i = 0; i < len; i++ )
751 else if( norm_type == CV_L1 )
752 for( i = 0; i < len; i++ )
758 for( i = 0; i < len; i++ )
764 norm = norm > DBL_EPSILON ? 1./norm : 0.;
765 for( i = 0; i < len; i++ )
766 dptr[i] = sptr[i]*norm;
771 scale = cvNorm( src, 0, norm_type, mask );
772 scale = scale > DBL_EPSILON ? 1./scale : 0.;
776 CV_ERROR( CV_StsBadArg, "Unknown/unsupported norm type" );
779 cvConvertScale( src, dst, scale, shift );
783 CV_CALL( dmat = cvGetMat(dst, &stub));
784 CV_CALL( tmp = cvCreateMat(dmat->rows, dmat->cols, dmat->type) );
785 cvConvertScale( src, tmp, scale, shift );
786 cvCopy( tmp, dst, mask );
792 cvReleaseMat( &tmp );
796 CV_IMPL void cvRandShuffle( CvArr* arr, CvRNG* rng, double iter_factor )
798 CV_FUNCNAME( "cvRandShuffle" );
802 const int sizeof_int = (int)sizeof(int);
803 CvMat stub, *mat = (CvMat*)arr;
804 int i, j, k, iters, delta = 0;
805 int cont_flag, arr_size, elem_size, cols, step;
806 const int pair_buf_sz = 100;
807 int* pair_buf = (int*)cvStackAlloc( pair_buf_sz*sizeof(pair_buf[0])*2 );
808 CvMat _pair_buf = cvMat( 1, pair_buf_sz*2, CV_32S, pair_buf );
809 CvRNG _rng = cvRNG(-1);
813 if( !CV_IS_MAT(mat) )
814 CV_CALL( mat = cvGetMat( mat, &stub ));
821 arr_size = cols*mat->rows;
822 iters = cvRound(iter_factor*arr_size)*2;
823 cont_flag = CV_IS_MAT_CONT(mat->type);
824 elem_size = CV_ELEM_SIZE(mat->type);
825 if( elem_size % sizeof_int == 0 && (cont_flag || step % sizeof_int == 0) )
829 elem_size /= sizeof_int;
832 data = mat->data.ptr;
834 for( i = 0; i < iters; i += delta )
836 delta = MIN( iters - i, pair_buf_sz*2 );
837 _pair_buf.cols = delta;
838 cvRandArr( rng, &_pair_buf, CV_RAND_UNI, cvRealScalar(0), cvRealScalar(arr_size) );
843 for( j = 0; j < delta; j += 2 )
845 int* p = idata + pair_buf[j]*elem_size, *q = idata + pair_buf[j+1]*elem_size, t;
846 for( k = 0; k < elem_size; k++ )
847 CV_SWAP( p[k], q[k], t );
850 for( j = 0; j < delta; j += 2 )
852 uchar* p = data + pair_buf[j]*elem_size, *q = data + pair_buf[j+1]*elem_size, t;
853 for( k = 0; k < elem_size; k++ )
854 CV_SWAP( p[k], q[k], t );
860 for( j = 0; j < delta; j += 2 )
862 int idx1 = pair_buf[j], idx2 = pair_buf[j+1], row1, row2;
864 row1 = idx1/step; row2 = idx2/step;
865 p = idata + row1*step + (idx1 - row1*cols)*elem_size;
866 q = idata + row2*step + (idx2 - row2*cols)*elem_size;
868 for( k = 0; k < elem_size; k++ )
869 CV_SWAP( p[k], q[k], t );
872 for( j = 0; j < delta; j += 2 )
874 int idx1 = pair_buf[j], idx2 = pair_buf[j+1], row1, row2;
876 row1 = idx1/step; row2 = idx2/step;
877 p = data + row1*step + (idx1 - row1*cols)*elem_size;
878 q = data + row2*step + (idx2 - row2*cols)*elem_size;
880 for( k = 0; k < elem_size; k++ )
881 CV_SWAP( p[k], q[k], t );
891 cvRange( CvArr* arr, double start, double end )
895 CV_FUNCNAME( "cvRange" );
899 CvMat stub, *mat = (CvMat*)arr;
906 if( !CV_IS_MAT(mat) )
907 CV_CALL( mat = cvGetMat( mat, &stub) );
911 type = CV_MAT_TYPE(mat->type);
912 delta = (end-start)/(rows*cols);
914 if( CV_IS_MAT_CONT(mat->type) )
921 step = mat->step / CV_ELEM_SIZE(type);
923 if( type == CV_32SC1 )
925 int* idata = mat->data.i;
926 int ival = cvRound(val), idelta = cvRound(delta);
928 if( fabs(val - ival) < DBL_EPSILON &&
929 fabs(delta - idelta) < DBL_EPSILON )
931 for( i = 0; i < rows; i++, idata += step )
932 for( j = 0; j < cols; j++, ival += idelta )
937 for( i = 0; i < rows; i++, idata += step )
938 for( j = 0; j < cols; j++, val += delta )
939 idata[j] = cvRound(val);
942 else if( type == CV_32FC1 )
944 float* fdata = mat->data.fl;
945 for( i = 0; i < rows; i++, fdata += step )
946 for( j = 0; j < cols; j++, val += delta )
947 fdata[j] = (float)val;
950 CV_ERROR( CV_StsUnsupportedFormat, "The function only supports 32sC1 and 32fC1 datatypes" );