Findng blobs in simple 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
jbell
Posts: 1
Joined: 2016-07-01T11:31:53-07:00
Authentication code: 1151

Findng blobs in simple image

Post by jbell »

Hello,

I'm trying to find white "blobs" of pixels in a black image, then segment them out and find their size. All of their sizes, specifically. Also, I'm using the php extention imagick, so I'm not extremely familiar with the ImageMagick calls, but I'm sure they're not much different from what I'm doing; and the concepts are the same.

Right now, I have a method that works, but it's a little hacky, and pretty slow. Here is what I do:

1) I look through every pixel in the image, and once I encounter a white pixel, I
2) Issue a "floodFillPaintImage" to turn that blob of white to yellow (just a random color).
a) I clone this image and save it for later.*
3) I turn all other instances of white in the image to black. I'm left with a black image with my "selected" (yellow) blob.
4) I do a "trimImage", which gives me an image that is just the size of the non-white blob. Then I can find the size by any method I choose.
5) *using the cloned image from 2a, I paint the selected/yellow part black. Now I have an image with white blobs that excludes the blob I just worked on.
Keep looking through pixels.

It works in practice, but it's really slow. And I don't even have to profile the code to know that the slow part is the part where I'm looking through every pixel using two nested for loops, and `getImagePixelColor(x, y)`.

Is there an easier way of doing what I'm trying to do? Is there a better/faster way of iterating over the pixels in PHP?

Here's the images in order of those steps, starting with step 2.

2:
Image
3:
Image
4:
Image
5:
Image
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Findng blobs in simple image

Post by snibgo »

jbell wrote:Is there a better/faster way of iterating over the pixels in PHP?
The answer is: don't do that. Iterating over all the pixels in an interpreted language will be massively slow.

"-connected-components" helps, for example:

Code: Select all

convert 0VmQJod.png -connected-components 8 -auto-level c.png
This assigns a different shade of gray to each component. The text output gives the size of each component, both as number of pixels, and the bounding rectangle. See http://www.imagemagick.org/script/comma ... components

However, I don't know if Imagick can do that.
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: Findng blobs in simple image

Post by fmw42 »

I do not think Imagick can do this. It is not keeping up with Imagemagick development and so is missing many of the new features such as this. You could do it using PHP exec().
Post Reply