Counting number of object in an PNG image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
hs.khan47
Posts: 1
Joined: 2018-09-10T06:27:11-07:00
Authentication code: 1152

Counting number of object in an PNG image

Post by hs.khan47 »

Hi everyone im new to Image magic and image processing
i have a test image
http://binarylabsolutions.com/hs/imgs-test/cam2.jpg

and after performing some operation via php imagic interface i converted the image into this

http://binarylabsolutions.com/hs/imgs-t ... 2_cam2.jpg
http://binarylabsolutions.com/hs/imgs-t ... m2.jpg.png

now i just want to count the number of white objects in the image
like in this example the answer will be "8"
-----------------------------------------------------

there was an old threat on this form which has been deleted and buts it was kind of same problem
https://imagemagick.org/discourse-serve ... hp?t=28856

however fortunately i had copy the 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")
);

but this is not working form me

help please !!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Counting number of object in an PNG image

Post by snibgo »

I'll assume the important image is 3_cam2.jpg.png, which is a PNG with transparency.

"connected-components" does the job. We ignore the transparency ("-alpha off"). The image is noisy, so we ignore connected components of less than, say, 50 pixels. We don't save the resulting image (we save it to "NULL:").

Code: Select all

convert 3_cam2.jpg.png -alpha off -define connected-components:verbose=true -define connected-components:area-threshold=50 -connected-components 4 NULL:
The text output is:

Code: Select all

Objects (id: bounding-box centroid area mean-color):
  0: 665x500+0+0 331.7,240.7 294764 gray(0)
  247: 129x103+82+378 150.2,430.1 10520 gray(255)
  246: 142x100+475+350 541.9,400.2 10169 gray(255)
  244: 97x64+132+232 181.7,263.9 4811 gray(255)
  238: 107x60+434+216 484.1,245.9 4797 gray(255)
  186: 76x40+162+145 200.9,164.4 2470 gray(255)
  176: 80x40+400+125 438.6,145.1 2413 gray(255)
  83: 60x28+185+75 215.3,88.4 1367 gray(255)
  55: 63x25+384+63 413.7,75.3 1189 gray(255)
We see that one component is black and the rest are white. A suitable Unix text filter will count the lines with "gray(255)".

You are using convert within PHP or something, so you need to write the text to an array or something.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Counting number of object in an PNG image

Post by fmw42 »

Please, always provide your IM version and platform when asking questions, since syntax may differ. Also provide your exact command line and if possible your images.

See the top-most post in this forum "IMPORTANT: Please Read This FIRST Before Posting" at http://www.imagemagick.org/discourse-se ... f=1&t=9620

If using Imagemagick 7, then see http://imagemagick.org/script/porting.php#cli


For novices, see

http://www.imagemagick.org/discourse-se ... f=1&t=9620
http://www.imagemagick.org/script/comma ... essing.php
http://www.imagemagick.org/Usage/reference.html
http://www.imagemagick.org/Usage/
https://github.com/ImageMagick/usage-markdown
Post Reply