How can I unscramble a scrambled image?

Hi! I'm just a beginner. I scrambled an image using the following source.
if true
---------- scramble an 512*512 image as 8 by 8 blocks---------------------
I = imread('lena_org.bmp');
row = size(I, 1) / 64;
col = size(I, 2) / 64;
I_scramble = mat2cell(I, ones(1, row) * 64, ones(1, col) * 64, size(I, 3));
I_scramble = cell2mat(reshape(I_scramble(randperm(row * col)), row, col));
imshow(I_scramble);
---------------------------------------------------------------------------
end
Anyone knows how to unscramble the image as it was? I spent a lot of time solving the problem, but I am going around in circles. Thanks a lot for your help in advance!

 Akzeptierte Antwort

harjeet singh
harjeet singh am 19 Dez. 2015
Bearbeitet: Image Analyst am 10 Jun. 2022
Dear @eulerphi, use this code:
I = imread('Lena512.bmp');
row = size(I, 1) / 64;
col = size(I, 2) / 64;
I_scramble = mat2cell(I, ones(1, row) * 64, ones(1, col) * 64, size(I, 3));
a=randperm(row * col);
I_scramble = cell2mat(reshape(I_scramble(a), row, col));
figure(1)
imshow(I_scramble);
drawnow
b=1:length(a);
com=[a' b'];
com=sortrows(com,1);
I_scramble = mat2cell(I_scramble, ones(1, row) * 64, ones(1, col) * 64, size(I, 3));
I_scramble = cell2mat(reshape(I_scramble(com(:,2)), row, col));
figure(2)
imshow(I_scramble);
drawnow

3 Kommentare

eulerphi
eulerphi am 21 Dez. 2015
I deeply appreciate your help! That's what I exactly wanted! Thank you! :)
BHAGYASHREE HEGDE
BHAGYASHREE HEGDE am 10 Jun. 2022
Can you please tell what is 64 here in this example??
@BHAGYASHREE HEGDE the 64 is so that you get exactly 8 blocks vertically and horizontally.
numBlocks = 512/64
numBlocks = 8

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 19 Dez. 2015

0 Stimmen

See my attached demo that scrambles an image element by element. You could adapt it to scramble a cell array element by element also.

4 Kommentare

eulerphi
eulerphi am 21 Dez. 2015
Thanks a lot for your help! Your code seems so nice for me to study. Thank you for your help! :)
hayat ali
hayat ali am 25 Nov. 2018
thank you for your efforts
can you tell me how to scramble the grayscale image? Itried this code in below but did not get a picture of scramble and unscramble. they are apear as a white image.
please help me to correct this code
the code :
clc
close all;
rgbImage = imread('pepper.bmp');
I=rgb2gray(rgbImage);
[row col]=size(I);
%figuer
subplot(2, 2, 1);
imshow(I)
% Get the order to scramble them in
scrambleorder=randperm(row*col);
% Scramble according to the scrambling order.
b=scrambleorder;
% Reshape into a 2D image
sec_b=reshape(b,[row,col]);
% Display the scrambled color image.
subplot(2, 2, 2);
imshow(sec_b);
% Recover the image, knowing the sort order
recoverOrder = zeros([row*col], 2);
recoverOrder(:, 1) = 1 : (row*col);
recoverOrder(:, 2) = b;
% Sort this to find out where each scrambled location needs to be sent to.
newOrder = sortrows(recoverOrder, 2);
% Extract just column 1, which is the order we need.
newOrder = newOrder(:,1);
% Unscramble according to the recoverOrder order.
b = b(newOrder);
% Reshape into a 2D image
b = reshape(b, [row, col]);
% Display the original color image.
subplot(2, 2, 3);
imshow(b);
See code altered to work on a gray scale image.
% Demo to use randperm() to scramble the pixel locations in a gray scale image, then undo the scrambling to obtain the original image again.
clc;
clearvars;
close all;
workspace;
fontSize = 16;
% Read in a gray scale demo image.
folder = fileparts(which('cameraman.tif')); % Determine where demo folder is (works with all versions).
baseFileName = 'cameraman.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
grayImage = rgb2gray(grayImage); % Convert to gray level.
end
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage);
title('Original Gray Scale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Get the order to scramble them in
scrambleOrder = randperm(rows*columns);
% Scramble according to the scrambling order.
grayImage = grayImage(scrambleOrder);
% Reshape into a 2D image
scrambledImage = reshape(grayImage, [rows, columns]);
% Display the scrambled gray scale image.
subplot(2, 2, 2);
imshow(scrambledImage);
title('Scrambled Gray Scale Image', 'FontSize', fontSize);
% Recover the image, knowing the sort order
recoverOrder = zeros([rows*columns], 2);
recoverOrder(:, 1) = 1 : (rows*columns);
recoverOrder(:, 2) = scrambleOrder;
% Sort this to find out where each scrambled location needs to be sent to.
newOrder = sortrows(recoverOrder, 2);
% Extract just column 1, which is the order we need.
newOrder = newOrder(:,1);
% Unscramble according to the recoverOrder order.
grayImage = grayImage(newOrder);
% Reshape into a 2D image
scrambledImage = reshape(grayImage, [rows, columns]);
% Display the original gray scale image.
subplot(2, 2, 3);
imshow(scrambledImage);
title('Unscrambled Gray Scale Image', 'FontSize', fontSize);
0000 Screenshot.png
hayat ali
hayat ali am 26 Nov. 2018
thank you very much. I wish you success.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by