Mat2Cell error when splitting an image up into smaller images

조회 수: 3 (최근 30일)
Jason
Jason 2019년 10월 18일
댓글: Jason 2019년 10월 20일
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