How do I store a series of RGB images in a 2D array?

1 Ansicht (letzte 30 Tage)
tekiwibird
tekiwibird am 17 Mär. 2017
Beantwortet: Guillaume am 17 Mär. 2017
Hi,
I'm working on a CNN project and I want to tile an image 224x224x3 with a stride factor S=4 and block size of 11x11. This corresponds to an output 2D array of size 54x54, where each element would be a 11x11x3 image tile.
I have generated these tiles using a for loop. What I wish to do is have an output array of size 54x54, where each element holds an 11x11x3 image tile. So if I index output_array(1,1) it will give me the first 11x11x3 image array I have tiled using my for loop.
Is this possible with matlab, and how would I go by achieving this?
My pseudocode so far:
// load image
for i=1:54
for j=1:54
// tile image by stride and blocksize
output_array(i,j)=tiled_image; //this does not work, I do not know how to initialize output_array
end
end
Help appreciated!
  2 Kommentare
tekiwibird
tekiwibird am 17 Mär. 2017
I just found out that output_array{i,j}=tiled_image would work, are there any downsides of using this?
Rik
Rik am 17 Mär. 2017
There isn't really a downside to using cells, although indexing can be tricky sometimes. I believe you can remove the need for a for-loop with mat2cell.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Guillaume
Guillaume am 17 Mär. 2017
You can indeed use a 2D cell array where each cell is one of the 3D matrix tile. This may be the simplest thing for you. You could also use a 5D matrix where each matrix5D(:, :, :, xtile, ytile) (or squeeze(matrix5D(xtile, ytile, :, :, :))) is one of the tile.
Processing will probably be faster with a 5D matrix than with the cell array, but I would recommend you use whichever form you're most comfortable with. Worry more about code clarity rather than speed to start with. Prologue for either case:
%img: source image
stride = [5, 5]; %height, width
blocksize = [11, 11]; %height, width
tilescount = (size(img(:, :, 1)) - blocksize - 1) / stride + 1;
assert(all(mod(tilescount, 1) == 0), 'cannot divide image into tile evenly')
Generating a cell array
tiles = cell(tilescount);
tileidx = 1;
for col = 1 : stride(2) : size(img, 2 ) - blocksize(2)
for row = 1 : stride(1) : size(img, 1) - blocksize(1)
tiles{tileidx) = img(row:row+stride(1)-1, col:col+stride(2)-1, :);
tileidx = tileidx + 1;
end
end
Generating a 5D matrix:
tiles = zeros([tilescount, blocksize, size(img, 3)]);
tilecol = 1;
for col = 1 : stride(2) : size(img, 2 ) - blocksize(2)
tilerow = 1;
for row = 1 : stride(1) : size(img, 1) - blocksize(1)
tiles(tilerow, tilecol :, :, :) = img(row:row+stride(1)-1, col:col+stride(2)-1, :);
tilerow = tilerow + 1;
end
tilecol = tilecol + 1;
end
%optionally afterward, if you prefer indexing with tiles(:, :, :, tilerow, tilecol)
%tiles = permute(tiles, [4 5 1 2 3]);

Weitere Antworten (0)

Kategorien

Mehr zu Deep Learning Toolbox finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by