方法1
如果您有图像处理工具箱,这是首选且最有效的方法。它利用了非常有用的blockproc函数,该函数专为处理块中的大图像而设计。例如,当你的图像没有平均分成相同大小的块时,它会处理填充,并将块处理的结果连接成一个结果矩阵。
vSize = [17935 10968];
imBig = rand([vSize 3]);
nParts = [2 2]; %means divide into 4 parts, 2 horizontal, 2 vertical
blockproc(imBig, ceil(vSize ./ nParts), @yourAlgorithm);
function res = yourAlgorithm(blockStruct)
%do your processing of the block here and
%optionally return a result in 'res'
%for example, just return the RGB vector of the first pixel
res = blockStruct.data(1,1,:);
end
方法2
如果您没有图像处理工具箱,则可以使用mat2cell功能。 Fisrt你找出所需的块大小,然后你得到一个包含不同块的单元格数组。然而,对于如此大的图像,速度和记忆可能成为问题。该代码借鉴了this Matlab Central的答案。
vSize = [17935 10968];
imBig = rand([vSize 3]);
nParts = [2 2]; %means divide into 4 parts, 2 horizontal, 2 vertical
%figure out the size of "regular" block and the last block
vRegBlockSize = ceil(vSize ./ nParts);
vLastBlockSize = vSize - vRegBlockSize .* (nParts - 1);
%put the sizes into a vector
vSplitR = [vRegBlockSize(1)*ones(1,nParts(1)-1), vLastBlockSize(1)];
vSplitC = [vRegBlockSize(2)*ones(1,nParts(2)-1), vLastBlockSize(2)];
%split the image
C = mat2cell(imBig, vSplitR, vSplitC, 3);
%access RGB pixel (x,y) in top left {1,1} block
p = C{1,1}(x, y, :);