Pls explain This Code Of Image Ecryption For me ???

 Accepted Answer

Geoff Hayes
Geoff Hayes on 9 Aug 2014
Edited: Geoff Hayes on 10 Aug 2014
The attached code is more of an image/pixel scrambling technique based on the Arnold Transformation. See the File Exchange for another implementation of this algorithm.
Basically, it just takes every pixel coordinate and maps it to a different coordinate using a matrix transform, and then repeats for a fixed number of iterations.
This piece of code is only valid for square grayscale images.
EDIT
See Arnold's cat map for details on this chaotic map (and on Vladimir Arnold).

11 Comments

Kindly explain why these steps ??
g=double(g)/255;
imshow(g)
%imhist(g)
x=g;
[m,n]=size(g);
a=[1,1;1,2];
g=double(g)/255;
is same as
g = im2double(g);
which is to say that it takes the input that is in the range uint8(0) to uint8(255) and converts it to be double in the range 0 to 1
imshow(g)
displays g as an image.
%imhist(g)
is a comment. It does nothing.
x=g;
copies g to x.
[m,n]=size(g);
This assigns the number of rows in g to the variable m, and assigns (number of columns times number of color planes) to n. The person who wrote the code probably intended to have n assigned the number of columns, but the code for that task would be
[m, n, ~] = size(g);
The person who wrote the code failed to take into account that JPEG files are almost always RGB images -- the rest of the code assumes that g is grayscale but it probably is not. For more information on why the call to size() was wrong, read the documentation for size()
Sorry I know this.Actually I mean what was the needs of this steps in the above code (image/pixel scrambling technique based on the Arnold Transformation)
OK, then Walter's comment did that.
If the question is why did the person who wrote that code use those lines, then you would need to ask whomever wrote the code. The lines serve little purpose. You can do a cat map efficiently on uint8 arrays as well -- you just have to remember when you are creating the output array, to make the output array the same data type as the input array.
cat map link is broken.
How to do this by changing Arnold cat map to another map
Renjith V Ravi:
"I've designed a moped here. How to do this by changing moped engine to a different engine? Oh, and I'm not going to tell you whether the new engine is from a wind-up toy, or from a '57 Chevy, or from a Wankel, or from a diesel truck, or is a Saturn V second stage."

Sign in to comment.

More Answers (1)

If what Geoff says is true, I've done that for another post. See first hidden comment in this post. The code below will work for rectangular color images.
clc;
clearvars;
close all;
workspace;
fontSize = 16;
% Read in a color demo image.
folder = fileparts(which('cameraman.tif')); % Determine where demo folder is (works with all versions).
baseFileName = 'peppers.png';
% 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
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Get the order to scramble them in
scrambleOrder = randperm(rows*columns);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Scramble according to the scrambling order.
redChannel = redChannel(scrambleOrder);
greenChannel = greenChannel(scrambleOrder);
blueChannel = blueChannel(scrambleOrder);
% Reshape into a 2D image
redChannel = reshape(redChannel, [rows, columns]);
greenChannel = reshape(greenChannel, [rows, columns]);
blueChannel = reshape(blueChannel, [rows, columns]);
% Recombine separate color channels into a single, true color RGB image.
scrambledImage = cat(3, redChannel, greenChannel, blueChannel);
% Display the scrambled color image.
subplot(2, 2, 2);
imshow(scrambledImage);
title('Scrambled Color 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.
redChannel = redChannel(newOrder);
greenChannel = greenChannel(newOrder);
blueChannel = blueChannel(newOrder);
% Reshape into a 2D image
redChannel = reshape(redChannel, [rows, columns]);
greenChannel = reshape(greenChannel, [rows, columns]);
blueChannel = reshape(blueChannel, [rows, columns]);
% Recombine separate color channels into a single, true color RGB image.
scrambledImage = cat(3, redChannel, greenChannel, blueChannel);
% Display the original color image.
subplot(2, 2, 3);
imshow(scrambledImage);
title('Unscrambled Color Image', 'FontSize', fontSize);

13 Comments

HOW CAN I GET period fo iterations where they will restore the original image arnolcAT MAP
You can either calculate it mathematically based on group theory, or you can run the iterations of the cat map in a loop, counting until the iteration brings you back to the original image.
i am working on a project "color image encryption based on chaotic maps" in which i first divide color image into rgb components and scramble each component using 2D arnold cat image ,can u please me with the code sir
All I have about Arnold's cat map is the attached m-file that I wrote. Adapt as needed. Good luck.
Please sir, how can I recover the original image after a random number of iterations?
You can't. The original image will only be returned after a very specific number of runs. After a random number of runs, unless it's the number that works, you'll be left with a scrambled looking image. That's what it's supposed to do. You can't just run it any old random number of runs and have it look like the original, but you'll get it back eventually. Did you run my demo?
"Please sir, how can I recover the original image after a random number of iterations?"
If the number of iterations is known, then it is possible to construct a reverse transform that recovers the original image in a single step.
If the number of iterations is not known, then you can take advantage of the fact that the chaotic manipulation of an image has a lot of sharp local contrasts and sharp color changes. So create a metric of how locally quickly a given version of the image is changing, and then proceed to put the image through another iteration and measure again -- and keep doing this until you are back at the image you were trying to decode. The version with the lowest measure of local disorder is probably the original image.
Image Analyst, Thank you sir for your answer. actually, your code has been executed successfully.
My question is simple: Suppose we did 10 iterations on the original image (Lena for example) using the Arnold's cat Map. how we can return Lena from this scrambled image ?
Walter Roberson, This is a good proposal. maybe I'll use correlation and entropy as metrics to evaluate the resemblance between the original image and the scrambled image. do you have any other suggestions about metrics?
You can keep running the code and using isequal(originalImage, scrambledImage) until it returns "true". Is that what you mean? Or, if you don't have the original image, run stdfilt() after each iteration and take the iteration where the std dev is min, like Walter suggested.
i want just the inverse function after N iterations to return the image original -> Simply, the inverse function of this :
N = rows;
oldScrambledImage = grayImage;
for row = 1 : rows % y
for col = 1 : columns % x
c = mod((2 * col) + row, N) + 1; % x coordinate
r = mod(col + row, N) + 1; % y coordinate
% Move the pixel. Note indexes are (row, column) = (y, x) NOT (x, y)!
currentScrambledImage(row, col, :) = oldScrambledImage(r, c, :);
end
end
I have outlined code to decode cat map in the past, if the number of iterations is given.
You create an "image" in which each location holds a value which is the linear index of the location. Put that through the known number of iterations of cat map. The result is a template for where each location moves. Now you can use
Recovered(Template) = Scrambled ;
and each pixel in Scrambled will be moved back to its proper location.

Sign in to comment.

Asked:

on 8 Aug 2014

Edited:

on 15 Feb 2018

Community Treasure Hunt

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

Start Hunting!