61fff298522fcf5a5bdb27a37bbdce2a43188d24
[profile/ivi/opencv.git] / modules / imgproc / src / moments.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of Intel Corporation may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41 #include "precomp.hpp"
42 #include "opencl_kernels.hpp"
43
44 namespace cv
45 {
46
47 // The function calculates center of gravity and the central second order moments
48 static void completeMomentState( Moments* moments )
49 {
50     double cx = 0, cy = 0;
51     double mu20, mu11, mu02;
52     double inv_m00 = 0.0;
53     assert( moments != 0 );
54
55     if( fabs(moments->m00) > DBL_EPSILON )
56     {
57         inv_m00 = 1. / moments->m00;
58         cx = moments->m10 * inv_m00;
59         cy = moments->m01 * inv_m00;
60     }
61
62     // mu20 = m20 - m10*cx
63     mu20 = moments->m20 - moments->m10 * cx;
64     // mu11 = m11 - m10*cy
65     mu11 = moments->m11 - moments->m10 * cy;
66     // mu02 = m02 - m01*cy
67     mu02 = moments->m02 - moments->m01 * cy;
68
69     moments->mu20 = mu20;
70     moments->mu11 = mu11;
71     moments->mu02 = mu02;
72
73     // mu30 = m30 - cx*(3*mu20 + cx*m10)
74     moments->mu30 = moments->m30 - cx * (3 * mu20 + cx * moments->m10);
75     mu11 += mu11;
76     // mu21 = m21 - cx*(2*mu11 + cx*m01) - cy*mu20
77     moments->mu21 = moments->m21 - cx * (mu11 + cx * moments->m01) - cy * mu20;
78     // mu12 = m12 - cy*(2*mu11 + cy*m10) - cx*mu02
79     moments->mu12 = moments->m12 - cy * (mu11 + cy * moments->m10) - cx * mu02;
80     // mu03 = m03 - cy*(3*mu02 + cy*m01)
81     moments->mu03 = moments->m03 - cy * (3 * mu02 + cy * moments->m01);
82
83
84     double inv_sqrt_m00 = std::sqrt(std::abs(inv_m00));
85     double s2 = inv_m00*inv_m00, s3 = s2*inv_sqrt_m00;
86
87     moments->nu20 = moments->mu20*s2; moments->nu11 = moments->mu11*s2; moments->nu02 = moments->mu02*s2;
88     moments->nu30 = moments->mu30*s3; moments->nu21 = moments->mu21*s3; moments->nu12 = moments->mu12*s3; moments->nu03 = moments->mu03*s3;
89
90 }
91
92
93 static Moments contourMoments( const Mat& contour )
94 {
95     Moments m;
96     int lpt = contour.checkVector(2);
97     int is_float = contour.depth() == CV_32F;
98     const Point* ptsi = (const Point*)contour.data;
99     const Point2f* ptsf = (const Point2f*)contour.data;
100
101     CV_Assert( contour.depth() == CV_32S || contour.depth() == CV_32F );
102
103     if( lpt == 0 )
104         return m;
105
106     double a00 = 0, a10 = 0, a01 = 0, a20 = 0, a11 = 0, a02 = 0, a30 = 0, a21 = 0, a12 = 0, a03 = 0;
107     double xi, yi, xi2, yi2, xi_1, yi_1, xi_12, yi_12, dxy, xii_1, yii_1;
108
109     if( !is_float )
110     {
111         xi_1 = ptsi[lpt-1].x;
112         yi_1 = ptsi[lpt-1].y;
113     }
114     else
115     {
116         xi_1 = ptsf[lpt-1].x;
117         yi_1 = ptsf[lpt-1].y;
118     }
119
120     xi_12 = xi_1 * xi_1;
121     yi_12 = yi_1 * yi_1;
122
123     for( int i = 0; i < lpt; i++ )
124     {
125         if( !is_float )
126         {
127             xi = ptsi[i].x;
128             yi = ptsi[i].y;
129         }
130         else
131         {
132             xi = ptsf[i].x;
133             yi = ptsf[i].y;
134         }
135
136         xi2 = xi * xi;
137         yi2 = yi * yi;
138         dxy = xi_1 * yi - xi * yi_1;
139         xii_1 = xi_1 + xi;
140         yii_1 = yi_1 + yi;
141
142         a00 += dxy;
143         a10 += dxy * xii_1;
144         a01 += dxy * yii_1;
145         a20 += dxy * (xi_1 * xii_1 + xi2);
146         a11 += dxy * (xi_1 * (yii_1 + yi_1) + xi * (yii_1 + yi));
147         a02 += dxy * (yi_1 * yii_1 + yi2);
148         a30 += dxy * xii_1 * (xi_12 + xi2);
149         a03 += dxy * yii_1 * (yi_12 + yi2);
150         a21 += dxy * (xi_12 * (3 * yi_1 + yi) + 2 * xi * xi_1 * yii_1 +
151                    xi2 * (yi_1 + 3 * yi));
152         a12 += dxy * (yi_12 * (3 * xi_1 + xi) + 2 * yi * yi_1 * xii_1 +
153                    yi2 * (xi_1 + 3 * xi));
154         xi_1 = xi;
155         yi_1 = yi;
156         xi_12 = xi2;
157         yi_12 = yi2;
158     }
159
160     if( fabs(a00) > FLT_EPSILON )
161     {
162         double db1_2, db1_6, db1_12, db1_24, db1_20, db1_60;
163
164         if( a00 > 0 )
165         {
166             db1_2 = 0.5;
167             db1_6 = 0.16666666666666666666666666666667;
168             db1_12 = 0.083333333333333333333333333333333;
169             db1_24 = 0.041666666666666666666666666666667;
170             db1_20 = 0.05;
171             db1_60 = 0.016666666666666666666666666666667;
172         }
173         else
174         {
175             db1_2 = -0.5;
176             db1_6 = -0.16666666666666666666666666666667;
177             db1_12 = -0.083333333333333333333333333333333;
178             db1_24 = -0.041666666666666666666666666666667;
179             db1_20 = -0.05;
180             db1_60 = -0.016666666666666666666666666666667;
181         }
182
183         // spatial moments
184         m.m00 = a00 * db1_2;
185         m.m10 = a10 * db1_6;
186         m.m01 = a01 * db1_6;
187         m.m20 = a20 * db1_12;
188         m.m11 = a11 * db1_24;
189         m.m02 = a02 * db1_12;
190         m.m30 = a30 * db1_20;
191         m.m21 = a21 * db1_60;
192         m.m12 = a12 * db1_60;
193         m.m03 = a03 * db1_20;
194
195         completeMomentState( &m );
196     }
197     return m;
198 }
199
200
201 /****************************************************************************************\
202 *                                Spatial Raster Moments                                  *
203 \****************************************************************************************/
204
205 template<typename T, typename WT, typename MT>
206 #if defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ >= 5 && __GNUC_MINOR__ < 9
207 // Workaround for http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60196
208 __attribute__((optimize("no-tree-vectorize")))
209 #endif
210 static void momentsInTile( const Mat& img, double* moments )
211 {
212     Size size = img.size();
213     int x, y;
214     MT mom[10] = {0,0,0,0,0,0,0,0,0,0};
215
216     for( y = 0; y < size.height; y++ )
217     {
218         const T* ptr = (const T*)(img.data + y*img.step);
219         WT x0 = 0, x1 = 0, x2 = 0;
220         MT x3 = 0;
221
222         for( x = 0; x < size.width; x++ )
223         {
224             WT p = ptr[x];
225             WT xp = x * p, xxp;
226
227             x0 += p;
228             x1 += xp;
229             xxp = xp * x;
230             x2 += xxp;
231             x3 += xxp * x;
232         }
233
234         WT py = y * x0, sy = y*y;
235
236         mom[9] += ((MT)py) * sy;  // m03
237         mom[8] += ((MT)x1) * sy;  // m12
238         mom[7] += ((MT)x2) * y;  // m21
239         mom[6] += x3;             // m30
240         mom[5] += x0 * sy;        // m02
241         mom[4] += x1 * y;         // m11
242         mom[3] += x2;             // m20
243         mom[2] += py;             // m01
244         mom[1] += x1;             // m10
245         mom[0] += x0;             // m00
246     }
247
248     for( x = 0; x < 10; x++ )
249         moments[x] = (double)mom[x];
250 }
251
252
253 #if CV_SSE2
254
255 template<> void momentsInTile<uchar, int, int>( const cv::Mat& img, double* moments )
256 {
257     typedef uchar T;
258     typedef int WT;
259     typedef int MT;
260     Size size = img.size();
261     int y;
262     MT mom[10] = {0,0,0,0,0,0,0,0,0,0};
263     bool useSIMD = checkHardwareSupport(CV_CPU_SSE2);
264
265     for( y = 0; y < size.height; y++ )
266     {
267         const T* ptr = img.ptr<T>(y);
268         int x0 = 0, x1 = 0, x2 = 0, x3 = 0, x = 0;
269
270         if( useSIMD )
271         {
272             __m128i qx_init = _mm_setr_epi16(0, 1, 2, 3, 4, 5, 6, 7);
273             __m128i dx = _mm_set1_epi16(8);
274             __m128i z = _mm_setzero_si128(), qx0 = z, qx1 = z, qx2 = z, qx3 = z, qx = qx_init;
275
276             for( ; x <= size.width - 8; x += 8 )
277             {
278                 __m128i p = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i*)(ptr + x)), z);
279                 qx0 = _mm_add_epi32(qx0, _mm_sad_epu8(p, z));
280                 __m128i px = _mm_mullo_epi16(p, qx);
281                 __m128i sx = _mm_mullo_epi16(qx, qx);
282                 qx1 = _mm_add_epi32(qx1, _mm_madd_epi16(p, qx));
283                 qx2 = _mm_add_epi32(qx2, _mm_madd_epi16(p, sx));
284                 qx3 = _mm_add_epi32(qx3, _mm_madd_epi16(px, sx));
285
286                 qx = _mm_add_epi16(qx, dx);
287             }
288             int CV_DECL_ALIGNED(16) buf[4];
289             _mm_store_si128((__m128i*)buf, qx0);
290             x0 = buf[0] + buf[1] + buf[2] + buf[3];
291             _mm_store_si128((__m128i*)buf, qx1);
292             x1 = buf[0] + buf[1] + buf[2] + buf[3];
293             _mm_store_si128((__m128i*)buf, qx2);
294             x2 = buf[0] + buf[1] + buf[2] + buf[3];
295             _mm_store_si128((__m128i*)buf, qx3);
296             x3 = buf[0] + buf[1] + buf[2] + buf[3];
297         }
298
299         for( ; x < size.width; x++ )
300         {
301             WT p = ptr[x];
302             WT xp = x * p, xxp;
303
304             x0 += p;
305             x1 += xp;
306             xxp = xp * x;
307             x2 += xxp;
308             x3 += xxp * x;
309         }
310
311         WT py = y * x0, sy = y*y;
312
313         mom[9] += ((MT)py) * sy;  // m03
314         mom[8] += ((MT)x1) * sy;  // m12
315         mom[7] += ((MT)x2) * y;  // m21
316         mom[6] += x3;             // m30
317         mom[5] += x0 * sy;        // m02
318         mom[4] += x1 * y;         // m11
319         mom[3] += x2;             // m20
320         mom[2] += py;             // m01
321         mom[1] += x1;             // m10
322         mom[0] += x0;             // m00
323     }
324
325     for(int x = 0; x < 10; x++ )
326         moments[x] = (double)mom[x];
327 }
328
329 #endif
330
331 typedef void (*MomentsInTileFunc)(const Mat& img, double* moments);
332
333 Moments::Moments()
334 {
335     m00 = m10 = m01 = m20 = m11 = m02 = m30 = m21 = m12 = m03 =
336     mu20 = mu11 = mu02 = mu30 = mu21 = mu12 = mu03 =
337     nu20 = nu11 = nu02 = nu30 = nu21 = nu12 = nu03 = 0.;
338 }
339
340 Moments::Moments( double _m00, double _m10, double _m01, double _m20, double _m11,
341                   double _m02, double _m30, double _m21, double _m12, double _m03 )
342 {
343     m00 = _m00; m10 = _m10; m01 = _m01;
344     m20 = _m20; m11 = _m11; m02 = _m02;
345     m30 = _m30; m21 = _m21; m12 = _m12; m03 = _m03;
346
347     double cx = 0, cy = 0, inv_m00 = 0;
348     if( std::abs(m00) > DBL_EPSILON )
349     {
350         inv_m00 = 1./m00;
351         cx = m10*inv_m00; cy = m01*inv_m00;
352     }
353
354     mu20 = m20 - m10*cx;
355     mu11 = m11 - m10*cy;
356     mu02 = m02 - m01*cy;
357
358     mu30 = m30 - cx*(3*mu20 + cx*m10);
359     mu21 = m21 - cx*(2*mu11 + cx*m01) - cy*mu20;
360     mu12 = m12 - cy*(2*mu11 + cy*m10) - cx*mu02;
361     mu03 = m03 - cy*(3*mu02 + cy*m01);
362
363     double inv_sqrt_m00 = std::sqrt(std::abs(inv_m00));
364     double s2 = inv_m00*inv_m00, s3 = s2*inv_sqrt_m00;
365
366     nu20 = mu20*s2; nu11 = mu11*s2; nu02 = mu02*s2;
367     nu30 = mu30*s3; nu21 = mu21*s3; nu12 = mu12*s3; nu03 = mu03*s3;
368 }
369
370 #ifdef HAVE_OPENCL
371
372 static bool ocl_moments( InputArray _src, Moments& m, bool binary)
373 {
374     const int TILE_SIZE = 32;
375     const int K = 10;
376
377     ocl::Kernel k = ocl::Kernel("moments", ocl::imgproc::moments_oclsrc,
378         format("-D TILE_SIZE=%d%s",
379         TILE_SIZE,
380         binary ? " -D OP_MOMENTS_BINARY" : ""));
381
382     if( k.empty() )
383         return false;
384
385     UMat src = _src.getUMat();
386     Size sz = src.size();
387     int xtiles = (sz.width + TILE_SIZE-1)/TILE_SIZE;
388     int ytiles = (sz.height + TILE_SIZE-1)/TILE_SIZE;
389     int ntiles = xtiles*ytiles;
390     UMat umbuf(1, ntiles*K, CV_32S);
391
392     size_t globalsize[] = {xtiles, sz.height}, localsize[] = {1, TILE_SIZE};
393     bool ok = k.args(ocl::KernelArg::ReadOnly(src),
394                      ocl::KernelArg::PtrWriteOnly(umbuf),
395                      xtiles).run(2, globalsize, localsize, true);
396     if(!ok)
397         return false;
398     Mat mbuf = umbuf.getMat(ACCESS_READ);
399     for( int i = 0; i < ntiles; i++ )
400     {
401         double x = (i % xtiles)*TILE_SIZE, y = (i / xtiles)*TILE_SIZE;
402         const int* mom = mbuf.ptr<int>() + i*K;
403         double xm = x * mom[0], ym = y * mom[0];
404
405         // accumulate moments computed in each tile
406
407         // + m00 ( = m00' )
408         m.m00 += mom[0];
409
410         // + m10 ( = m10' + x*m00' )
411         m.m10 += mom[1] + xm;
412
413         // + m01 ( = m01' + y*m00' )
414         m.m01 += mom[2] + ym;
415
416         // + m20 ( = m20' + 2*x*m10' + x*x*m00' )
417         m.m20 += mom[3] + x * (mom[1] * 2 + xm);
418
419         // + m11 ( = m11' + x*m01' + y*m10' + x*y*m00' )
420         m.m11 += mom[4] + x * (mom[2] + ym) + y * mom[1];
421
422         // + m02 ( = m02' + 2*y*m01' + y*y*m00' )
423         m.m02 += mom[5] + y * (mom[2] * 2 + ym);
424
425         // + m30 ( = m30' + 3*x*m20' + 3*x*x*m10' + x*x*x*m00' )
426         m.m30 += mom[6] + x * (3. * mom[3] + x * (3. * mom[1] + xm));
427
428         // + m21 ( = m21' + x*(2*m11' + 2*y*m10' + x*m01' + x*y*m00') + y*m20')
429         m.m21 += mom[7] + x * (2 * (mom[4] + y * mom[1]) + x * (mom[2] + ym)) + y * mom[3];
430
431         // + m12 ( = m12' + y*(2*m11' + 2*x*m01' + y*m10' + x*y*m00') + x*m02')
432         m.m12 += mom[8] + y * (2 * (mom[4] + x * mom[2]) + y * (mom[1] + xm)) + x * mom[5];
433
434         // + m03 ( = m03' + 3*y*m02' + 3*y*y*m01' + y*y*y*m00' )
435         m.m03 += mom[9] + y * (3. * mom[5] + y * (3. * mom[2] + ym));
436     }
437
438     return true;
439 }
440
441 #endif
442
443 }
444
445
446 cv::Moments cv::moments( InputArray _src, bool binary )
447 {
448     const int TILE_SIZE = 32;
449     MomentsInTileFunc func = 0;
450     uchar nzbuf[TILE_SIZE*TILE_SIZE];
451     Moments m;
452     int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
453     Size size = _src.size();
454
455     if( size.width <= 0 || size.height <= 0 )
456         return m;
457
458 #ifdef HAVE_OPENCL
459     if( !(ocl::useOpenCL() && type == CV_8UC1  &&
460         _src.isUMat() && ocl_moments(_src, m, binary)) )
461 #endif
462     {
463         Mat mat = _src.getMat();
464         if( mat.checkVector(2) >= 0 && (depth == CV_32F || depth == CV_32S))
465             return contourMoments(mat);
466
467         if( cn > 1 )
468             CV_Error( CV_StsBadArg, "Invalid image type (must be single-channel)" );
469
470 #if IPP_VERSION_X100 >= 801 && 0
471         if (!binary)
472         {
473             IppiSize roi = { mat.cols, mat.rows };
474             IppiMomentState_64f * moment = NULL;
475             // ippiMomentInitAlloc_64f, ippiMomentFree_64f are deprecated in 8.1, but there are not another way
476             // to initialize IppiMomentState_64f. When GetStateSize and Init functions will appear we have to
477             // change our code.
478             CV_SUPPRESS_DEPRECATED_START
479             if (ippiMomentInitAlloc_64f(&moment, ippAlgHintAccurate) >= 0)
480             {
481                 typedef IppStatus (CV_STDCALL * ippiMoments)(const void * pSrc, int srcStep, IppiSize roiSize, IppiMomentState_64f* pCtx);
482                 ippiMoments ippFunc =
483                     type == CV_8UC1 ? (ippiMoments)ippiMoments64f_8u_C1R :
484                     type == CV_16UC1 ? (ippiMoments)ippiMoments64f_16u_C1R :
485                     type == CV_32FC1? (ippiMoments)ippiMoments64f_32f_C1R : 0;
486
487                 if (ippFunc)
488                 {
489                     if (ippFunc(mat.data, (int)mat.step, roi, moment) >= 0)
490                     {
491                         IppiPoint point = { 0, 0 };
492                         ippiGetSpatialMoment_64f(moment, 0, 0, 0, point, &m.m00);
493                         ippiGetSpatialMoment_64f(moment, 1, 0, 0, point, &m.m10);
494                         ippiGetSpatialMoment_64f(moment, 0, 1, 0, point, &m.m01);
495
496                         ippiGetSpatialMoment_64f(moment, 2, 0, 0, point, &m.m20);
497                         ippiGetSpatialMoment_64f(moment, 1, 1, 0, point, &m.m11);
498                         ippiGetSpatialMoment_64f(moment, 0, 2, 0, point, &m.m02);
499
500                         ippiGetSpatialMoment_64f(moment, 3, 0, 0, point, &m.m30);
501                         ippiGetSpatialMoment_64f(moment, 2, 1, 0, point, &m.m21);
502                         ippiGetSpatialMoment_64f(moment, 1, 2, 0, point, &m.m12);
503                         ippiGetSpatialMoment_64f(moment, 0, 3, 0, point, &m.m03);
504                         ippiGetCentralMoment_64f(moment, 2, 0, 0, &m.mu20);
505                         ippiGetCentralMoment_64f(moment, 1, 1, 0, &m.mu11);
506                         ippiGetCentralMoment_64f(moment, 0, 2, 0, &m.mu02);
507                         ippiGetCentralMoment_64f(moment, 3, 0, 0, &m.mu30);
508                         ippiGetCentralMoment_64f(moment, 2, 1, 0, &m.mu21);
509                         ippiGetCentralMoment_64f(moment, 1, 2, 0, &m.mu12);
510                         ippiGetCentralMoment_64f(moment, 0, 3, 0, &m.mu03);
511                         ippiGetNormalizedCentralMoment_64f(moment, 2, 0, 0, &m.nu20);
512                         ippiGetNormalizedCentralMoment_64f(moment, 1, 1, 0, &m.nu11);
513                         ippiGetNormalizedCentralMoment_64f(moment, 0, 2, 0, &m.nu02);
514                         ippiGetNormalizedCentralMoment_64f(moment, 3, 0, 0, &m.nu30);
515                         ippiGetNormalizedCentralMoment_64f(moment, 2, 1, 0, &m.nu21);
516                         ippiGetNormalizedCentralMoment_64f(moment, 1, 2, 0, &m.nu12);
517                         ippiGetNormalizedCentralMoment_64f(moment, 0, 3, 0, &m.nu03);
518
519                         ippiMomentFree_64f(moment);
520                         return m;
521                     }
522                     setIppErrorStatus();
523                 }
524                 ippiMomentFree_64f(moment);
525             }
526             else
527                 setIppErrorStatus();
528             CV_SUPPRESS_DEPRECATED_END
529         }
530 #endif
531
532         if( binary || depth == CV_8U )
533             func = momentsInTile<uchar, int, int>;
534         else if( depth == CV_16U )
535             func = momentsInTile<ushort, int, int64>;
536         else if( depth == CV_16S )
537             func = momentsInTile<short, int, int64>;
538         else if( depth == CV_32F )
539             func = momentsInTile<float, double, double>;
540         else if( depth == CV_64F )
541             func = momentsInTile<double, double, double>;
542         else
543             CV_Error( CV_StsUnsupportedFormat, "" );
544
545         Mat src0(mat);
546
547         for( int y = 0; y < size.height; y += TILE_SIZE )
548         {
549             Size tileSize;
550             tileSize.height = std::min(TILE_SIZE, size.height - y);
551
552             for( int x = 0; x < size.width; x += TILE_SIZE )
553             {
554                 tileSize.width = std::min(TILE_SIZE, size.width - x);
555                 Mat src(src0, cv::Rect(x, y, tileSize.width, tileSize.height));
556
557                 if( binary )
558                 {
559                     cv::Mat tmp(tileSize, CV_8U, nzbuf);
560                     cv::compare( src, 0, tmp, CV_CMP_NE );
561                     src = tmp;
562                 }
563
564                 double mom[10];
565                 func( src, mom );
566
567                 if(binary)
568                 {
569                     double s = 1./255;
570                     for( int k = 0; k < 10; k++ )
571                         mom[k] *= s;
572                 }
573
574                 double xm = x * mom[0], ym = y * mom[0];
575
576                 // accumulate moments computed in each tile
577
578                 // + m00 ( = m00' )
579                 m.m00 += mom[0];
580
581                 // + m10 ( = m10' + x*m00' )
582                 m.m10 += mom[1] + xm;
583
584                 // + m01 ( = m01' + y*m00' )
585                 m.m01 += mom[2] + ym;
586
587                 // + m20 ( = m20' + 2*x*m10' + x*x*m00' )
588                 m.m20 += mom[3] + x * (mom[1] * 2 + xm);
589
590                 // + m11 ( = m11' + x*m01' + y*m10' + x*y*m00' )
591                 m.m11 += mom[4] + x * (mom[2] + ym) + y * mom[1];
592
593                 // + m02 ( = m02' + 2*y*m01' + y*y*m00' )
594                 m.m02 += mom[5] + y * (mom[2] * 2 + ym);
595
596                 // + m30 ( = m30' + 3*x*m20' + 3*x*x*m10' + x*x*x*m00' )
597                 m.m30 += mom[6] + x * (3. * mom[3] + x * (3. * mom[1] + xm));
598
599                 // + m21 ( = m21' + x*(2*m11' + 2*y*m10' + x*m01' + x*y*m00') + y*m20')
600                 m.m21 += mom[7] + x * (2 * (mom[4] + y * mom[1]) + x * (mom[2] + ym)) + y * mom[3];
601
602                 // + m12 ( = m12' + y*(2*m11' + 2*x*m01' + y*m10' + x*y*m00') + x*m02')
603                 m.m12 += mom[8] + y * (2 * (mom[4] + x * mom[2]) + y * (mom[1] + xm)) + x * mom[5];
604
605                 // + m03 ( = m03' + 3*y*m02' + 3*y*y*m01' + y*y*y*m00' )
606                 m.m03 += mom[9] + y * (3. * mom[5] + y * (3. * mom[2] + ym));
607             }
608         }
609     }
610
611     completeMomentState( &m );
612     return m;
613 }
614
615
616 void cv::HuMoments( const Moments& m, double hu[7] )
617 {
618     double t0 = m.nu30 + m.nu12;
619     double t1 = m.nu21 + m.nu03;
620
621     double q0 = t0 * t0, q1 = t1 * t1;
622
623     double n4 = 4 * m.nu11;
624     double s = m.nu20 + m.nu02;
625     double d = m.nu20 - m.nu02;
626
627     hu[0] = s;
628     hu[1] = d * d + n4 * m.nu11;
629     hu[3] = q0 + q1;
630     hu[5] = d * (q0 - q1) + n4 * t0 * t1;
631
632     t0 *= q0 - 3 * q1;
633     t1 *= 3 * q0 - q1;
634
635     q0 = m.nu30 - 3 * m.nu12;
636     q1 = 3 * m.nu21 - m.nu03;
637
638     hu[2] = q0 * q0 + q1 * q1;
639     hu[4] = q0 * t0 + q1 * t1;
640     hu[6] = q1 * t0 - q0 * t1;
641 }
642
643 void cv::HuMoments( const Moments& m, OutputArray _hu )
644 {
645     _hu.create(7, 1, CV_64F);
646     Mat hu = _hu.getMat();
647     CV_Assert( hu.isContinuous() );
648     HuMoments(m, (double*)hu.data);
649 }
650
651
652 CV_IMPL void cvMoments( const CvArr* arr, CvMoments* moments, int binary )
653 {
654     const IplImage* img = (const IplImage*)arr;
655     cv::Mat src;
656     if( CV_IS_IMAGE(arr) && img->roi && img->roi->coi > 0 )
657         cv::extractImageCOI(arr, src, img->roi->coi-1);
658     else
659         src = cv::cvarrToMat(arr);
660     cv::Moments m = cv::moments(src, binary != 0);
661     CV_Assert( moments != 0 );
662     *moments = m;
663 }
664
665
666 CV_IMPL double cvGetSpatialMoment( CvMoments * moments, int x_order, int y_order )
667 {
668     int order = x_order + y_order;
669
670     if( !moments )
671         CV_Error( CV_StsNullPtr, "" );
672     if( (x_order | y_order) < 0 || order > 3 )
673         CV_Error( CV_StsOutOfRange, "" );
674
675     return (&(moments->m00))[order + (order >> 1) + (order > 2) * 2 + y_order];
676 }
677
678
679 CV_IMPL double cvGetCentralMoment( CvMoments * moments, int x_order, int y_order )
680 {
681     int order = x_order + y_order;
682
683     if( !moments )
684         CV_Error( CV_StsNullPtr, "" );
685     if( (x_order | y_order) < 0 || order > 3 )
686         CV_Error( CV_StsOutOfRange, "" );
687
688     return order >= 2 ? (&(moments->m00))[4 + order * 3 + y_order] :
689     order == 0 ? moments->m00 : 0;
690 }
691
692
693 CV_IMPL double cvGetNormalizedCentralMoment( CvMoments * moments, int x_order, int y_order )
694 {
695     int order = x_order + y_order;
696
697     double mu = cvGetCentralMoment( moments, x_order, y_order );
698     double m00s = moments->inv_sqrt_m00;
699
700     while( --order >= 0 )
701         mu *= m00s;
702     return mu * m00s * m00s;
703 }
704
705
706 CV_IMPL void cvGetHuMoments( CvMoments * mState, CvHuMoments * HuState )
707 {
708     if( !mState || !HuState )
709         CV_Error( CV_StsNullPtr, "" );
710
711     double m00s = mState->inv_sqrt_m00, m00 = m00s * m00s, s2 = m00 * m00, s3 = s2 * m00s;
712
713     double nu20 = mState->mu20 * s2,
714     nu11 = mState->mu11 * s2,
715     nu02 = mState->mu02 * s2,
716     nu30 = mState->mu30 * s3,
717     nu21 = mState->mu21 * s3, nu12 = mState->mu12 * s3, nu03 = mState->mu03 * s3;
718
719     double t0 = nu30 + nu12;
720     double t1 = nu21 + nu03;
721
722     double q0 = t0 * t0, q1 = t1 * t1;
723
724     double n4 = 4 * nu11;
725     double s = nu20 + nu02;
726     double d = nu20 - nu02;
727
728     HuState->hu1 = s;
729     HuState->hu2 = d * d + n4 * nu11;
730     HuState->hu4 = q0 + q1;
731     HuState->hu6 = d * (q0 - q1) + n4 * t0 * t1;
732
733     t0 *= q0 - 3 * q1;
734     t1 *= 3 * q0 - q1;
735
736     q0 = nu30 - 3 * nu12;
737     q1 = 3 * nu21 - nu03;
738
739     HuState->hu3 = q0 * q0 + q1 * q1;
740     HuState->hu5 = q0 * t0 + q1 * t1;
741     HuState->hu7 = q1 * t0 - q0 * t1;
742 }
743
744
745 /* End of file. */