Findng blobs in simple image
Posted: 2016-07-01T12:13:58-07:00
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:
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: