This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-08-20 21:40:34

gtoal
Scratcher
Registered: 2010-08-15
Posts: 26

counting pixels - sprite question

I want to count all the pixels on a screen of a certain color.  To avoid a very slow algorithm, I will create square sprites of 1x1, 2x2, 4x4, 8x8 etc up to 256x256, and use a recursive divide and conquer algorithm to subdivide the screen into square areas and then see if each square either contains some pixels set, or all pixels set.  If all are set (ire the whole square matches, add the count for that square; if some pixels match (ie 'touching' returns true) then subdivide and test the sub-squares recursively.

(recursion will be handled by a stack of pending parameters)

However - I know how to do the 'Are some pixels set' test with a 'touching' block, but I don't know how to do a 'are all pixels set' test.  Without this I'm forced to recurse down to the level of single pixels which is still quite expensive (though not as expensive as scanning the full screen in an x * y loop)

If my description isn't clear, I've posted equivalent C code at http://www.gtoal.com/scratch/pixels.c.html which works correctly.  (I'm not actually counting pixels in a graphics image here - for test purposes I just imagine that there's a 100 ^ 100 square of pixels at some arbitrary place in the screen)

Any ideas which sprite primitives I can use to get the desired effect?

thanks

Graham

Offline

 

#2 2011-08-21 21:05:35

gtoal
Scratcher
Registered: 2010-08-15
Posts: 26

Re: counting pixels - sprite question

Could I ask folks who read this request and don't plan to post a reply, to post anyway to let me know if you're not posting because of one of:

1) Don't understand what you're trying to do (OK, I'll try to explain better if I get a lot of this response)

2) Understand what you want but don't know if it can be done or how to do it

3) Understand what you want and know for sure that it *can't* be done.

(4 would be 'I know how to do this' but I assume anyone who does would post to explain how!)

thanks,

G

Offline

 

#3 2011-08-21 21:37:43

fullmoon
Retired Community Moderator
Registered: 2007-06-04
Posts: 1000+

Re: counting pixels - sprite question

I wonder if you could use a distinct, single-color background to help with this problem.

Imagine you have a background of color A and you are counting pixels of color B. For a sprite of any size or shape the logical equivalent of <ONLY TOUCHING COLOR A> would be <NOT TOUCHING COLOR B>. Of course this breaks down if you have many different colors floating around on the stage but it's something to think about. Let me know if that helps!


http://i302.photobucket.com/albums/nn100/fullmoon32/wow.jpg

Offline

 

#4 2011-08-21 22:09:35

gtoal
Scratcher
Registered: 2010-08-15
Posts: 26

Re: counting pixels - sprite question

fullmoon wrote:

I wonder if you could use a distinct, single-color background to help with this problem.

Imagine you have a background of color A and you are counting pixels of color B. For a sprite of any size or shape the logical equivalent of <ONLY TOUCHING COLOR A> would be <NOT TOUCHING COLOR B>. Of course this breaks down if you have many different colors floating around on the stage but it's something to think about. Let me know if that helps!

Indeed I think you've got it, for the subset of the problem that I want for one of my programs - the 'roomba remix challenge' needs to know how much of the surface has been traversed by the main sprite, and since the relevant surface is a single color, what you suggest might work.

However when I decided to write this code, I was setting myself the challenge of writing a generic procedure that could be usable anywhere, hence why it would be useful to be able to select any color from the display.

Good suggestion.  I'll give it some more thought and see if there's anything else I can do with it.

Thanks,

G

Offline

 

#5 2011-08-22 13:27:04

fullmoon
Retired Community Moderator
Registered: 2007-06-04
Posts: 1000+

Re: counting pixels - sprite question

Glad I could help. I think your generalized solution is a little impractical at this point because Scratch cannot tell you what color(s) a sprite is touching, only whether it is touching a given color...and for that matter there's no practical way to cycle through a given set of colors. You might find Panther helpful -- it's based off of Scratch and it includes, among other things, improved color detection. Best of luck with your project!


http://i302.photobucket.com/albums/nn100/fullmoon32/wow.jpg

Offline

 

#6 2011-08-22 14:43:45

gtoal
Scratcher
Registered: 2010-08-15
Posts: 26

Re: counting pixels - sprite question

fullmoon wrote:

Glad I could help. I think your generalized solution is a little impractical at this point because Scratch cannot tell you what color(s) a sprite is touching, only whether it is touching a given color...and for that matter there's no practical way to cycle through a given set of colors.

Actually that was exactly what I was thinking of doing - cycling through the named colors as supported by scratch.  It's still an approximation (eg if an image has two different reds) but usually people pick the significant colors to be different from the background colors.  *Maybe* a loop over red/green/cyan/magenta/yellow/black/white would detect all forms of blue?  Without needing to color-pick all the other colors except blue?

Or maybe its enough just to write an approximate framework and then anyone who wants to use it can tweak it to fit their project (or vice versa :-) )

Thanks again,

G

Offline

 

#7 2011-08-22 21:16:09

fullmoon
Retired Community Moderator
Registered: 2007-06-04
Posts: 1000+

Re: counting pixels - sprite question

gtoal wrote:

*Maybe* a loop over red/green/cyan/magenta/yellow/black/white would detect all forms of blue?  Without needing to color-pick all the other colors except blue?

You could definitely cycle over a few chosen colors, but keep in mind that a <TOUCHING COLOR [blue]> block would detect only that specific shade of blue, not any other.


http://i302.photobucket.com/albums/nn100/fullmoon32/wow.jpg

Offline

 

Board footer