I need this code working. I am not able to figure out the problem.

Hello Everyone This code is an chaotic Image Encryption Code. I am Not able to Run this code Because it is giving errors. I am attaching a rar Matlab file. Pls can someone correct it for me.. Thanks in advance

 Accepted Answer

Yagyesh - running the code produces the following error message
Index exceeds matrix dimensions.
Error in encryptImage (line 36)
B=I(:,:,2);
encryptImage is what I named the function to do the encryption. Putting a breakpoint at the first line, and running the function, stops at line
I = imread('lena.bmp');
Step past this line and the problem is clear. I is a two-dimensional 256x256 matrix of 8-bit unsigned integers. The lena.bmp is a black and white image, with no red, green or blue channels. Once the code reaches
A=I(:,:,1);
B=I(:,:,2);
C=I(:,:,3);
A is assigned correctly since I is 256x256 (or 256x256x1), but the two lines fail after that.
It would appear that the code has been written with the expectation of handling images with three dimensions.
I suggest that you try with a different image (one that has three dimensions), or adjust the code to handle the case where ndims(I)==2 (and so avoid all B and C "channel" work).

5 Comments

Thanks Your answer solved my problem..Now I am Having a second Problem. Image is not decrypting as original Image. Any Help
Yagyesh - Rather than posting a zip file, please just attach the relevant files using the paperclip button.
As well, please describe your changes to the code that would prevent the image from being decrypted. I was able to decrypt any colour image with the original code, so you must have modified one of the two files. Have you stepped through the code to see what could be going wrong? Is there an error message? Have you restored the code to its original state?
This is my code. I haven't changed anything else Just file names. I am getting This output
Yagyesh - no, you didn't change any of the code, just the format of the input file from a bitmap (uncompressed image) to a jpeg (with lossy compression). Stepping through the code, at roughly lines 58-64
title('encrypted image');
imwrite(I,'a1.jpg');
drawnow;
% ¶ÔͼÏñ½øÐнâÃÜ
I1=imread('a1.jpg');
The encrypted data from I is written to the al.jpg file and then read back in to I1. If you compare the two
Z = I==I1;
isempty(Z) % returns false indicating differences!
So that which we wrote to file is not that which we read in...because of the jpeg format.
So either you stick with bitmap files (or other uncompressed image formats) or, if you decide to use jpeg, then just write the data as
imwrite(I,'a1.jpg','mode','lossless');
You are awesome. Thank You very much

Sign in to comment.

More Answers (0)

Asked:

on 5 Aug 2014

Commented:

on 7 Aug 2014

Community Treasure Hunt

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

Start Hunting!