How do I create a chessboard of variable size from images of individual tiles?

I need to create a chessboard using MATLAB, by using given images of tiles. The main problem, of course, is that the dimensions of the board must be configurable by the user (though it must still be a square). I've tried:
1. Storing the images in a cell array (don't know how to display this). 2. Automating the concatenation of row vectors/cell arrays into a matrix via loops (can't store images in a vector; using a cell array in place of the vector turns the matrix into a cell array).
tile{6} = imread('tw.png','png');
tile{5} = imread('twpw.png','png');
tile{4} = imread('twpb.png','png');
tile{3} = imread('tb.png','png');
tile{2} = imread('tbpw.png','png');
tile{1} = imread('tbpb.png','png');
rc=input('Enter number of rows: ');
board=cell(rc);
for k=1:rc
for l=1:rc
if (mod(k,2)&&mod(l,2))||((mod(k,2)==0)&&(mod(l,2)==0))
if (k==1)||(k==2)
board{k,l}=tile{1};
elseif (k==(rc-1))||(k==rc)
board{k,l}=tile{2};
else
board{k,l}=tile{3};
end
else
if (k==1)||(k==2)
board{k,l}=tile{4};
elseif (k==(rc-1))||(k==rc)
board{k,l}=tile{5};
else
board{k,l}=tile{6};
end
end
end
end
image(board)
And of course, using image on that gives "Numeric or logical matrix required for image CData" . The expected result would be a chessboard with a black tile in the top left corner, two rows of white pawns at the top and two rows of black pawns at the bottom.
I'm pretty much new to using MATLAB, so perhaps there was a function I missed...?

 Accepted Answer

Okay, I only mentioned this code in passing in the question, but I had written a loop that constructed a one-row cell array in the order required and concatenated it onto a pre-allocated matrix. Of course, this resulted in the matrix itself becoming a cell array.
I managed to avoid this change by concatenating the contents of the one-row cell array onto the matrix. Essentially:
board=[board;row{1,:}];
instead of
board=[board;row];
where board is a matrix and row is a one-row cell array.
With this, the function image can be used to create a figure from the matrix board.

More Answers (1)

Did you try the montage() function?

10 Comments

Sorry it took me so long to reply.
I have, but I'm not sure I'm using it right.
Also, I want to fit it into axes in a GUI if possible, so I can make it a mouse-based interface. Would montage work for that, or will I need to go about this another way?
montage() returns an image that you can display in an axes with imshow().
Hmm...I'm probably doing it wrong.
I believe that montage requires me to specify a folder/file? It wouldn't work with a matrix/cell array, right?
Not at this time. The current version of montage only works with a list of filenames. But you have those - I saw them at the beginning of your code - so you should be all set.
I think I will need to use a matrix though; the pieces have to be movable since it's an actual game.
How should I go about doing it in this case?
You'll have to stitch images together:
wideImage = [image1, image2]; % Use comma to do left/right.
tallImage = [image1; image2]; % Use semicolon to do top/bottom
Uhh...actually I meant "how should I go about displaying it in this case?"; I'm already doing that stitching together thing. Sorry about that. :x
Well, somehow managed to solve it myself while screwing around with the coding. Thanks for your help though. :x
OK. If you don't need that "row" cell array later after you construct the montage, just delete it to free up lots of memory:
clear('row');
I was just trying to show you a way you could do it without creating a cell array (using montage()), or using the way that you finally ended up doing:
tallImage = [image1; image2];
I thought you'd know that image1 would be an image and that to get an image you could either use imread or extract it from a cell array with the braces,
image1 = row{1};
So the line would be
tallImage = [row{1}; row{2}];
Of course the 1 and 2 could be index variables. There is a FAQ that explains cell arrays and how/when to use braces or parentheses. http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F

Sign in to comment.

Asked:

on 24 Oct 2014

Commented:

on 29 Oct 2014

Community Treasure Hunt

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

Start Hunting!