@@ -1486,8 +1486,6 @@ MagickExport ChannelMoments *GetImageChannelMoments(const Image *image,
1486
1486
* channel_moments ;
1487
1487
1488
1488
double
1489
- cx ,
1490
- cy ,
1491
1489
M00 ,
1492
1490
M01 ,
1493
1491
M02 ,
@@ -1500,6 +1498,12 @@ MagickExport ChannelMoments *GetImageChannelMoments(const Image *image,
1500
1498
M22 ,
1501
1499
M30 ;
1502
1500
1501
+ MagickPixelPacket
1502
+ pixel ;
1503
+
1504
+ PointInfo
1505
+ centroid ;
1506
+
1503
1507
ssize_t
1504
1508
y ;
1505
1509
@@ -1527,6 +1531,7 @@ MagickExport ChannelMoments *GetImageChannelMoments(const Image *image,
1527
1531
M21 = 0.0 ;
1528
1532
M22 = 0.0 ;
1529
1533
M30 = 0.0 ;
1534
+ GetMagickPixelPacket (image ,& pixel );
1530
1535
/*
1531
1536
Compute center of mass (centroid).
1532
1537
*/
@@ -1547,19 +1552,15 @@ MagickExport ChannelMoments *GetImageChannelMoments(const Image *image,
1547
1552
indexes = GetVirtualIndexQueue (image );
1548
1553
for (x = 0 ; x < (ssize_t ) image -> columns ; x ++ )
1549
1554
{
1550
- MagickPixelPacket
1551
- pixel ;
1552
-
1553
- GetMagickPixelPacket (image ,& pixel );
1554
1555
SetMagickPixelPacket (image ,p ,indexes + x ,& pixel );
1555
1556
M00 += QuantumScale * pixel .red ;
1556
1557
M10 += x * QuantumScale * pixel .red ;
1557
1558
M01 += y * QuantumScale * pixel .red ;
1558
1559
p ++ ;
1559
1560
}
1560
1561
}
1561
- cx = M10 /M00 ;
1562
- cy = M01 /M00 ;
1562
+ centroid . x = M10 /M00 ;
1563
+ centroid . y = M01 /M00 ;
1563
1564
/*
1564
1565
Compute the image moments.
1565
1566
*/
@@ -1580,19 +1581,16 @@ MagickExport ChannelMoments *GetImageChannelMoments(const Image *image,
1580
1581
indexes = GetVirtualIndexQueue (image );
1581
1582
for (x = 0 ; x < (ssize_t ) image -> columns ; x ++ )
1582
1583
{
1583
- MagickPixelPacket
1584
- pixel ;
1585
-
1586
- GetMagickPixelPacket (image ,& pixel );
1587
1584
SetMagickPixelPacket (image ,p ,indexes + x ,& pixel );
1588
- M11 += (x - cx )* (y - cy )* QuantumScale * pixel .red ;
1589
- M20 += (x - cx )* (x - cx )* QuantumScale * pixel .red ;
1590
- M02 += (y - cy )* (y - cy )* QuantumScale * pixel .red ;
1591
- M21 += (x - cx )* (x - cx )* (y - cy )* QuantumScale * pixel .red ;
1592
- M12 += (x - cx )* (y - cy )* (y - cy )* QuantumScale * pixel .red ;
1593
- M22 += (x - cx )* (x - cx )* (y - cy )* (y - cy )* QuantumScale * pixel .red ;
1594
- M30 += (x - cx )* (x - cx )* (x - cx )* QuantumScale * pixel .red ;
1595
- M03 += (y - cy )* (y - cy )* (y - cy )* QuantumScale * pixel .red ;
1585
+ M11 += (x - centroid .x )* (y - centroid .y )* QuantumScale * pixel .red ;
1586
+ M20 += (x - centroid .x )* (x - centroid .x )* QuantumScale * pixel .red ;
1587
+ M02 += (y - centroid .y )* (y - centroid .y )* QuantumScale * pixel .red ;
1588
+ M21 += (x - centroid .x )* (x - centroid .x )* (y - centroid .y )* QuantumScale * pixel .red ;
1589
+ M12 += (x - centroid .x )* (y - centroid .y )* (y - centroid .y )* QuantumScale * pixel .red ;
1590
+ M22 += (x - centroid .x )* (x - centroid .x )* (y - centroid .y )* (y - centroid .y )*
1591
+ QuantumScale * pixel .red ;
1592
+ M30 += (x - centroid .x )* (x - centroid .x )* (x - centroid .x )* QuantumScale * pixel .red ;
1593
+ M03 += (y - centroid .y )* (y - centroid .y )* (y - centroid .y )* QuantumScale * pixel .red ;
1596
1594
p ++ ;
1597
1595
}
1598
1596
}
@@ -1612,26 +1610,32 @@ MagickExport ChannelMoments *GetImageChannelMoments(const Image *image,
1612
1610
*/
1613
1611
channel_moments [RedChannel ].I [0 ]= M20 + M02 ;
1614
1612
channel_moments [RedChannel ].I [1 ]= (M20 - M02 )* (M20 - M02 )+ 4.0 * M11 * M11 ;
1615
- channel_moments [RedChannel ].I [2 ]= (M30 - 3.0 * M12 )* (M30 - 3.0 * M12 )+ ( 3.0 * M21 - M03 ) *
1616
- (3.0 * M21 - M03 );
1613
+ channel_moments [RedChannel ].I [2 ]= (M30 - 3.0 * M12 )* (M30 - 3.0 * M12 )+
1614
+ (3.0 * M21 - M03 )* ( 3.0 * M21 - M03 ) ;
1617
1615
channel_moments [RedChannel ].I [3 ]= (M30 + M12 )* (M30 + M12 )+ (M21 + M03 )* (M21 + M03 );
1618
1616
channel_moments [RedChannel ].I [4 ]= (M30 - 3.0 * M12 )* (M30 + M12 )* ((M30 + M12 )*
1619
- (M30 + M12 )- 3.0 * (M21 + M03 )* (M21 + M03 ))+ (3.0 * M21 - M03 )* (M21 + M03 )* ( 3.0 * ( M30 + M12 ) *
1620
- (M30 + M12 )- (M21 + M03 )* (M21 + M03 ));
1621
- channel_moments [RedChannel ].I [5 ]= (M20 - M02 )* ((M30 + M12 )* (M30 + M12 )- ( M21 + M03 ) *
1622
- (M21 + M03 ))+ 4.0 * M11 * (M30 + M12 )* (M21 + M03 );
1617
+ (M30 + M12 )- 3.0 * (M21 + M03 )* (M21 + M03 ))+ (3.0 * M21 - M03 )* (M21 + M03 )*
1618
+ (3.0 * ( M30 + M12 ) * ( M30 + M12 )- (M21 + M03 )* (M21 + M03 ));
1619
+ channel_moments [RedChannel ].I [5 ]= (M20 - M02 )* ((M30 + M12 )* (M30 + M12 )-
1620
+ (M21 + M03 )* ( M21 + M03 ) )+ 4.0 * M11 * (M30 + M12 )* (M21 + M03 );
1623
1621
channel_moments [RedChannel ].I [6 ]= (3.0 * M21 - M03 )* (M30 + M12 )* ((M30 + M12 )*
1624
- (M30 + M12 )- 3.0 * (M21 + M03 )* (M21 + M03 ))- (M30 - 3 * M12 )* (M21 + M03 )* ( 3.0 * ( M30 + M12 ) *
1625
- (M30 + M12 )- (M21 + M03 )* (M21 + M03 ));
1622
+ (M30 + M12 )- 3.0 * (M21 + M03 )* (M21 + M03 ))- (M30 - 3 * M12 )* (M21 + M03 )*
1623
+ (3.0 * ( M30 + M12 ) * ( M30 + M12 )- (M21 + M03 )* (M21 + M03 ));
1626
1624
channel_moments [RedChannel ].I [7 ]= M11 * ((M30 + M12 )* (M30 + M12 )- (M03 + M21 )*
1627
1625
(M03 + M21 ))- (M20 - M02 )* (M30 + M12 )* (M03 + M21 );
1628
- channel_moments [RedChannel ].centroid .x = cx ;
1629
- channel_moments [RedChannel ].centroid .y = cy ;
1630
- channel_moments [RedChannel ].ellipse_axis .x = 0.0 ;
1631
- channel_moments [RedChannel ].ellipse_axis .y = 0.0 ;
1632
- channel_moments [RedChannel ].ellipse_angle = 0.0 ;
1633
- channel_moments [RedChannel ].ellipse_eccentricity = 0.0 ;
1634
- channel_moments [RedChannel ].ellipse_intensity = 0.0 ;
1626
+ channel_moments [RedChannel ].centroid = centroid ;
1627
+ channel_moments [RedChannel ].ellipse_axis .x = sqrt ((2.0 /M00 )* ((M20 + M02 )+
1628
+ sqrt (4.0 * M11 * M11 + (M20 - M02 )* (M20 - M02 ))));
1629
+ channel_moments [RedChannel ].ellipse_axis .y = sqrt ((2.0 /M00 )* ((M20 + M02 )-
1630
+ sqrt (4.0 * M11 * M11 + (M20 - M02 )* (M20 - M02 ))));
1631
+ channel_moments [RedChannel ].ellipse_angle = RadiansToDegrees (0.5 * atan (2.0 * M11 /
1632
+ (M20 - M02 )));
1633
+ channel_moments [RedChannel ].ellipse_eccentricity = sqrt (1.0 - (
1634
+ channel_moments [RedChannel ].ellipse_axis .y /
1635
+ channel_moments [RedChannel ].ellipse_axis .x ));
1636
+ channel_moments [RedChannel ].ellipse_intensity = M00 /(MagickPI *
1637
+ channel_moments [RedChannel ].ellipse_axis .x *
1638
+ channel_moments [RedChannel ].ellipse_axis .y );
1635
1639
if (y < image -> rows )
1636
1640
channel_moments = (ChannelMoments * ) RelinquishMagickMemory (channel_moments );
1637
1641
return (channel_moments );
0 commit comments