001package org.opencv.imgproc;
002
003//javadoc:Moments
004public class Moments {
005
006    public double m00;
007    public double m10;
008    public double m01;
009    public double m20;
010    public double m11;
011    public double m02;
012    public double m30;
013    public double m21;
014    public double m12;
015    public double m03;
016
017    public double mu20;
018    public double mu11;
019    public double mu02;
020    public double mu30;
021    public double mu21;
022    public double mu12;
023    public double mu03;
024
025    public double nu20;
026    public double nu11;
027    public double nu02;
028    public double nu30;
029    public double nu21;
030    public double nu12;
031    public double nu03;
032
033    public Moments(
034        double m00,
035        double m10,
036        double m01,
037        double m20,
038        double m11,
039        double m02,
040        double m30,
041        double m21,
042        double m12,
043        double m03)
044    {
045        this.m00 = m00;
046        this.m10 = m10;
047        this.m01 = m01;
048        this.m20 = m20;
049        this.m11 = m11;
050        this.m02 = m02;
051        this.m30 = m30;
052        this.m21 = m21;
053        this.m12 = m12;
054        this.m03 = m03;
055        this.completeState();
056    }
057
058    public Moments() {
059        this(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
060    }
061
062    public Moments(double[] vals) {
063        set(vals);
064    }
065
066    public void set(double[] vals) {
067        if (vals != null) {
068            m00 = vals.length > 0 ? vals[0] : 0;
069            m10 = vals.length > 1 ? vals[1] : 0;
070            m01 = vals.length > 2 ? vals[2] : 0;
071            m20 = vals.length > 3 ? vals[3] : 0;
072            m11 = vals.length > 4 ? vals[4] : 0;
073            m02 = vals.length > 5 ? vals[5] : 0;
074            m30 = vals.length > 6 ? vals[6] : 0;
075            m21 = vals.length > 7 ? vals[7] : 0;
076            m12 = vals.length > 8 ? vals[8] : 0;
077            m03 = vals.length > 9 ? vals[9] : 0;
078            this.completeState();
079        } else {
080            m00 = 0;
081            m10 = 0;
082            m01 = 0;
083            m20 = 0;
084            m11 = 0;
085            m02 = 0;
086            m30 = 0;
087            m21 = 0;
088            m12 = 0;
089            m03 = 0;
090            mu20 = 0;
091            mu11 = 0;
092            mu02 = 0;
093            mu30 = 0;
094            mu21 = 0;
095            mu12 = 0;
096            mu03 = 0;
097            nu20 = 0;
098            nu11 = 0;
099            nu02 = 0;
100            nu30 = 0;
101            nu21 = 0;
102            nu12 = 0;
103            nu03 = 0;
104        }
105    }
106
107    @Override
108    public String toString() {
109        return "Moments [ " +
110            "\n" +
111            "m00=" + m00 + ", " +
112            "\n" +
113            "m10=" + m10 + ", " +
114            "m01=" + m01 + ", " +
115            "\n" +
116            "m20=" + m20 + ", " +
117            "m11=" + m11 + ", " +
118            "m02=" + m02 + ", " +
119            "\n" +
120            "m30=" + m30 + ", " +
121            "m21=" + m21 + ", " +
122            "m12=" + m12 + ", " +
123            "m03=" + m03 + ", " +
124            "\n" +
125            "mu20=" + mu20 + ", " +
126            "mu11=" + mu11 + ", " +
127            "mu02=" + mu02 + ", " +
128            "\n" +
129            "mu30=" + mu30 + ", " +
130            "mu21=" + mu21 + ", " +
131            "mu12=" + mu12 + ", " +
132            "mu03=" + mu03 + ", " +
133            "\n" +
134            "nu20=" + nu20 + ", " +
135            "nu11=" + nu11 + ", " +
136            "nu02=" + nu02 + ", " +
137            "\n" +
138            "nu30=" + nu30 + ", " +
139            "nu21=" + nu21 + ", " +
140            "nu12=" + nu12 + ", " +
141            "nu03=" + nu03 + ", " +
142            "\n]";
143    }
144
145    protected void completeState()
146    {
147        double cx = 0, cy = 0;
148        double mu20, mu11, mu02;
149        double inv_m00 = 0.0;
150
151        if( Math.abs(this.m00) > 0.00000001 )
152        {
153            inv_m00 = 1. / this.m00;
154            cx = this.m10 * inv_m00;
155            cy = this.m01 * inv_m00;
156        }
157
158        // mu20 = m20 - m10*cx
159        mu20 = this.m20 - this.m10 * cx;
160        // mu11 = m11 - m10*cy
161        mu11 = this.m11 - this.m10 * cy;
162        // mu02 = m02 - m01*cy
163        mu02 = this.m02 - this.m01 * cy;
164
165        this.mu20 = mu20;
166        this.mu11 = mu11;
167        this.mu02 = mu02;
168
169        // mu30 = m30 - cx*(3*mu20 + cx*m10)
170        this.mu30 = this.m30 - cx * (3 * mu20 + cx * this.m10);
171        mu11 += mu11;
172        // mu21 = m21 - cx*(2*mu11 + cx*m01) - cy*mu20
173        this.mu21 = this.m21 - cx * (mu11 + cx * this.m01) - cy * mu20;
174        // mu12 = m12 - cy*(2*mu11 + cy*m10) - cx*mu02
175        this.mu12 = this.m12 - cy * (mu11 + cy * this.m10) - cx * mu02;
176        // mu03 = m03 - cy*(3*mu02 + cy*m01)
177        this.mu03 = this.m03 - cy * (3 * mu02 + cy * this.m01);
178
179
180        double inv_sqrt_m00 = Math.sqrt(Math.abs(inv_m00));
181        double s2 = inv_m00*inv_m00, s3 = s2*inv_sqrt_m00;
182
183        this.nu20 = this.mu20*s2;
184        this.nu11 = this.mu11*s2;
185        this.nu02 = this.mu02*s2;
186        this.nu30 = this.mu30*s3;
187        this.nu21 = this.mu21*s3;
188        this.nu12 = this.mu12*s3;
189        this.nu03 = this.mu03*s3;
190
191    }
192
193    public double get_m00() { return this.m00; }
194    public double get_m10() { return this.m10; }
195    public double get_m01() { return this.m01; }
196    public double get_m20() { return this.m20; }
197    public double get_m11() { return this.m11; }
198    public double get_m02() { return this.m02; }
199    public double get_m30() { return this.m30; }
200    public double get_m21() { return this.m21; }
201    public double get_m12() { return this.m12; }
202    public double get_m03() { return this.m03; }
203    public double get_mu20() { return this.mu20; }
204    public double get_mu11() { return this.mu11; }
205    public double get_mu02() { return this.mu02; }
206    public double get_mu30() { return this.mu30; }
207    public double get_mu21() { return this.mu21; }
208    public double get_mu12() { return this.mu12; }
209    public double get_mu03() { return this.mu03; }
210    public double get_nu20() { return this.nu20; }
211    public double get_nu11() { return this.nu11; }
212    public double get_nu02() { return this.nu02; }
213    public double get_nu30() { return this.nu30; }
214    public double get_nu21() { return this.nu21; }
215    public double get_nu12() { return this.nu12; }
216    public double get_nu03() { return this.nu03; }
217
218    public void set_m00(double m00) { this.m00 = m00; }
219    public void set_m10(double m10) { this.m10 = m10; }
220    public void set_m01(double m01) { this.m01 = m01; }
221    public void set_m20(double m20) { this.m20 = m20; }
222    public void set_m11(double m11) { this.m11 = m11; }
223    public void set_m02(double m02) { this.m02 = m02; }
224    public void set_m30(double m30) { this.m30 = m30; }
225    public void set_m21(double m21) { this.m21 = m21; }
226    public void set_m12(double m12) { this.m12 = m12; }
227    public void set_m03(double m03) { this.m03 = m03; }
228    public void set_mu20(double mu20) { this.mu20 = mu20; }
229    public void set_mu11(double mu11) { this.mu11 = mu11; }
230    public void set_mu02(double mu02) { this.mu02 = mu02; }
231    public void set_mu30(double mu30) { this.mu30 = mu30; }
232    public void set_mu21(double mu21) { this.mu21 = mu21; }
233    public void set_mu12(double mu12) { this.mu12 = mu12; }
234    public void set_mu03(double mu03) { this.mu03 = mu03; }
235    public void set_nu20(double nu20) { this.nu20 = nu20; }
236    public void set_nu11(double nu11) { this.nu11 = nu11; }
237    public void set_nu02(double nu02) { this.nu02 = nu02; }
238    public void set_nu30(double nu30) { this.nu30 = nu30; }
239    public void set_nu21(double nu21) { this.nu21 = nu21; }
240    public void set_nu12(double nu12) { this.nu12 = nu12; }
241    public void set_nu03(double nu03) { this.nu03 = nu03; }
242}