1

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.

alxx
  • 9,897
  • 4
  • 26
  • 41
Stii
  • 37
  • 4
  • That question suggests read pixels from bitmap data and analyze them yourself, it doesn't have finished solution. If you need it to work fast, you can read not every pixel, but every second or third one. – alxx Jun 20 '11 at 05:42
  • are there any faster way to do it? maybe a build in function in flash? as i need to run this script within an enterframe function, analyzing each pixel every frame seems pretty heavy – Stii Jun 20 '11 at 06:21
  • This is essentially image recognition, it's supposed to be pixel-heavy. You can scan bitmap skipping several pixels until red is found, then find bounds more precisely. Don't forget to lock and unlock bitmapdata, and performance should be OK. – alxx Jun 20 '11 at 07:38
  • One more idea - use getColorBoundsRect, then separate circles with pixel analysis. Might be faster or slower than manual scan (requires testing.) – alxx Jun 20 '11 at 07:40
  • yea, i'm currently testing the method u mention, get the area with getColorBoundsRect 1st, then i scaled it smaller with matrix, then i do pixel analysis. still testing it out though. Thanks for your help! – Stii Jun 20 '11 at 07:58
  • Why scale bitmap data, this is heavy operation. Just increase step when scanning... – alxx Jun 20 '11 at 08:05
  • the reason i scaled it, is to reduce the pixels, so that it performs faster. for example for a 300x250 = 75000px, reduced it by90% making it only 7500px. Would it be faster or slower, if compare with not scaling and looping all the pixels? – Stii Jun 20 '11 at 09:46
  • This is what I'm trying to tell you. You don't have to loop ALL the pixels. Cycles can be run with step more than 1. Find where red is with big step (maximum possible for your circles size.) Then find exact location with additional samples. – alxx Jun 22 '11 at 08:08
  • And if you find bounds with scaled image, they won't be exact, because some resolution is lost. – alxx Jun 22 '11 at 08:09

1 Answers1

2

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;
  }
}
Cay
  • 3,804
  • 2
  • 20
  • 27
  • +1 Clever, but might be too slow with so many pixel operations (although hapening in the fast native code.) – alxx Jun 20 '11 at 08:44
  • I've used it a few times, and it runs fast enough to perform it on a mid-sized BitmapData each frame. I'm pretty sure there are faster algos around, but I love it's simplicity :) – Cay Jun 20 '11 at 08:51
  • @Cay how come the getPixel & floodfill the x is always 0? won't this cause only the 1st column of it is examined? the bmp.getColorBoundsRect(green) only specify the mask color? sorry i'm not very good at bitmapdata manipulation, thus need more pointers from pros like u guys :) thanks – Stii Jun 20 '11 at 10:04
  • sorry, that zero should have been "rect.x"... I just fixed it. – Cay Jun 20 '11 at 13:17
  • Thanks for taking time to answer my question, my 2nd question means, you wrote "bmp.getColorBoundsRect(green)" where as for getColorBoundsRect, it needed at least 2 params right? getColorBoundsRect(mask:uint, color:uint, findColor:Boolean = true) so i think the code will be and error, and i am not sure how to fix it. Thanks for being patient! – Stii Jun 20 '11 at 14:40
  • Oh, it was intended as pseudo-code. I would advice you to learn as much as you can about BitmapData before attempting something like this. Otherwise you will get very frustrated not knowing how to fix or tweak your own code. – Cay Jun 20 '11 at 21:47