how to resolve error in this program
Info
This question is closed. Reopen it to edit or answer.
Show older comments
This is program of scrambling technique very simple and easy to understand algorithm but i can't get result.. this programs shows error in declaration of functions and J variable, return statment...if any one knows...give me a comment...thank you...
clear all;
close all;
clc;
image = imread('A.png');
Original_Image = im2double(image);
I = Original_Image;
key = 100;
M = 256; % size of matrix
for k = 1:key
for x = 1:M
for y = 1:M
x1 = x +y;
y1 = 2*x+y;
if x1>M
x1 = mod(x1,M);
end
if y1>M
y1 = mod(y1,M);
end
if x1==0
x1=M;
end
if y1 ==0
y1 = M;
end
J(x1,y1) = I(x,y);
end
end
I = J;
end
Scrambled_Image = I; % Scramble Image
key1= 156; % key 1 = M - key
imshow (Original_Image);
title('Original Image');
figure, imshow(Scrambled_Image);
title('Scrambled Image');
Recovered = restore(key1,M1,Scrambled_Image);
figure,imshow(Recovered);
title('Recovered Image');
nc = NC(1,2);
fprintf('Normalized Coorelation %5.4f\n:', nc); % fprintf(format space, a1)
function [Recovered]= restore(Key1,M1,Scrambled_Image)
key = key1;
M = M1;
I = Scrambled_Image;
for k =1:key
for x = 1:M
for y = 1:M
x1 = x +y;
y1 = 2*x+y;
if x1>M
x1 = mod(x1,M);
end
if y1>M
y1 = mod(y1,M);
end
if x1==0
x1=M;
end
if y1 ==0
y1 = M;
end
J(x1,y1) = I(x,y);
end
end
I = J;
end
Recovered_Image = J;
return;
Answers (2)
Guillaume
on 5 Jan 2016
The problem is fairly simple. Since you never declare J, it gets resized on iterations of your inner loop whenever x1 or y1 is greater than previous x1 or y1, respectively.
Thus, if your two inner loops never generate a x1 or y1 equal to M, at the end of the x loop, J will be smaller than I. You assign that to I, so you reduce the size of I on the next k iteration but does not change your M bound for the inner loops, so try to access elements that do not exist.
Morale of the story: always preallocate matrices. It also avoids constant resizing:
J = zeros(size(I)); %before the k loop
Other morale: Do not hardcode size of matrices. Get it from the matrix:
for x = 1:size(I, 1)
for y = 1:size(I, 2)
%... replace M by size(I, 1) or size(I, 2) depending on which dimension you're manipulating
Walter Roberson
on 5 Jan 2016
When you have a function declaration like your
function [Recovered]= restore(Key1,M1,Scrambled_Image)
then that function must be in one of:
- a file whose first non-empty non-comment line starts with "function"; or
- a file whose first non-empty non-comment line starts with "classdef"
Any .m file which does not start with one of those is referred to a "script". It is never valid to have a "function" instead of a script file.
You can move the "function" to the end of the file to another file named the same thing as the function name but ending in .m -- restore.m in this case. Or you can transform your script into a function file.
To transform a script into a function file, delete the lines
clear all;
close all;
clc;
from the top of the file, and insert a line
function NameOfTheFileGoesHere
where NameOfTheFileGoesHere is the same name as the file you are storing the code in. For example it might be
function assign4d
if the code is in assign4d.m
When your file does start with "function" then you can have any number of "function" lines in the same file.
This question is closed.
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!