moments.cpp 19.7 KB
Newer Older
wester committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                        Intel License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of Intel Corporation may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "precomp.hpp"

// The function calculates center of gravity and the central second order moments
wester committed
44
static void icvCompleteMomentState( CvMoments* moments )
wester committed
45 46 47
{
    double cx = 0, cy = 0;
    double mu20, mu11, mu02;
wester committed
48

wester committed
49
    assert( moments != 0 );
wester committed
50
    moments->inv_sqrt_m00 = 0;
wester committed
51 52 53

    if( fabs(moments->m00) > DBL_EPSILON )
    {
wester committed
54
        double inv_m00 = 1. / moments->m00;
wester committed
55 56
        cx = moments->m10 * inv_m00;
        cy = moments->m01 * inv_m00;
wester committed
57
        moments->inv_sqrt_m00 = std::sqrt( fabs(inv_m00) );
wester committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    }

    // mu20 = m20 - m10*cx
    mu20 = moments->m20 - moments->m10 * cx;
    // mu11 = m11 - m10*cy
    mu11 = moments->m11 - moments->m10 * cy;
    // mu02 = m02 - m01*cy
    mu02 = moments->m02 - moments->m01 * cy;

    moments->mu20 = mu20;
    moments->mu11 = mu11;
    moments->mu02 = mu02;

    // mu30 = m30 - cx*(3*mu20 + cx*m10)
    moments->mu30 = moments->m30 - cx * (3 * mu20 + cx * moments->m10);
    mu11 += mu11;
    // mu21 = m21 - cx*(2*mu11 + cx*m01) - cy*mu20
    moments->mu21 = moments->m21 - cx * (mu11 + cx * moments->m01) - cy * mu20;
    // mu12 = m12 - cy*(2*mu11 + cy*m10) - cx*mu02
    moments->mu12 = moments->m12 - cy * (mu11 + cy * moments->m10) - cx * mu02;
    // mu03 = m03 - cy*(3*mu02 + cy*m01)
    moments->mu03 = moments->m03 - cy * (3 * mu02 + cy * moments->m01);
}


