,.
. :%%%. .%%%.
__%%%(\ `%%%%% .%%%%%
/a ^ '% %%%% %: ,% %%"`
'__.. ,'% .-%: %-' %
~~""%:. ` % ' . `.
%% % ` %% .%: . \.
%%:. `-' ` .%% . %: :\
%(%,%..." `%, %%' %% ) )
%)%%)%%' )%%%.....- ' "/ (
%a:f%%\ % / \`% "%%% ` / \))
%(%' % /-. \ ' \ |-. '.
`' |% `() \| `()
|| / () /
() 0 | o
\ /\ o /
o ` /-|
___________ ,-/ ` _________ ,-/ _________________ INCLUDES _ OCAML _ SOURCE _ CODE _ EXAMPLES _____________________________________________

Saturday, January 24, 2015

The Puzzle Filter

New Year, New Post. Today, I will present you a little idea I have had.
We will call it the puzzle filter. This filter doesn't create the impression that your image has been transformed into an already solved puzzle by drawing the outlines of all the pieces like that :
I prefer to explain you how to shuffle it. Here are some results with our classic test image of Lena :
 
According to this result, the function we will see (in 4 parts) will take different parameters like the image matrix representation, its dimensions and also the color of the pieces' outlines, the size n of the pieces which are squares. Here is an example of its declaration (in OCaml) :

Now, the first thing to do is to consider what will be the puzzle pieces according to your image. If the dimensions of your image are 512x512 and the dimensions of the pieces are 64x64 for example, there are therefore only (512/64)*(512/64) = 64 puzzle pieces on your image. And you must stock all these pieces in an array. So your array must have (width/n)*(height/n) indexes and each index must contain a small matrix of size nxn.

After filling your array with all the puzzle pieces of your image, you have to shuffle them. A very simple solution can be to choose 2 random positions (must be different) in your array, swap the content of the corresponding indexes and then just repeat this procedure in function of the number of pieces. Here is an example of source code :

Finally, the last procedure consists in displaying all the puzzle pieces in the new order of your array.
You can also delimit the pieces by drawing their outline using the color parameter of the function.