Code not working. I need to move the image blocks by swapping cell's position inside the main image matrix. Help?

1 次查看(过去 30 天)
clc;clear;close all;warning off;
imdata = imread('check.jpg');
new_dims = size(imdata) - rem(size(imdata), 3);
imdata = imdata(1:new_dims(1),1:new_dims(2), :);
figure(1)
image(imdata)
text(100,300,'You Have40 Secs To Solve it !','FontSize',14,'Color','y');
text(150,350,'Use Arrow Keys : ','FontSize',14,'Color','y');
text(295,400,'\uparrow','FontSize',20,'Color','y','FontWeight','Bold');
text(225,450,'\leftarrow \downarrow \rightarrow','FontSize',20,'Color','y','FontWeight','Bold');
pause(3)
%%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));
%blacking the 9th image first
blocks(9) = {zeros(block_dims, class(imdata))};
%%Rearrange randomly
blocks(1:9) = blocks(randperm(9));
%%Set one block to zero
%
%blocks(3) = {zeros(block_dims, class(imdata))};
%%Return to image
puzzle = cell2mat(blocks);
disp(blocks)
%%Plot input and output
figure(2)
subplot(2,1,1), image(puzzle), axis equal;
%subplot(2,1,2),implay(timer_examp()), axis equal;
%truesize
k=index(cell{blocks(9)})% i am just trying to index a cell
pause(3)
%* bold*timer_examp()
%play
tic
move = 0;k = ind(blocks(9))
while ~isequal(puzzle,[1:3;4:6;7:9])
move= move+1;
m=waitforbuttonpress;
key = get(gcf,'CurrentKey');
% only works for keyboardinput if m==0
; %if it doesn't works then try find (blocks==9)
if strcmp(key,'uparrow')
blocks{k}=blocks{k-1};
elseif strcmp(key,'leftarrow')&& (k>3)
blocks(k) =blocks(k-3);
elseif strcmp(key,'rightarrow')&&(k<3)
blocks(k)= blocks(k+3);
elseif strcmp(key,'downarrow')
blocks(k) = blocks(k+3);
else
blocks(k)
end
end
  2 个评论
Zurez
Zurez 2013-4-19
编辑:Zurez 2013-4-19
I hope that by now , you have understood that half of the code generates a scrambled image/ now I want the user to solve the image puzzle. Now whenever a user should press an arrow key/ let say suppose the uparrow , the black block of the image puzzle should move up. That can only happen if i swap the black cell with the picture block cell above it... I am just not able to do that. Also I want a timer to run , just below the image frame. timer_examp is the timer function.

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2013-4-19
other_k = k - 1;
blocks([k other_k]) = blocks([other_k k]);
  4 个评论
Zurez
Zurez 2013-4-19
clc;clear;close all;warning off; imdata = imread('check.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)); %blacking the 9th image first blocks(9) = {zeros(block_dims, class(imdata))}; % this has become iirelevnet now :p a=blocks;
%% Rearrange randomly blocks(1:9) = blocks(randperm(9)); %% Set one block to zero % %blocks(3) = {zeros(block_dims, class(imdata))}; %% Return to image puzzle = cell2mat(blocks); disp(blocks) %% Plot input and output figure(2) image(puzzle);
k=9% i am just trying give a value to 9
move = 0; while ~isequal(puzzle,[1:3;4:6;7:9]) move= move+1; m=waitforbuttonpress; key = get(gcf,'CurrentKey');
if strcmp(key,'uparrow')
other_k = k - 1;
blocks([k other_k]) = blocks([other_k k]);
puzzle = cell2mat(blocks);
image(puzzle);
%blocks(k)=blocks(k-1);
elseif strcmp(key,'leftarrow')
other_k = k - 3;
blocks([k other_k]) = blocks([other_k k]);
puzzle = cell2mat(blocks);
image(puzzle);
disp(key)
elseif strcmp(key,'rightarrow')
other_k = k +3;
blocks([k other_k]) = blocks([other_k k]);
puzzle = cell2mat(blocks);
image(puzzle);
disp(key)
% blocks(k)= blocks(k+3);
elseif strcmp(key,'downarrow')
other_k = k + 1;
blocks([k other_k]) = blocks([other_k k]);
puzzle = cell2mat(blocks);
image(puzzle);disp(key)
else
blocks(k)
puzzle = cell2mat(blocks);
image(puzzle);
end
end
Zurez
Zurez 2013-4-19
The above is my updated code. I have remove or commented out what I don't need. The value of the blackened block should change every time. And therefore the value of k should also change. How should I do that?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Computer Vision with Simulink 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by