Stii
Stii

Reputation: 37

Actionscript 3: getColorBoundsRect

I have a question. I have a BitmapData with 2 red circles. I want to find the rectangle area or each circle. If i use [B]getColorBoundsRect[/B] I get the smallest area enclosed by the 2 circles.

How can i go about this and get individual area of the circles? below is a diagram i created to better explain my question:
http://img831.imageshack.us/img831/3360/sampleja.png

previously this question was asked before but don't quite understand how the provided solution solved the problem. http://www.kirupa.com/forum/showthread.php?324586-Question-to-getColorBoundsRect

hope someone here can shed some light for me. Thanks a million.

Upvotes: 1

Views: 1959

Answers (1)

Cay
Cay

Reputation: 3794

There's a very neat trick to do it. First you need to make sure you get only two colors in your BitmapData (threshold will do the trick). After that, you can use getColorBounds together with floodFill to find all blobs in the image. The pseudo-code would be something like this:

//Do the following until rect.width is zero.
rect = bmp.getColorBoundsRect(red);
//check the first row of pixels until you find the start of the blob
for(y = rect.y; y < rect.height + rect.y; y++) {
  if(bmp.getPixel(rect.x,y) == red) {
    bmp.floodFill(rect.x,y, green); // paint the blob green
    blobs.push(bmp.getColorBoundsRect(green)); // get the green bounds and push a new blob
    bmp.floodFill(rect.x,y, white); // clear it
    break;
  }
}

Upvotes: 2

Related Questions