Skip to content

Commit

Permalink
Added Area property to the ConnectedComponent class (#517).
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemstra committed Sep 13, 2019
1 parent 09eef5c commit e0fbe49
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
6 changes: 6 additions & 0 deletions src/Magick.NET/Shared/Types/ConnectedComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public sealed partial class ConnectedComponent
{
private ConnectedComponent(IntPtr instance)
{
Area = (int)NativeConnectedComponent.GetArea(instance);
Centroid = PointD.FromPointInfo(NativeConnectedComponent.GetCentroid(instance));
Color = NativeConnectedComponent.GetColor(instance);
Height = NativeConnectedComponent.GetHeight(instance);
Expand All @@ -32,6 +33,11 @@ private ConnectedComponent(IntPtr instance)
Y = NativeConnectedComponent.GetY(instance);
}

/// <summary>
/// Gets the pixel count of the area.
/// </summary>
public int Area { get; }

/// <summary>
/// Gets the centroid of the area.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,21 @@ public void ShouldReturnTheConnectedComponents()

var color = MagickColors.Black;

AssertComponent(image, components[1], 2, 94, 297, 128, 151, color, 157, 371);
AssertComponent(image, components[2], 5, 99, 554, 128, 150, color, 162, 628);
AssertComponent(image, components[3], 4, 267, 432, 89, 139, color, 310, 501);
AssertComponent(image, components[4], 1, 301, 202, 148, 143, color, 374, 272);
AssertComponent(image, components[5], 6, 341, 622, 136, 150, color, 408, 696);
AssertComponent(image, components[6], 3, 434, 411, 88, 139, color, 477, 480);
#if Q8
AssertComponent(image, components[1], 2, 94, 297, 128, 151, 11783, color, 157, 371);
AssertComponent(image, components[2], 5, 99, 554, 128, 150, 11772, color, 162, 628);
AssertComponent(image, components[3], 4, 267, 432, 89, 139, 11792, color, 310, 501);
AssertComponent(image, components[4], 1, 301, 202, 148, 143, 11801, color, 374, 272);
AssertComponent(image, components[5], 6, 341, 622, 136, 150, 11793, color, 408, 696);
AssertComponent(image, components[6], 3, 434, 411, 88, 139, 11835, color, 477, 480);
#else
AssertComponent(image, components[1], 2, 94, 297, 128, 151, 11737, color, 157, 371);
AssertComponent(image, components[2], 5, 99, 554, 128, 150, 11734, color, 162, 628);
AssertComponent(image, components[3], 4, 267, 432, 89, 139, 11749, color, 310, 501);
AssertComponent(image, components[4], 1, 301, 202, 148, 143, 11755, color, 374, 272);
AssertComponent(image, components[5], 6, 341, 622, 136, 150, 11746, color, 408, 696);
AssertComponent(image, components[6], 3, 434, 411, 88, 139, 11793, color, 477, 480);
#endif
}

#if !Q8
Expand All @@ -102,24 +111,24 @@ public void ShouldReturnTheConnectedComponents()
var color1 = new MagickColor("#010101010101");
var color2 = MagickColors.Black;

AssertComponent(image, components[1], 597, 90, 293, 139, 162, color1, 157, 372);
AssertComponent(image, components[2], 3439, 96, 550, 138, 162, color1, 162, 628);
AssertComponent(image, components[3], 4367, 213, 633, 1, 2, color2, 213, 633);
AssertComponent(image, components[4], 4412, 215, 637, 3, 1, color2, 215, 637);
AssertComponent(image, components[5], 4453, 217, 641, 3, 1, color2, 217, 641);
AssertComponent(image, components[6], 4495, 219, 645, 3, 1, color2, 219, 645);
AssertComponent(image, components[7], 4538, 221, 647, 3, 1, color2, 221, 649);
AssertComponent(image, components[8], 2105, 268, 433, 89, 139, color1, 311, 502);
AssertComponent(image, components[9], 17, 298, 198, 155, 151, color1, 375, 273);
AssertComponent(image, components[10], 4202, 337, 618, 148, 158, color1, 409, 696);
AssertComponent(image, components[11], 314, 410, 247, 2, 1, color2, 410, 247);
AssertComponent(image, components[12], 1703, 434, 411, 88, 140, color1, 477, 480);
AssertComponent(image, components[1], 597, 90, 293, 139, 162, 11902, color1, 157, 372);
AssertComponent(image, components[2], 3439, 96, 550, 138, 162, 11999, color1, 162, 628);
AssertComponent(image, components[3], 4367, 213, 633, 1, 2, 1, color2, 213, 633);
AssertComponent(image, components[4], 4412, 215, 637, 3, 1, 1, color2, 215, 637);
AssertComponent(image, components[5], 4453, 217, 641, 3, 1, 1, color2, 217, 641);
AssertComponent(image, components[6], 4495, 219, 645, 3, 1, 1, color2, 219, 645);
AssertComponent(image, components[7], 4538, 221, 647, 3, 1, 1, color2, 221, 649);
AssertComponent(image, components[8], 2105, 268, 433, 89, 139, 11808, color1, 311, 502);
AssertComponent(image, components[9], 17, 298, 198, 155, 151, 11927, color1, 375, 273);
AssertComponent(image, components[10], 4202, 337, 618, 148, 158, 11974, color1, 409, 696);
AssertComponent(image, components[11], 314, 410, 247, 2, 1, 2, color2, 410, 247);
AssertComponent(image, components[12], 1703, 434, 411, 88, 140, 11763, color1, 477, 480);
}
#endif
}
}

private void AssertComponent(IMagickImage image, ConnectedComponent component, int id, int x, int y, int width, int height, MagickColor color, int centroidX, int centroidY)
private void AssertComponent(IMagickImage image, ConnectedComponent component, int id, int x, int y, int width, int height, int area, MagickColor color, int centroidX, int centroidY)
{
var delta = 2;

Expand All @@ -128,15 +137,16 @@ private void AssertComponent(IMagickImage image, ConnectedComponent component, i
Assert.AreEqual(y, component.Y, delta);
Assert.AreEqual(width, component.Width, delta);
Assert.AreEqual(height, component.Height, delta);
Assert.AreEqual(area, component.Area, delta);
ColorAssert.AreEqual(color, component.Color);
Assert.AreEqual(centroidX, component.Centroid.X, delta);
Assert.AreEqual(centroidY, component.Centroid.Y, delta);

using (IMagickImage area = image.Clone())
using (IMagickImage componentImage = image.Clone())
{
area.Crop(component.ToGeometry(10));
Assert.AreEqual(width + 20, area.Width, delta);
Assert.AreEqual(height + 20, area.Height, delta);
componentImage.Crop(component.ToGeometry(10));
Assert.AreEqual(width + 20, componentImage.Width, delta);
Assert.AreEqual(height + 20, componentImage.Height, delta);
}
}
}
Expand Down

0 comments on commit e0fbe49

Please sign in to comment.