Add MagickConnectedComponentsImage function
When converting an image using ImageMagick, you can pass the option connected-components to search the image for areas that are linked through pixels with the same intensity values.
When defining the option connected-components:verbose=true, the convert command will return something like this:
/var/www # convert test-9.jpg -define connected-components:verbose=true -define connected-components:area-threshold=1000 -define connected-components:keep=0 -connected-components 4 test-10.jpg
Objects (id: bounding-box centroid area mean-color):
0: 434x600+0+0 221.0,277.3 222040 gray(0)
2031: 138x197+139+311 194.3,433.1 16175 gray(255)
4822: 380x36+22+538 211.8,554.9 11737 gray(255)
664: 33x189+10+81 26.1,177.9 4782 gray(255)
3701: 111x72+289+441 334.1,477.4 4021 gray(255)
88: 123x25+64+42 124.5,57.3 1572 gray(255)
I am wondering on how to get this output from Imagick. I have tried something like this:
$image = new \Imagick;
$image->setOption('connected-components', '4');
$image->readImage('test-9.jpg');
$image->setImageArtifact('connected-components:verbose', 'true');
$image->setImageArtifact('connected-components:area', '1000');
$image->setImageArtifact('connected-components:keep', '0');
$image->write('test-10.jpg');
// Should this contain the connected components?
print_r($image->identifyImage(TRUE));
Am I close? Is this even possible at the moment? Thanks for helping!
I have no idea.
If you want this to work, please could you ask upstream at https://github.com/ImageMagick/ImageMagick something like 'how get the connected components output through the magick wand api? Is this possible to call through the C library?'.
Thanks for your speedy response, I opened an issue upstream. https://github.com/ImageMagick/ImageMagick/issues/1568
Possibly including the details of what you're trying to do i.e. the first code box from above, might be clearer....I think they have a younger volunteer triaging their issues who might not know what you mean.
Apparently, the parameters are returned through an object (CCObjectInfo?). See https://imagemagick.org/api/magick-image.php#MagickConnectedComponentsImage for more information.
For reference.
typedef struct _CCObjectInfo
{
ssize_t
id;
RectangleInfo
bounding_box;
PixelInfo
color;
PointInfo
centroid;
double
area,
census;
} CCObjectInfo;
This is going to need thinking about, if nothing else.
Theoretically, the correct way to do this would be to add types (i.e. classes) for RectangleInfo PointInfo and probably CObjectInfo as well. In practice that's a lot of work...
I can work around the issue by executing the command and capturing the output directly. Feel free to put this on the 'long term todo list' :)