Image Tiling
Show older comments
hi 2 all, Please give the example for mat2cell
Accepted Answer
More Answers (2)
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
harjan
on 16 Aug 2011
Walter Roberson
on 16 Aug 2011
SplitImage is going to have 64 total cells, 8 by 8. Each of the cells is going to be an 8 x 8 array. There is no 32 involved.
You will be able to do operations such as
SplitImage{2,5} = cos(SplitImage{2,5});
but if your image is one of the integer data types, you will need to take that in to account.
Question: you say that your image is 256 x 256, but is it grayscale or is it pseudocolor or is it RGB ? The splitting code I showed is not correct for RGB: for RGB it should be
SplitImage = mat2cell(Image, Window * ones(1,size(Image,1)/Window), Window * ones(1,size(Image,2)/Window), 3 );
harjan
on 17 Aug 2011
Andrei Bobrov
on 17 Aug 2011
SplitImage=cellfun(@(x)0.25*x,SplitImage,'un',0)
harjan
on 17 Aug 2011
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
Categories
Find more on Axes Transformations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!