Page 1 of 1

Findng blobs in simple image

Posted: 2016-07-01T12:13:58-07:00
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

Re: Findng blobs in simple image

Posted: 2016-07-01T12:30:45-07:00
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.

Re: Findng blobs in simple image

Posted: 2016-07-01T15:18:29-07:00
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().