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:
3:
4:
5:
Findng blobs in simple image
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Findng blobs in simple image
The answer is: don't do that. Iterating over all the pixels in an interpreted language will be massively slow.jbell wrote:Is there a better/faster way of iterating over the pixels in PHP?
"-connected-components" helps, for example:
Code: Select all
convert 0VmQJod.png -connected-components 8 -auto-level c.png
However, I don't know if Imagick can do that.
snibgo's IM pages: im.snibgo.com
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Findng blobs in simple image
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().