如何打乱图像中的特定元素?
How to scramble specific elements within an image?
尽管我能够成功地打乱图像(使用 JigSaw
或 RANDBLOCK
),但我不知道如何打乱每个图像中的特定矩阵元素(块)。我无法附加图像,所以我需要的正是以图像形式 here 显示的(只需点击 'scramble')。正如您将看到的,第一张图像被划分为 8 x 7
矩阵,然后特定元素被打乱以产生第二张图像,即
[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]
我将非常感谢任何建议,因为我是 MATLAB 的新手,需要尽快完成刺激!
非常感谢,
玛丽亚
我更喜欢Lena
,所以我们开始吧-
%// 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')
输出-
尽管我能够成功地打乱图像(使用 JigSaw
或 RANDBLOCK
),但我不知道如何打乱每个图像中的特定矩阵元素(块)。我无法附加图像,所以我需要的正是以图像形式 here 显示的(只需点击 'scramble')。正如您将看到的,第一张图像被划分为 8 x 7
矩阵,然后特定元素被打乱以产生第二张图像,即
[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]
我将非常感谢任何建议,因为我是 MATLAB 的新手,需要尽快完成刺激!
非常感谢,
玛丽亚
我更喜欢Lena
,所以我们开始吧-
%// 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')
输出-