How to create an image with a matrix and a cell array

3 views (last 30 days)
I'm currently trying to create an Image of a 2D Game Map in Matlab.
Using a 3rd party program i have created a Matrix M, that designates where which tile should be used.
As of now my Code looks like this:
I=imread('tileset1.png');
M=dlmread('Karte.txt');
%Creating the tiles using the picture
Tileset.width.tiles=32; %Width of my tileset1.png in tiles
Tileset.height.tiles=19; %height of my tileset in tiles
Tile.width=12; %width of a tile in pixels
Tile.height=12; %height of a tile in pixels
Nr.tiles=Tileset.width.tiles*Tileset.height.tiles;
tile=zeros(0,(Nr.tiles));
for n=1:(Nr.tiles)
x=mod(n,Tileset.width.tiles);
y=floor(n/Tileset.width.tiles);
rect=[((x*Tile.width)-Tile.width),((y*Tile.height)+1),Tile.width,(Tile.height-1)];
tile{n}=imcrop(I,rect);
end
I want to create an image using my tile cells in their designated spots using the matrix M.
Thanks in advance !
Edit : I have included Karte.txt and tileset1.png which I use in my code.

Accepted Answer

Guillaume
Guillaume on 4 Dec 2014
If I understood correctly, and assuming M is an m*n matrix of tile indices, it should simply be:
tiledimage = cell2mat(tile(M));
----
Side note: Your
tile=zeros(0,(Nr.tiles));
should be
tile = cell(1, Nr.tiles);
  3 Comments
Guillaume
Guillaume on 4 Dec 2014
That's because some of your images in tile are 12*12 and others are 12*13. There's either a bug in your tile separation code or you got caught by the absurd concept of spatial coordinates implemented by matlab (see the Tips section of imcrop).
Anyway, this is guaranteed to work and is much simpler:
tile = mat2cell(I, ones(1, Tileset.height.tiles) * Tile.height, ones(1, Tileset.width.tiles) * Tile.width, size(I, 3))';
tile = tile(:);
Gino
Gino on 4 Dec 2014
That is really strange. The code works perfectly though, thank you !

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!