wester committed
83
static void icvContourMoments( CvSeq* contour, CvMoments* moments )
wester committed
84
{
wester committed
85
    int is_float = CV_SEQ_ELTYPE(contour) == CV_32FC2;
wester committed
86

wester committed
87
    if( contour->total )
wester committed
88
    {
wester committed
89 90 91 92
        CvSeqReader reader;
        double a00, a10, a01, a20, a11, a02, a30, a21, a12, a03;
        double xi, yi, xi2, yi2, xi_1, yi_1, xi_12, yi_12, dxy, xii_1, yii_1;
        int lpt = contour->total;
wester committed
93

wester committed
94 95 96
        a00 = a10 = a01 = a20 = a11 = a02 = a30 = a21 = a12 = a03 = 0;

        cvStartReadSeq( contour, &reader, 0 );
wester committed
97 98 99

        if( !is_float )
        {
wester committed
100 101
            xi_1 = ((CvPoint*)(reader.ptr))->x;
            yi_1 = ((CvPoint*)(reader.ptr))->y;
wester committed
102 103 104
        }
        else
        {
wester committed
105 106
            xi_1 = ((CvPoint2D32f*)(reader.ptr))->x;
            yi_1 = ((CvPoint2D32f*)(reader.ptr))->y;
wester committed
107
        }
wester committed
108
        CV_NEXT_SEQ_ELEM( contour->elem_size, reader );
wester committed
109

wester committed
110 111
        xi_12 = xi_1 * xi_1;
        yi_12 = yi_1 * yi_1;
wester committed
112

wester committed
113
        while( lpt-- > 0 )
wester committed
114
        {
wester committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
            if( !is_float )
            {
                xi = ((CvPoint*)(reader.ptr))->x;
                yi = ((CvPoint*)(reader.ptr))->y;
            }
            else
            {
                xi = ((CvPoint2D32f*)(reader.ptr))->x;
                yi = ((CvPoint2D32f*)(reader.ptr))->y;
            }
            CV_NEXT_SEQ_ELEM( contour->elem_size, reader );

            xi2 = xi * xi;
            yi2 = yi * yi;
            dxy = xi_1 * yi - xi * yi_1;
            xii_1 = xi_1 + xi;
            yii_1 = yi_1 + yi;

            a00 += dxy;
            a10 += dxy * xii_1;
            a01 += dxy * yii_1;
            a20 += dxy * (xi_1 * xii_1 + xi2);
            a11 += dxy * (xi_1 * (yii_1 + yi_1) + xi * (yii_1 + yi));
            a02 += dxy * (yi_1 * yii_1 + yi2);
            a30 += dxy * xii_1 * (xi_12 + xi2);
            a03 += dxy * yii_1 * (yi_12 + yi2);
            a21 +=
                dxy * (xi_12 * (3 * yi_1 + yi) + 2 * xi * xi_1 * yii_1 +
                       xi2 * (yi_1 + 3 * yi));
            a12 +=
                dxy * (yi_12 * (3 * xi_1 + xi) + 2 * yi * yi_1 * xii_1 +
                       yi2 * (xi_1 + 3 * xi));

            xi_1 = xi;
            yi_1 = yi;
            xi_12 = xi2;
            yi_12 = yi2;
wester committed
152
        }
wester committed
153 154 155 156

        double db1_2, db1_6, db1_12, db1_24, db1_20, db1_60;

        if( fabs(a00) > FLT_EPSILON )
wester committed
157
        {
wester committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
            if( a00 > 0 )
            {
                db1_2 = 0.5;
                db1_6 = 0.16666666666666666666666666666667;
                db1_12 = 0.083333333333333333333333333333333;
                db1_24 = 0.041666666666666666666666666666667;
                db1_20 = 0.05;
                db1_60 = 0.016666666666666666666666666666667;
            }
            else
            {
                db1_2 = -0.5;
                db1_6 = -0.16666666666666666666666666666667;
                db1_12 = -0.083333333333333333333333333333333;
                db1_24 = -0.041666666666666666666666666666667;
                db1_20 = -0.05;
                db1_60 = -0.016666666666666666666666666666667;
            }
wester committed
176

wester committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190
            // spatial moments
            moments->m00 = a00 * db1_2;
            moments->m10 = a10 * db1_6;
            moments->m01 = a01 * db1_6;
            moments->m20 = a20 * db1_12;
            moments->m11 = a11 * db1_24;
            moments->m02 = a02 * db1_12;
            moments->m30 = a30 * db1_20;
            moments->m21 = a21 * db1_60;
            moments->m12 = a12 * db1_60;
            moments->m03 = a03 * db1_20;

            icvCompleteMomentState( moments );
        }
wester committed
191 192 193 194 195 196 197 198 199
    }
}


/****************************************************************************************\
*                                Spatial Raster Moments                                  *
\****************************************************************************************/

template<typename T, typename WT, typename MT>
wester committed
200 201 202 203 204
#if defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ >= 5 && __GNUC_MINOR__ < 9
// Workaround for http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60196
__attribute__((optimize("no-tree-vectorize")))
#endif
static void momentsInTile( const cv::Mat& img, double* moments )
wester committed
205
{
wester committed
206 207 208 209 210
    cv::Size size = img.size();
    int x, y;
    MT mom[10] = {0,0,0,0,0,0,0,0,0,0};

    for( y = 0; y < size.height; y++ )
wester committed
211
    {
wester committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
        const T* ptr = (const T*)(img.data + y*img.step);
        WT x0 = 0, x1 = 0, x2 = 0;
        MT x3 = 0;

        for( x = 0; x < size.width; x++ )
        {
            WT p = ptr[x];
            WT xp = x * p, xxp;

            x0 += p;
            x1 += xp;
            xxp = xp * x;
            x2 += xxp;
            x3 += xxp * x;
        }

        WT py = y * x0, sy = y*y;

        mom[9] += ((MT)py) * sy;  // m03
        mom[8] += ((MT)x1) * sy;  // m12
        mom[7] += ((MT)x2) * y;  // m21
        mom[6] += x3;             // m30
        mom[5] += x0 * sy;        // m02
        mom[4] += x1 * y;         // m11
        mom[3] += x2;             // m20
        mom[2] += py;             // m01
        mom[1] += x1;             // m10
        mom[0] += x0;             // m00
wester committed
240
    }
wester committed
241 242 243 244 245

    for( x = 0; x < 10; x++ )
        moments[x] = (double)mom[x];
}

