The other day I was looking at on youtube (do yourself a favour and watch it!) and it made me think back to my PHP image hacking days. One thing led to another and I ended up saying to my partner Linds that I’d write her a little PHP demo. 3 hours later, I had digital camo in PHP (click the images for an online demo; refresh to cycle through the different types):
The Online Demo generates larger camo patterns and has a few other camo types; refresh it to cycle through them.
Let me know if you find a use for this code :)
After the break, I go into the detail of how the code is constructed, and you get a funky debug image too!
Ok, so the code isn’t as pretty as it could be, but it’s good enough for me to publish, and I’m confident that it would only take an hour or two to clean it up.
Here’s that debug image I promised you:
The background fill colour is usually chosen from one of the preset camo colours, but in debug mode we make it blue. This helped me make sure that there wasn’t too much background showing through in the final image.
We then draw patches of camo in a randomly chosen camo colour. The patches are drawn in a grid covering in the entire image; each patch begins at one of the red dots in the debug image. We randomly deviate the positions of the patches a little to keep it from being too regular. We also have the patches start and end a little outside the boundary of the final image, to ensure that we don’t get a lot of background colour showing around the edges.
A patch is actually just a big square made of lots of little squares of the same colour. We make the patches irregular by a) randomly altering the dimensions of each patch, and more importantly b) on the outside edge of the patch, we sometimes don’t draw a square, so the patches end up looking like this, instead of a regular square:
Once we’ve created the whole camo pattern, we blur the whole image. Below is a before/after comparison showing the effect of myImageBlur (follow the link for an improved PHP image blur function):
Before Blur |
After Blur |
If you want to go deeper, the code is fairly well commented :)
#1 by ciro on January 9, 2011 - 4:53 am
hello
nice program, i feel the idea of generate camouflage patterns from computer aided methods very attactive.
Specially i have heard about computer software based on fractals and genetics in order to “learn”/generate a camouflage pattern given a set of training images.
I am specially courious about the fitness or the potential evaluation function used in automatic camouflage design, that is not trivial because the methods of human perception are partially unknown.
in general i feel this topic quite interesting, like hacking the human perception system
#2 by LarryM on July 25, 2012 - 8:54 pm
this is so cool thanks a lot and please don’t ever take this page down thanks