How to move a white square through a black grid

1 view (last 30 days)
I have made a black 256x256 grid and i want to move a 8x8 white square in an horizontal line, from the top-left corner to the top-right corner, using for loops and the implay command, so i can view the 36 seperate frames in an order, so it seems like a video is playing,depicting the moving square.
This is the code I tried :
x=zeros(256,256);
for M=8:7:256
for T=2:36
x(1:8,M-7:M,T-1:T)=255;
end
end
implay(x,4)
This code gives me 36 frames, but every one of these 36 frames does not depict the square in a different position as it should, but it depicts all the possible positions.
Can someone help ?

Accepted Answer

KALYAN ACHARJYA
KALYAN ACHARJYA on 5 Jan 2021
Edited: KALYAN ACHARJYA on 5 Jan 2021
Have you tried this way?
x=zeros(256,256);
original_dat=x;
block=ones(8,8);
h=figure,
for i=1:8:256
for j=1:8:256
refresh(h)
x=original_dat;
x(i:i+7,j:j+7)=x(i:i+7,j:j+7)+block;
imshow(x);
pause(0.5); % set as per requirements
end
end
  1 Comment
ATHANASIOS KONSTANTAKOPOULOS
That actually accomplishes what i was looking for, thank you, but isn't there a way to execute it using the implay command?

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!