wester committed
246 247 248

#if CV_SSE2

wester committed
249
template<> void momentsInTile<uchar, int, int>( const cv::Mat& img, double* moments )
wester committed
250
{
wester committed
251 252 253 254 255 256 257
    typedef uchar T;
    typedef int WT;
    typedef int MT;
    cv::Size size = img.size();
    int y;
    MT mom[10] = {0,0,0,0,0,0,0,0,0,0};
    bool useSIMD = cv::checkHardwareSupport(CV_CPU_SSE2);
wester committed
258

wester committed
259
    for( y = 0; y < size.height; y++ )
wester committed
260
    {
wester committed
261 262
        const T* ptr = img.ptr<T>(y);
        int x0 = 0, x1 = 0, x2 = 0, x3 = 0, x = 0;
wester committed
263 264 265

        if( useSIMD )
        {
a  
Kai Westerkamp committed
266
            __m128i qx_init = _mm_setr_epi16(0, 1, 2, 3, 4, 5, 6, 7);
wester committed
267
            __m128i dx = _mm_set1_epi16(8);
a  
Kai Westerkamp committed
268
            __m128i z = _mm_setzero_si128(), qx0 = z, qx1 = z, qx2 = z, qx3 = z, qx = qx_init;
wester committed
269

wester committed
270
            for( ; x <= size.width - 8; x += 8 )
wester committed
271 272
            {
                __m128i p = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i*)(ptr + x)), z);
a  
Kai Westerkamp committed
273
                qx0 = _mm_add_epi32(qx0, _mm_sad_epu8(p, z));
wester committed
274 275
                __m128i px = _mm_mullo_epi16(p, qx);
                __m128i sx = _mm_mullo_epi16(qx, qx);
wester committed
276 277
                qx1 = _mm_add_epi32(qx1, _mm_madd_epi16(p, qx));
                qx2 = _mm_add_epi32(qx2, _mm_madd_epi16(p, sx));
wester committed
278
                qx3 = _mm_add_epi32(qx3, _mm_madd_epi16(px, sx));
wester committed
279 280 281

                qx = _mm_add_epi16(qx, dx);
            }
wester committed
282
            int CV_DECL_ALIGNED(16) buf[4];
a  
Kai Westerkamp committed
283 284 285 286 287 288 289 290
            _mm_store_si128((__m128i*)buf, qx0);
            x0 = buf[0] + buf[1] + buf[2] + buf[3];
            _mm_store_si128((__m128i*)buf, qx1);
            x1 = buf[0] + buf[1] + buf[2] + buf[3];
            _mm_store_si128((__m128i*)buf, qx2);
            x2 = buf[0] + buf[1] + buf[2] + buf[3];
            _mm_store_si128((__m128i*)buf, qx3);
            x3 = buf[0] + buf[1] + buf[2] + buf[3];
