我已将512X512图像分成2X2像素块。因此我总共有65536块。每个块有四个像素。
现在我想以随机顺序遍历图像。例如:从第6个街区开始,然后到第3个街区,然后到第8个街区,再到第1个街区......就像这样,直到整个图像被遍历。
重要提示:我需要存储遍历顺序以供日后使用。
请帮我写一个MATLAB代码。很多人都提前感谢。
答案 0 :(得分:1)
很简单,让我们以小矩阵(6x6)
为例Im = rand(6,6);
nblocks = 9;
blocksize = 2;
您将拥有大小为2x2的块(总共3x3 = 9个块)。 将矩阵重塑为2 x 18矩阵。
Im = reshape(Im, numel(Im)/blocksize, blocksize);
现在生成由块大小分隔的索引的随机排列:
idx = randperm(nblocks) * blocksize;
Etvoilà。现在你可以访问第5块了:
currentblock = Im(idx(5):idx(5)+blocksize, :);
使用循环横切每个块。
答案 1 :(得分:1)
您可以使用this great answer将图像划分为块并沿第三维平铺它们。然后循环遍历第三维索引的随机排列:
A = randn(12,12);
m = 3;
n = 6;
T = permute(reshape(permute(reshape(A, size(A, 1), n, []), [2 1 3]), n, m, []), [2 1 3]);
% each third-dim slice is an mxn block
scan_order = randperm(size(T,3)); % random permutation of block indices
for b = scan_order
block = T(:,:,b);
% Do stuff with current block
end