0

I have divided a 512X512 image into 2X2 pixel blocks. Thus I have 65536 blocks in total. Each block has four pixels.

Image divided into blocks like this

Now I want to traverse the image in random order. As for example: starting from 6th block, then to 3rd block, then to 8th, then to 1st block...... like this until the whole image is traversed.

Important: I need to store the traversing order for later use.

Please help me writing a MATLAB code for this. Many many many thanks in advance.

matlabcoder
  • 57
  • 2
  • 2
  • 7
  • 2
    Define _traverse_: what do you want to do with each block? – Luis Mendo Jun 28 '16 at 14:10
  • in which format is the picture and do you already have a those individual blocks in matlab? – Finn Jun 28 '16 at 14:15
  • Thank you. Traversing means going from one block to another until all blocks are reached. After accessing each block, i will do some operation on that block, and then go to another block. I can traverse or scan across the blocks in raster scan order. But not in random order :-( – matlabcoder Jun 28 '16 at 14:24
  • @matlabcoder But it all depends on the operation you apply to the block. What's the final desired output? Or maybe post your code with raster scan so we can see what you want to do and how to modify it – Luis Mendo Jun 28 '16 at 14:24
  • its gray scale image. and yes I already have those individual blocks in matlab. I can scan across the blocks in raster scan order. – matlabcoder Jun 28 '16 at 14:28
  • can we use loops to scan or traverse in random order. Because I need to store the traversing order for later use. – matlabcoder Jun 28 '16 at 14:52

2 Answers2

1

Easy, let's make an example with small matrix (6x6)

Im = rand(6,6);
nblocks = 9;
blocksize = 2;

You will have blocks of size 2x2 (in total 3x3=9 blocks). Reshape the matrix into a 2 x 18 matrix.

Im = reshape(Im, numel(Im)/blocksize, blocksize);

Now generate a random permutation of indexes separated by the size of the block:

idx = randperm(nblocks) * blocksize;

Et voilà. Now you can access the 5th block just doing:

 currentblock = Im(idx(5):idx(5)+blocksize, :);

Use a loop to transverse each block.

  • Thank you. Traversing means going from one block to another until all blocks are reached. After accessing each block, i will do some operation on that block, and then go to another block. – matlabcoder Jun 28 '16 at 14:23
  • can we use loops to scan or traverse in random order. Because I need to store the traversing order for later use. – matlabcoder Jun 28 '16 at 14:55
  • idx stores the order that you will transverse the matrix. You just need to iterate from 1 to numel(idx) –  Jun 28 '16 at 15:01
1

You can divide the image into blocks and tile them along a third dimension using this great answer. You then loop over a random permutation of the third dimension indices:

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
Community
  • 1
  • 1
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147