2

I am trying to count the number of object in a image

I have a test image test image with red squars

and after using php magic function I am able to convert it in to a binary image

test image converted into binary image

what I need is a function which return number of white object in the image in this case 8 I don't know much about image however there was a deleted post on the imagemagic from which uses the following command

var_dump( 
        exec("convert out.pbm -define connected-components:verbose=true  -define connected-components:area-threshold=50 \ -connected-components 4 -auto-level -depth 8 test.png")
);
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • I have posted over 40 answers on this, please type the following into the StackOverflow search box `user:2836621 connected components` – Mark Setchell Sep 10 '18 at 15:18
  • @MarkSetchell i was looking this post of your and i think this would be my solution however the exec function is not returning anything, i tired a simple convert command and it was working – Hassan Nisar Khan Sep 10 '18 at 16:08
  • i tired different chunks of command exec("convert binary.jpg -colorspace gray -negate -threshold 10% -define connected-components:verbose=true -define connected-components:area-threshold=100 output.png", $o) its working and the output.png is created (however i get nothing in return in php )but when i add the final command -connected-components 8 -auto-level the output file is not created so it mean there is some error – Hassan Nisar Khan Sep 10 '18 at 16:15

2 Answers2

1

What is your version of ImageMagick? -connected-components needs at least version 6.8.9.10. Take out the \ and make it all one long line. Using new line \ may be confusing PHP exec().

Try it this way and increase your area-threshold to 150:

<?php
exec("convert out.pbm -define connected-components:verbose=true -define connected-components:area-threshold=150 -connected-components 4 -auto-level -depth 8 test.png 2>&1",$out,$returnval);
foreach($out as $text)
{echo "$text<br>";}
?>


It should then return:

Objects (id: bounding-box centroid area mean-color):
  22: 665x500+0+0 332.0,241.1 295195 gray(0)
  7605: 125x101+86+380 150.6,431.3 10246 gray(255)
  6995: 139x105+476+350 541.7,401.0 10087 gray(255)
  5560: 94x62+133+233 182.0,265.4 4622 gray(255)
  5196: 106x61+434+217 483.3,246.8 4608 gray(255)
  3470: 76x42+162+145 201.4,164.9 2448 gray(255)
  3023: 76x40+401+126 438.7,145.5 2391 gray(255)
  1420: 58x28+186+75 215.5,88.7 1315 gray(255)
  992: 61x24+385+64 414.3,75.7 1146 gray(255)
  2: 33x18+0+0 12.9,6.6 391 gray(0)


If you just want the list without the image, you can replace test.png with null:.

If you want the output to be binary rather than grayscale coded for the id numbers, then add -define connected-components:mean-color=true:

<?php
exec("convert out.pbm -define connected-components:verbose=true -define connected-components:area-threshold=150 -define connected-components:mean-color=true -connected-components 4 -auto-level -depth 8 test.png 2>&1",$out,$returnval);
foreach($out as $text)
{echo "$text<br>";}
?>


If you just want the count and the binary output, then try:

<?php
exec("convert image.jpg -define connected-components:verbose=true -define connected-components:area-threshold=150 -define connected-components:mean-color=true -connected-components 4 -auto-level -depth 8 test.png 2>&1 | grep "gray(255)" | wc -l | sed 's/^[ ]*//' ",$out,$returnval);
foreach($out as $text)
{echo "$text<br>";}
?>


Which should return 8.

See https://www.imagemagick.org/script/connected-components.php

fmw42
  • 46,825
  • 10
  • 62
  • 80
0

This seems to do what you need:

<?php
$output=shell_exec("convert -size 1000x1000 xc:black -fill white -draw \"rectangle 10,10 900,900\" -define connected-components:verbose=true -connected-components 4 -auto-level -depth 8 test.png");
echo $output;
?>

Be sure to parse the output, which looks like this:

Objects (id: bounding-box centroid area mean-color):
1: 891x891+10+10 455.0,455.0 793881 srgba(100%,100%,100%,1.08255)
0: 1000x1000+0+0 670.9,670.9 206119 srgba(0%,0%,0%,1.31795)

and pay attention to the colour (if you want white objects) and also the area - the fields/columns are titled/labelled in the first line of output. So you should notice there are 2 objects detected, the first is white and smaller, the second is black and the size of the entire image.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432