Mat2Cell error when splitting an image up into smaller images
6 ビュー (過去 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 件のコメント
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
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 件)
参考
カテゴリ
Help Center および File Exchange で Block Libraries についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!