wester committed
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
        }

        for( ; x < size.width; x++ )
        {
            WT p = ptr[x];
            WT xp = x * p, xxp;

            x0 += p;
            x1 += xp;
            xxp = xp * x;
            x2 += xxp;
            x3 += xxp * x;
        }

        WT py = y * x0, sy = y*y;

        mom[9] += ((MT)py) * sy;  // m03
        mom[8] += ((MT)x1) * sy;  // m12
        mom[7] += ((MT)x2) * y;  // m21
        mom[6] += x3;             // m30
        mom[5] += x0 * sy;        // m02
        mom[4] += x1 * y;         // m11
        mom[3] += x2;             // m20
        mom[2] += py;             // m01
        mom[1] += x1;             // m10
        mom[0] += x0;             // m00
    }

wester committed
319
    for(int x = 0; x < 10; x++ )
wester committed
320 321 322
        moments[x] = (double)mom[x];
}

wester committed
323
#endif
wester committed
324

wester committed
325
typedef void (*CvMomentsInTileFunc)(const cv::Mat& img, double* moments);
wester committed
326

wester committed
327
CV_IMPL void cvMoments( const void* array, CvMoments* moments, int binary )
wester committed
328
{
wester committed
329 330 331 332 333 334 335 336 337
    const int TILE_SIZE = 32;
    int type, depth, cn, coi = 0;
    CvMat stub, *mat = (CvMat*)array;
    CvMomentsInTileFunc func = 0;
    CvContour contourHeader;
    CvSeq* contour = 0;
    CvSeqBlock block;
    double buf[TILE_SIZE*TILE_SIZE];
    uchar nzbuf[TILE_SIZE*TILE_SIZE];
wester committed
338

wester committed
339
    if( CV_IS_SEQ( array ))
wester committed
340
    {
wester committed
341 342 343
        contour = (CvSeq*)array;
        if( !CV_IS_SEQ_POINT_SET( contour ))
            CV_Error( CV_StsBadArg, "The passed sequence is not a valid contour" );
wester committed
344 345
    }

wester committed
346 347
    if( !moments )
        CV_Error( CV_StsNullPtr, "" );
wester committed
348

wester committed
349
    memset( moments, 0, sizeof(*moments));
wester committed
350

wester committed
351
    if( !contour )
wester committed
352
    {
wester committed
353 354
        mat = cvGetMat( mat, &stub, &coi );
        type = CV_MAT_TYPE( mat->type );
wester committed
355

wester committed
356 357 358 359 360 361
        if( type == CV_32SC2 || type == CV_32FC2 )
        {
            contour = cvPointSeqFromMat(
                CV_SEQ_KIND_CURVE | CV_SEQ_FLAG_CLOSED,
                mat, &contourHeader, &block );
        }
wester committed
362 363
    }

wester committed
364 365 366 367 368
    if( contour )
    {
        icvContourMoments( contour, moments );
        return;
    }
wester committed
369

wester committed
370 371 372
    type = CV_MAT_TYPE( mat->type );
    depth = CV_MAT_DEPTH( type );
    cn = CV_MAT_CN( type );
wester committed
373

wester committed
374
    cv::Size size = cvGetMatSize( mat );
wester committed
375

wester committed
376 377
    if( cn > 1 && coi == 0 )
        CV_Error( CV_StsBadArg, "Invalid image type" );
wester committed
378 379

    if( size.width <= 0 || size.height <= 0 )
wester committed
380 381 382 383 384 385 386 387 388 389 390 391 392 393
        return;

    if( binary || depth == CV_8U )
        func = momentsInTile<uchar, int, int>;
    else if( depth == CV_16U )
        func = momentsInTile<ushort, int, int64>;
    else if( depth == CV_16S )
        func = momentsInTile<short, int, int64>;
    else if( depth == CV_32F )
        func = momentsInTile<float, double, double>;
    else if( depth == CV_64F )
        func = momentsInTile<double, double, double>;
    else
        CV_Error( CV_StsUnsupportedFormat, "" );
wester committed
394

wester committed
395
    cv::Mat src0(mat);
wester committed
396

wester committed
397 398 399 400
    for( int y = 0; y < size.height; y += TILE_SIZE )
    {
        cv::Size tileSize;
        tileSize.height = std::min(TILE_SIZE, size.height - y);
wester committed
401

wester committed
402
        for( int x = 0; x < size.width; x += TILE_SIZE )
a  
Kai Westerkamp committed
403
        {
wester committed
404 405 406 407
            tileSize.width = std::min(TILE_SIZE, size.width - x);
            cv::Mat src(src0, cv::Rect(x, y, tileSize.width, tileSize.height));

            if( coi > 0 )
a  
Kai Westerkamp committed
408
            {
wester committed
409 410 411 412
                cv::Mat tmp(tileSize, depth, buf);
                int pairs[] = {coi-1, 0};
                cv::mixChannels(&src, 1, &tmp, 1, pairs, 1);
                src = tmp;
a  
Kai Westerkamp committed
413
            }
wester committed
414
            if( binary )
wester committed
415
            {
wester committed
416 417 418 419
                cv::Mat tmp(tileSize, CV_8U, nzbuf);
                cv::compare( src, 0, tmp, CV_CMP_NE );
                src = tmp;
            }
wester committed
420

wester committed
421 422
            double mom[10];
            func( src, mom );
a  
Kai Westerkamp committed
423

wester committed
424 425 426 427 428 429
            if(binary)
            {
                double s = 1./255;
                for( int k = 0; k < 10; k++ )
                    mom[k] *= s;
            }
wester committed
430

wester committed
431
            double xm = x * mom[0], ym = y * mom[0];
wester committed
432

wester committed
433
            // accumulate moments computed in each tile
wester committed
434

wester committed
435 436
            // + m00 ( = m00' )
            moments->m00 += mom[0];
wester committed
437

wester committed
438 439
            // + m10 ( = m10' + x*m00' )
            moments->m10 += mom[1] + xm;
wester committed
440

wester committed
441 442
            // + m01 ( = m01' + y*m00' )
            moments->m01 += mom[2] + ym;
wester committed
443

wester committed
444 445
            // + m20 ( = m20' + 2*x*m10' + x*x*m00' )
            moments->m20 += mom[3] + x * (mom[1] * 2 + xm);
wester committed
446

wester committed
447 448
            // + m11 ( = m11' + x*m01' + y*m10' + x*y*m00' )
            moments->m11 += mom[4] + x * (mom[2] + ym) + y * mom[1];
wester committed
449

wester committed
450 451
            // + m02 ( = m02' + 2*y*m01' + y*y*m00' )
            moments->m02 += mom[5] + y * (mom[2] * 2 + ym);
wester committed
452

wester committed
453 454
            // + m30 ( = m30' + 3*x*m20' + 3*x*x*m10' + x*x*x*m00' )
            moments->m30 += mom[6] + x * (3. * mom[3] + x * (3. * mom[1] + xm));
wester committed
455

wester committed
456 457
            // + m21 ( = m21' + x*(2*m11' + 2*y*m10' + x*m01' + x*y*m00') + y*m20')
            moments->m21 += mom[7] + x * (2 * (mom[4] + y * mom[1]) + x * (mom[2] + ym)) + y * mom[3];
wester committed
458

wester committed
459 460
            // + m12 ( = m12' + y*(2*m11' + 2*x*m01' + y*m10' + x*y*m00') + x*m02')
            moments->m12 += mom[8] + y * (2 * (mom[4] + x * mom[2]) + y * (mom[1] + xm)) + x * mom[5];
wester committed
461

wester committed
462 463
            // + m03 ( = m03' + 3*y*m02' + 3*y*y*m01' + y*y*y*m00' )
            moments->m03 += mom[9] + y * (3. * mom[5] + y * (3. * mom[2] + ym));
wester committed
464 465 466
        }
    }

wester committed
467
    icvCompleteMomentState( moments );
wester committed
468 469 470
}


