# How to create an image puzzle?

47 views (last 30 days)

Show older comments

##### 0 Comments

### Accepted Answer

Knut
on 14 Apr 2013

Edited: Knut
on 14 Apr 2013

Hi.

I was not entirely sure what you were requesting, so I choose to do some interpretation. The snip below will load a standard image, divide it into a 3x3 grid, randomly sort those tiles, set one tile to zero, then assemble a "puzzle" image.

You could probably do this neater using image processing toolbox (blkproc), but I don't have that.

%%load and crop image

imdata = imread('ngc6543a.jpg');

new_dims = size(imdata) - rem(size(imdata), 3);

imdata = imdata(1:new_dims(1),1:new_dims(2), :);

%%Arrange into 3x3 cell

block_dims = new_dims./[3 3 1];

blocks = mat2cell(imdata, block_dims(1)*ones(3,1), block_dims(2)*ones(3,1), block_dims(3));

%%Rearrange randomly

blocks(1:9) = blocks(randperm(9));

%%Set one block to zero

blocks(ceil(9*rand(1))) = {zeros(block_dims, class(imdata))};

%%Return to image

puzzle = cell2mat(blocks);

%%Plot input and output

figure(1)

image(imdata)

figure(2)

image(puzzle)

Edit: Removed an error spotted by Zurez

##### 9 Comments

mohamad k
on 28 Jun 2018

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!