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

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.

 Akzeptierte Antwort

Guillaume
Guillaume am 4 Dez. 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 Kommentare

Thank you for the fast reply !
Thanks for the side note, I missed that.
Using cell2mat(tile(M)) returns this error
Error using cat
Dimensions of matrices being concatenated are not consistent.
Error in cell2mat (line 84)
m{n} = cat(1,c{:,n});
Error in Map (line 28)
tiledimage=cell2mat(tile(M));
I have also tried cell2mat(tile{M}) but that results in
Error using cell2mat
Too many input arguments.
Error in Map (line 28)
tiledimage=cell2mat(tile{M});
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(:);
That is really strange. The code works perfectly though, thank you !

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Produkte

Gefragt:

am 4 Dez. 2014

Kommentiert:

am 4 Dez. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by