0

Although I am able to successfully scramble images (using JigSaw, or RANDBLOCK), I cannot figure out how to scramble specific matrix elements (blocks) within each image. I am unable to attach images, so precisely what I require is shown in image form here (just hit 'scramble'). As you will see, the first image is divided into an 8 x 7 matrix, and then specific elements are scrambled to produce the second image, i.e.

[1,1;1,2;1,3;1,4;1,5;2,1;2,2;2,3;2,4;2,5;3,1;3,2;3,3;3,4;3,5;4,2;4,3;4,4;5,2;5,3;5,4;6,2;6,3;6,4] 

I would be ever so grateful for any advice, as I am a novice to MATLAB, and need to complete the stimuli asap!

Many thanks in advance,

Maria

Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • You should demonstrate some basic research on this topic. Look up how to index blocks within a matrix and you should be good to go. – Marius Aug 26 '15 at 07:18

1 Answers1

4

I would prefer Lena, so here we go -

%// Indices of specific blocks to be randomized
sp_idx = [2,2;2,3;2,4;2,5;2,6;3,2;3,3;3,4;3,5;3,6;4,2;4,3;4,4;4,5;4,6;
          5,3;5,4;5,5;6,3;6,4;6,5;7,3;7,4;7,5];

%// Invite lena to MATLAB workspace and *cut off her right arm*
im = imread('http://www.ece.rice.edu/~wakin/images/lenaTest3.jpg');
im = im(:,1:448);

%// Define blocksize (rows x columns)
n = 64;
m = 64;

%// New random indices corresponding to sp_idx
new_rand_idx = sp_idx(randperm(size(sp_idx,1)),:);

%// Split image into blocks
split_blks = mat2cell(im, ones(1,8)*n, ones(1,7)*m);

%// Get old and new linear indices and thus randomize specific blocks
old_lind = sub2ind(size(split_blks),sp_idx(:,1),sp_idx(:,2));
new_lind = sub2ind(size(split_blks),new_rand_idx(:,1),new_rand_idx(:,2));
split_blks(new_lind) = split_blks(old_lind);
new_im = cell2mat(split_blks);

%// Show images
figure,
subplot(121),imshow(im),title('Before')
subplot(122),imshow(new_im),title('After')

Output -

enter image description here

Divakar
  • 218,885
  • 19
  • 262
  • 358
  • 2
    @LuisMendo Used my magic wand ;) Though cutting off the right arm was the toughest decision I made today :) – Divakar Aug 26 '15 at 10:09
  • 2
    @Benoit_11 Figured we need some MATLAB *inside jokes* ;) – Divakar Aug 26 '15 at 10:52
  • @Divakar Carefull with the wording when talking about Lena.... To use a "magic wand" in a photo that comes from the front page of a playboy magazine.... It can be.... misinterpreted :P – Ander Biguri Aug 26 '15 at 16:15
  • 1
    @AnderBiguri Don't we IP guys... "own" her more than the playboy industry! ;) – Divakar Aug 27 '15 at 05:23