Image Tiling

3 views (last 30 days)
harjan
harjan on 16 Aug 2011
hi 2 all, Please give the example for mat2cell

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 16 Aug 2011
doc mat2cell
example
>> A = randi([-345 367],5)
A =
235 -276 -233 -244 122
300 -147 347 -45 -320
-255 44 337 307 260
306 337 1 219 320
105 342 225 339 138
>> mat2cell(A,[4 1],[2 1 2])
ans =
[4x2 double] [4x1 double] [4x2 double]
[1x2 double] [ 225] [1x2 double]
>> ans{:}
ans =
235 -276
300 -147
-255 44
306 337
ans =
105 342
ans =
-233
347
337
1
ans =
225
ans =
-244 122
-45 -320
307 260
219 320
ans =
339 138
>>

More Answers (2)

Walter Roberson
Walter Roberson on 16 Aug 2011
Well, since you asked...
Window = 8;
SplitImage = mat2cell(Image, Window * ones(1,size(Image,1)/Window), Window * ones(1,size(Image,2)/Window) );
But Image Analyst was right to instead recommend imfilter(), nlfilter(), blockproc, or (older MATLAB) blkproc().
  5 Comments
Andrei Bobrov
Andrei Bobrov on 17 Aug 2011
SplitImage=cellfun(@(x)0.25*x,SplitImage,'un',0)
harjan
harjan on 17 Aug 2011
Thx a lot....

Sign in to comment.


Image Analyst
Image Analyst on 4 Feb 2012
Here is some fairly robust and flexible code I wrote to chop or divide a 2D matrix up into tiles of specified size. It first makes a cell array, in the general case where there are partial tiles. If the special case where tiles fit in evenly it also makes a 3D array. It's fairly simple - it just looks long because it's jam packed with descriptive comments.
% Divides a 2D matrix up into a certain number of non-overlapping blocks (tiles).
clc;
% First create some sample data that we will divide up.
% Sample matrix of 17 rows and 16 columns.
% This option will have different sized blocks with the block sizes of 3 and 5.
m = rand(17,16)
% Sample matrix of 15 rows and 20 columns.
% This option will have uniformly sized blocks with the block sizes of 3 and 5.
m = rand(12, 15)
% Modify the above lines to whatever you want or need.
% Get the size of m
[rows columns] = size(m);
% Now specify the block sizes in the y (row) and x (column) directions.
% For the rows, let's say that we want to specify
% that the blocks are blockSizeY elements (rows) tall.
blockSizeY = 3; % Say blockSizeY = 3 for example.
% For the columns, let's say that we want to specify
% that the blocks are blockSizeX elements (columns) wide.
blockSizeX = 5; % Say blockSizeX = 5 for example.
% Again, modify the above lines to whatever you want or need.
% Find out how many times we can replicate this block size vertically
% (down the rows) and still fit inside the array.
numFullSizeBlocksY = floor(rows / blockSizeY)
% Construct the block height array.
blockHeights = blockSizeY * ones(numFullSizeBlocksY, 1)
% Find out if there is a remaining smaller block that didn't fit.
partialBlockY = rem(rows, blockSizeY)
% If there is a smaller block, add it on to the block size array.
if partialBlockY ~= 0
blockHeights = [blockHeights; partialBlockY]
end
% Now we have our list of sizes for the blocks in the Y direction.
% Now do the same thing for the other direction.
% Find out how many times we can replicate this block size horizontally
% (across the columns) and still fit inside the array.
numFullSizeBlocksX = floor(columns / blockSizeX)
% Construct the block width array.
blockWidths = blockSizeX * ones(numFullSizeBlocksX, 1)
% Find out if there is a remaining smaller block that didn't fit.
partialBlockX = rem(columns, blockSizeX)
% If there is a smaller block, add it on to the block size array.
if partialBlockX ~= 0
blockWidths = [blockWidths; partialBlockX]
end
% Now we have our list of sizes for the blocks in the X direction.
% Now divide up the array. If we don't have uniformly sized blocks,
% we can't use a 3D array. In that case we must use a cell array.
% Even if the blocks are all the same size,
% we can still use a cell array if that's what we want to do.
mCell = mat2cell(m, blockHeights, blockWidths)
% The cell array will be 6 rows by 4 columns for this example.
fprintf('Made cell array of %d rows by %d columns.\n', ...
length(blockHeights), length(blockWidths));
% The arrays inside each cell will have a variety of sizes
% since our blocks did not evenly divide the m array.
% Optional: make 3D array if possible.
if partialBlockX == 0 & partialBlockY == 0
% All blocks are the same size.
% We can put into a 3D array.
% Determine the number of slices (planes) in the z direction.
numberOfSlices = numel(mCell);
% Preallocate space for the 3D array.
m3D = zeros(blockSizeY, blockSizeX, numberOfSlices);
% Do the assignment.
for slice = 1 : numFullSizeBlocksY * numFullSizeBlocksX
m3D(:, :, slice) = mCell{slice};
end
fprintf('Made 3D array of %d slices (planes).\n', numberOfSlices);
end

Community Treasure Hunt

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

Start Hunting!