Search code examples
imagematlabscramble

How to scramble specific elements within an image?


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


Solution

  • 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