wester committed
471
CV_IMPL void cvGetHuMoments( CvMoments * mState, CvHuMoments * HuState )
wester committed
472
{
wester committed
473 474
    if( !mState || !HuState )
        CV_Error( CV_StsNullPtr, "" );
wester committed
475

wester committed
476
    double m00s = mState->inv_sqrt_m00, m00 = m00s * m00s, s2 = m00 * m00, s3 = s2 * m00s;
wester committed
477

wester committed
478 479 480 481 482
    double nu20 = mState->mu20 * s2,
        nu11 = mState->mu11 * s2,
        nu02 = mState->mu02 * s2,
        nu30 = mState->mu30 * s3,
        nu21 = mState->mu21 * s3, nu12 = mState->mu12 * s3, nu03 = mState->mu03 * s3;
wester committed
483

wester committed
484 485
    double t0 = nu30 + nu12;
    double t1 = nu21 + nu03;
wester committed
486

wester committed
487
    double q0 = t0 * t0, q1 = t1 * t1;
wester committed
488

wester committed
489 490 491
    double n4 = 4 * nu11;
    double s = nu20 + nu02;
    double d = nu20 - nu02;
wester committed
492

wester committed
493 494 495 496
    HuState->hu1 = s;
    HuState->hu2 = d * d + n4 * nu11;
    HuState->hu4 = q0 + q1;
    HuState->hu6 = d * (q0 - q1) + n4 * t0 * t1;
wester committed
497

wester committed
498 499
    t0 *= q0 - 3 * q1;
    t1 *= 3 * q0 - q1;
wester committed
500

wester committed
501 502
    q0 = nu30 - 3 * nu12;
    q1 = 3 * nu21 - nu03;
wester committed
503

wester committed
504 505 506
    HuState->hu3 = q0 * q0 + q1 * q1;
    HuState->hu5 = q0 * t0 + q1 * t1;
    HuState->hu7 = q1 * t0 - q0 * t1;
wester committed
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
}


