Mat2Cell error when splitting an image up into smaller images

3 次查看(过去 30 天)
Hello, i have a large Image (3200 x 14444 pixels). I want to split it into subimages. Im using mat2cell but Im getting the error as shown below.
blockSizeRow=512
blockSizeCol=256
function blockImages = splitImageIntoBlocks(image, blockSizeRow, blockSizeCol)
% blockSizeRow: Rows in block
% blockSizeCol: Columns in block
[nrows, ncols] = size(image);
% Calculate size of each block by rows and columns
nBlocksRow = floor(nrows / blockSizeRow);
nBlocksCol = floor(ncols / blockSizeCol);
rowDist = [blockSizeRow * ones(1, nBlocksRow-1), rem(nrows, nBlocksRow) + blockSizeRow];
colDist = [blockSizeCol * ones(1, nBlocksCol-1), rem(ncols, nBlocksCol) + blockSizeCol];
blockImages = mat2cell(image, rowDist, colDist);
Error using mat2cell (line 89)
Input arguments, D1 through D2, must sum to each dimension of the input matrix size, [3200 14444].
  3 个评论
Jason
Jason 2019-10-18
Hi Walter, I'd rather the last block be smaller so I retain as many partial blocks of the same size as possible
Walter Roberson
Walter Roberson 2019-10-18
Last block is mod(nrows, blockSizeRow) with nothing added. Just watch out for the case where the size is exactly divisible into blocks: mat2cell is happy to toss in a block of size 0 but you probably do not want those

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2019-10-20
function blockImages = splitImageIntoBlocks(image, blockSizeRow, blockSizeCol)
% blockSizeRow: Rows in block
% blockSizeCol: Columns in block
[nrows, ncols] = size(image);
% Calculate size of each block by rows and columns
nBlocksRow = floor(nrows / blockSizeRow);
nBlocksCol = floor(ncols / blockSizeCol);
rowDist = [blockSizeRow * ones(1, nBlocksRow), mod(nrows, nBlocksRow)];
colDist = [blockSizeCol * ones(1, nBlocksCol), mod(ncols, nBlocksCol)];
%check for case of file image divisible into blocks
if rowDist(end) == 0; rowDist(end) = []; end
if colDist(end) == 0; colDist(end) = []; end
blockImages = mat2cell(image, rowDist, colDist, size(image,3));

更多回答(0 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by