Mat2Cell error when splitting an image up into smaller images

2 Ansichten (letzte 30 Tage)
Jason
Jason am 18 Okt. 2019
Kommentiert: Jason am 20 Okt. 2019
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 Kommentare
Jason
Jason am 18 Okt. 2019
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 am 18 Okt. 2019
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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 20 Okt. 2019
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));

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by