CV_IMPL double cvGetSpatialMoment( CvMoments * moments, int x_order, int y_order )
{
    int order = x_order + y_order;

    if( !moments )
        CV_Error( CV_StsNullPtr, "" );
    if( (x_order | y_order) < 0 || order > 3 )
        CV_Error( CV_StsOutOfRange, "" );

    return (&(moments->m00))[order + (order >> 1) + (order > 2) * 2 + y_order];
}


CV_IMPL double cvGetCentralMoment( CvMoments * moments, int x_order, int y_order )
{
    int order = x_order + y_order;

    if( !moments )
        CV_Error( CV_StsNullPtr, "" );
    if( (x_order | y_order) < 0 || order > 3 )
        CV_Error( CV_StsOutOfRange, "" );

    return order >= 2 ? (&(moments->m00))[4 + order * 3 + y_order] :
wester committed
533
           order == 0 ? moments->m00 : 0;
wester committed
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
}


CV_IMPL double cvGetNormalizedCentralMoment( CvMoments * moments, int x_order, int y_order )
{
    int order = x_order + y_order;

    double mu = cvGetCentralMoment( moments, x_order, y_order );
    double m00s = moments->inv_sqrt_m00;

    while( --order >= 0 )
        mu *= m00s;
    return mu * m00s * m00s;
}


