I may have a solution but I don't think it's a particularly clean one.
I originally thought that using SetDefine(MagickFormat.Png, ..., ...) would prepare the software to use what is stored as a .png file. But if I just call SetDefine(..., ...), and applied the settings whilst reading in the image I'd have some success. After calling ConnectedComponents() the output image would look correct, filtered down as per the area threshold. However, the IEnumerable that was returned would still have the amount of elements as if it hadn't been filtered. To correct this, I ran the filtered image back through ConnectedComponents() so I could get 'the number of elements in the input image'.
Code is as follows:
I also ran some tests to compare against the Command Line version of Connected Components and area thresholding, and I noticed that the IEnumerable<ConnectedComponent> in Magick.NET returned 11 components while the command line returned 8. In .NET, the first 8 ConnectedComponent elements match the Command Line exactly. Every other element has its surrounding rectangle listed as starting at 1280, 720 (my image is 1280x720) with rectangle size -1219, -719, encompassing the whole image backwards. I can just ignore these values, but I figured I should mention it. I wouldn't have expected something like that, but it might be some weird side-effect of calling ConnectedComponents() twice in succession.
I originally thought that using SetDefine(MagickFormat.Png, ..., ...) would prepare the software to use what is stored as a .png file. But if I just call SetDefine(..., ...), and applied the settings whilst reading in the image I'd have some success. After calling ConnectedComponents() the output image would look correct, filtered down as per the area threshold. However, the IEnumerable that was returned would still have the amount of elements as if it hadn't been filtered. To correct this, I ran the filtered image back through ConnectedComponents() so I could get 'the number of elements in the input image'.
Code is as follows:
MagickReadSettings settings = new MagickReadSettings();
settings.SetDefine("connected-components:area-threshold", "200"); // area > 200px
using (MagickImage compared = new MagickImage(filepath, settings))
{
// Find the connected components
IEnumerable<ConnectedComponent> check = compared.ConnectedComponents(4);
// check.Count = 1596, 'compared' has changed
check = compared.ConnectedComponents(4);
// check.Count = 11
compared.AutoLevel();
compared.Write("..\\..\\Test Images\\connectedcomponents.png");
}
For now, it works enough for me to progress but because I'm calling the same thing twice and don't understand why this happens, it doesn't feel right.I also ran some tests to compare against the Command Line version of Connected Components and area thresholding, and I noticed that the IEnumerable<ConnectedComponent> in Magick.NET returned 11 components while the command line returned 8. In .NET, the first 8 ConnectedComponent elements match the Command Line exactly. Every other element has its surrounding rectangle listed as starting at 1280, 720 (my image is 1280x720) with rectangle size -1219, -719, encompassing the whole image backwards. I can just ignore these values, but I figured I should mention it. I wouldn't have expected something like that, but it might be some weird side-effect of calling ConnectedComponents() twice in succession.