How to create an image puzzle?

I have to take an image and break it into 8 different blocks and then reshuffle it. Then the whole reshuffled image should become a part of 3*3 square blocks with one one block empty so as to create an image puzzle. Please guide me . I am using Matlab 2009b

 採用された回答

Knut
Knut 2013 年 4 月 14 日
編集済み: Knut 2013 年 4 月 14 日

1 投票

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 件のコメント

Zurez
Zurez 2013 年 4 月 14 日
編集済み: Zurez 2013 年 4 月 14 日
Yes you guessed it right , I am trying to make an image puzzle with user solving it using the arrow keys. Is there a limit on the dimension or file size of the image used? P.S : While using your code my Matlab R2012a is giving the following errors:
Error using index (line 2) Not enough input arguments. Error in image1 (line 9) blocks(index) = blocks(randperm(9));
Knut
Knut 2013 年 4 月 14 日
If you want user interaction that is outside of my MATLAB competence. You might be able to use my script as a starting-point, and simple user-interaction using the input() function.
I am aware of no limits on image dimensions or file-size. I have tried to make it work with various rgb/mono and uint8/double image formats, but no guarantees. The image will be cropped to the nearest multiple of 3 in both dimensions.
Zurez
Zurez 2013 年 4 月 14 日
Knut, thank you so much for helping me out and spending your precious time for me . I am accepting this answer. I have more queries , but I will post them as a different question :) . Thanx again.
Mark
Mark 2013 年 4 月 14 日
Excuse me..
I have a project, also image puzzle, here's my code, but the problem is the line "s=creat;" i don't know why is it like that. i'm using 7.9.0(R2009b)Matlab version. can you tell me how to fix it? thank you.
Mark
Mark 2013 年 4 月 14 日
%%HELP : PUZZEL
% Use Arrow keys
% \uparrow
% \leftarrow \downarrow \rightarrow
% to move toward the empty
% space.
%%Init
clc;clear;close all;warning off;
im = imread('se.jpg');
im1 = imresize(im,[300 300]);
imshow(im1);
text(50,150,'You Have40 Secs To Solve it !','FontSize',12,'Color','y');
text(75,174.5,'Use Arrow Keys : ','FontSize',12,'Color','y');
text(147.5,200,'\uparrow','FontSize',20,'Color','y','FontWeight','Bold');
text(112.5,225,'\leftarrow \downarrow \rightarrow','FontSize',20,'Color','y','FontWeight','Bold');
pause(3)
res=1;
for i = 1:3
part1(:,:,i) = im1(1:200,1:200,i);
end
for i = 1:3
part4(:,:,i) = im1(1:200,1:200,i);
end
for i = 1:3
part7(:,:,i) = im1(1:200,1:200,i);
end
for i = 1:3
part2(:,:,i) = im1(1:200,1:200,i);
end
for i = 1:3
part5(:,:,i) = im1(1:200,1:200,i);
end
for i = 1:3
part8(:,:,i) = im1(1:200,1:200,i);
end
for i = 1:3
part3(:,:,i) = im1(1:200,1:200,i);
end
for i = 1:3
part6(:,:,i) = im1(1:200,1:200,i);
end
for i = 1:3
part9(:,:,i) = im1(1:200,1:200,i);
end
part9 = uint8(0.941*ones(200,200,3));
partf = {part1 part4 part7 part2 part5 part8 part3 part6 part9};
part = {part1 part4 part7 part2 part5 part8 part3 part6 part9};
part11 = cell(3,3);
s = creat;
for i = 1:9
ind = s(i);
part11(i) = part(ind);
end
part2 = part11;
img = cell2mat(part2);
imshow(img);
move = 0;
tic
%%PLAY
while ~isequal(s,[1:3;4:6;7:9])
move = move + 1;
k = waitforbuttonpress;
cc = get(gcf,'CurrentKey');
ind = find(s==9);
switch ind
case 1
if strcmp(cc,'uparrow')
s = swapmat(s,1,2);
elseif strcmp(cc,'leftarrow')
s = swapmat(s,1,4);
else
s = s;
end
case 2
if strcmp(cc,'uparrow')
s = swapmat(s,2,3);
elseif strcmp(cc,'leftarrow')
s = swapmat(s,2,5);
elseif strcmp(cc,'downarrow');
s = swapmat(s,2,1);
else
s = s;
end
case 3
if strcmp(cc,'downarrow')
s = swapmat(s,3,2);
elseif strcmp(cc,'leftarrow')
s = swapmat(s,3,6);
else
s = s;
end
case 4
if strcmp(cc,'uparrow')
s = swapmat(s,4,5);
elseif strcmp(cc,'leftarrow')
s = swapmat(s,4,7);
elseif strcmp(cc,'rightarrow');
s = swapmat(s,4,1);
else
s = s;
end
case 5
if strcmp(cc,'uparrow')
s = swapmat(s,5,6);
elseif strcmp(cc,'leftarrow')
s = swapmat(s,5,8);
elseif strcmp(cc,'downarrow');
s = swapmat(s,5,4);
elseif strcmp(cc,'rightarrow')
s = swapmat(s,5,2);
else
s = s;
end
case 6
if strcmp(cc,'downarrow')
s = swapmat(s,6,5);
elseif strcmp(cc,'leftarrow')
s = swapmat(s,6,9);
elseif strcmp(cc,'rightarrow');
s = swapmat(s,6,3);
else
s = s;
end
case 7
if strcmp(cc,'uparrow')
s = swapmat(s,7,8);
elseif strcmp(cc,'rightarrow')
s = swapmat(s,7,4);
else
s = s;
end
case 8
if strcmp(cc,'downarrow')
s = swapmat(s,8,7);
elseif strcmp(cc,'uparrow')
s = swapmat(s,8,9);
elseif strcmp(cc,'rightarrow');
s = swapmat(s,8,5);
else
s = s;
end
case 9
if strcmp(cc,'downarrow')
s = swapmat(s,9,8);
elseif strcmp(cc,'rightarrow')
s = swapmat(s,9,6);
else
s = s;
end
end
for i = 1:9
ind = s(i);
part11(i) = part(ind);
end
part2 = part11;
img = cell2mat(part2);
imshow(img);
t = toc;
if t > 40
s = [1:3;4:6;7:9];
res = 0;
end
end
partf1 = cell(3,3); for i = 1:9 ind = s(i); partf1(i) = partf(ind); end img = cell2mat(partf1); imshow(img); if res == 0 text(180,300,'TIME OUT','FontSize',20,'Color','y'); else text(100,300,['TOTAL MOVES : ' num2str(move)],'FontSize',20,'Color','y'); end %% eof
Mark
Mark 2013 年 4 月 14 日
or can i have a code for image puzzle?? because the project will be submitted tomorrow.. can you help me with this?
Zurez
Zurez 2013 年 4 月 15 日
Mark , that creat is a separate function. And my project was to create an image puzzle and develop and algorithm to solve that puzzle. I am still working on it ..
Harshit
Harshit 2013 年 4 月 15 日
mohamad k
mohamad k 2018 年 6 月 28 日
編集済み: mohamad k 2018 年 6 月 28 日
hi guys, I have a project like this and the answer is appropriate but in addition I need to displace the blocks in the project. could you help me how to displace two blocks by clicking on them? thanks

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeWord games についてさらに検索

質問済み:

2013 年 4 月 14 日

編集済み:

2018 年 6 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by