wester committed
550
namespace cv
wester committed
551 552
{

wester committed
553 554 555 556 557 558
Moments::Moments()
{
    m00 = m10 = m01 = m20 = m11 = m02 = m30 = m21 = m12 = m03 =
    mu20 = mu11 = mu02 = mu30 = mu21 = mu12 = mu03 =
    nu20 = nu11 = nu02 = nu30 = nu21 = nu12 = nu03 = 0.;
}
wester committed
559

wester committed
560 561 562 563 564 565
Moments::Moments( double _m00, double _m10, double _m01, double _m20, double _m11,
                  double _m02, double _m30, double _m21, double _m12, double _m03 )
{
    m00 = _m00; m10 = _m10; m01 = _m01;
    m20 = _m20; m11 = _m11; m02 = _m02;
    m30 = _m30; m21 = _m21; m12 = _m12; m03 = _m03;
wester committed
566

wester committed
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
    double cx = 0, cy = 0, inv_m00 = 0;
    if( std::abs(m00) > DBL_EPSILON )
    {
        inv_m00 = 1./m00;
        cx = m10*inv_m00; cy = m01*inv_m00;
    }

    mu20 = m20 - m10*cx;
    mu11 = m11 - m10*cy;
    mu02 = m02 - m01*cy;

    mu30 = m30 - cx*(3*mu20 + cx*m10);
    mu21 = m21 - cx*(2*mu11 + cx*m01) - cy*mu20;
    mu12 = m12 - cy*(2*mu11 + cy*m10) - cx*mu02;
    mu03 = m03 - cy*(3*mu02 + cy*m01);

    double inv_sqrt_m00 = std::sqrt(std::abs(inv_m00));
    double s2 = inv_m00*inv_m00, s3 = s2*inv_sqrt_m00;

    nu20 = mu20*s2; nu11 = mu11*s2; nu02 = mu02*s2;
    nu30 = mu30*s3; nu21 = mu21*s3; nu12 = mu12*s3; nu03 = mu03*s3;
}

Moments::Moments( const CvMoments& m )
{
    *this = Moments(m.m00, m.m10, m.m01, m.m20, m.m11, m.m02, m.m30, m.m21, m.m12, m.m03);
}

Moments::operator CvMoments() const
{
    CvMoments m;
    m.m00 = m00; m.m10 = m10; m.m01 = m01;
    m.m20 = m20; m.m11 = m11; m.m02 = m02;
    m.m30 = m30; m.m21 = m21; m.m12 = m12; m.m03 = m03;
    m.mu20 = mu20; m.mu11 = mu11; m.mu02 = mu02;
    m.mu30 = mu30; m.mu21 = mu21; m.mu12 = mu12; m.mu03 = mu03;
    double am00 = std::abs(m00);
    m.inv_sqrt_m00 = am00 > DBL_EPSILON ? 1./std::sqrt(am00) : 0;

    return m;
}

}

cv::Moments cv::moments( InputArray _array, bool binaryImage )
{
    CvMoments om;
    Mat arr = _array.getMat();
    CvMat c_array = arr;
    cvMoments(&c_array, &om, binaryImage);
    return om;
}

void cv::HuMoments( const Moments& m, double hu[7] )
{
    double t0 = m.nu30 + m.nu12;
    double t1 = m.nu21 + m.nu03;
wester committed
624 625 626

    double q0 = t0 * t0, q1 = t1 * t1;

wester committed
627 628 629
    double n4 = 4 * m.nu11;
    double s = m.nu20 + m.nu02;
    double d = m.nu20 - m.nu02;
wester committed
630

wester committed
631 632 633 634
    hu[0] = s;
    hu[1] = d * d + n4 * m.nu11;
    hu[3] = q0 + q1;
    hu[5] = d * (q0 - q1) + n4 * t0 * t1;
wester committed
635 636 637 638

    t0 *= q0 - 3 * q1;
    t1 *= 3 * q0 - q1;

wester committed
639 640
    q0 = m.nu30 - 3 * m.nu12;
    q1 = 3 * m.nu21 - m.nu03;
wester committed
641

wester committed
642 643 644
    hu[2] = q0 * q0 + q1 * q1;
    hu[4] = q0 * t0 + q1 * t1;
    hu[6] = q1 * t0 - q0 * t1;
wester committed
645 646
}

wester committed
647 648 649 650 651 652 653
void cv::HuMoments( const Moments& m, OutputArray _hu )
{
    _hu.create(7, 1, CV_64F);
    Mat hu = _hu.getMat();
    CV_Assert( hu.isContinuous() );
    HuMoments(m, (double*)hu.data);
}
wester committed
654 655

/* End of file. */