To RESHAPE the number of elements must not change?

3 views (last 30 days)
here is the full code of the program:
%%Main Function
function ImageSharing
clear; close all;
Height = 256;
Width =256;
t = 4;
w = 8;
Users = [1,3,5,8];
base = 257;
Sub_width = Width/t^(0.5);
Sub_height = Height/t^(0.5);
if(length(Users)<t)
error('invalid length of Users');
end;
if(mod(Sub_height,1) ~= 0)
error('invalid length of Sub height');
end;
Im_Name = 'i.bin';
[Im_linear, Im_Square] = ReadImage(Im_Name,Height,Width);
P_Im = GetPreparedIm(Im_linear,t);
Shadow_Image = GetShadow_Image(P_Im,w,base);
a = find(Shadow_Image>256);
ShowShadowImage(Shadow_Image,Sub_width,Sub_height);
RequiredShadowImage = GetRequiredShadowImage(Shadow_Image,Users);
% RequiredShadowImage = AddNoise(RequiredShadowImage);
RecoveredImage = RecoverImage(RequiredShadowImage,Users,Height,Width,base);
Err = sum(RecoveredImage(:) - Im_linear);
disp(sprintf('Error bit: %d',Err));
figure;
imshow(uint8(RecoveredImage));
title('Recovered Image');
%%function RequiredShadowImage = AddNoise(RequiredShadowImage);
function RequiredShadowImage = AddNoise(RequiredShadowImage)
TT = size(RequiredShadowImage);
Err = round(rand(TT(1),TT(2)));
Err(Err == 0) = -1;
RequiredShadowImage = RequiredShadowImage + int32(Err);
%%function RecoveredImage = RecoverImage(RequiredShadowImage)
function RecoveredImage = RecoverImage(RequiredShadowImage,Users,Height,Width,base)
TT = size(RequiredShadowImage);
xx = ones(TT(2),TT(2));
for i = 1:TT(2)
xx(:,i) = xx(:,i).*(Users.^(i-1))';
end;
for i = 1:TT(1)
if(mod(i,100)==0)
disp(sprintf('%d of %d',i/100,floor(TT(1)/100)));
end;
RecoveredImage(i,:) = solveEq(xx,RequiredShadowImage(i,:),base);
end;
RecoveredImage = reshape(RecoveredImage(:),Height,Width)';
%%function b = solveEq(Users,RequiredShadowImage(i,:),TT(2),base);
function b = solveEq(xx,yy,base)
b = GetIntMod(inv(sym(xx))*yy',base)';
%b = inv(sym(xx))*yy';
%%function Re_b = GetIntMod(b,base)
function Re_b = GetIntMod(b,base)
[n,d] = numden(b);
n = double(n);
d = double(d);
Re_b = mod(n.*powermod(d,-1,base),base);
%%function RequiredShadowImage = GetRequiredShadowImage();
function RequiredShadowImage = GetRequiredShadowImage(Shadow_Image,Users)
TT = length(Users);
for i = 1:TT
RequiredShadowImage(:,i) = Shadow_Image(:,Users(i));
end;
%%ShowShadowImage
function ShowShadowImage(Shadow_Image,Sub_width,Sub_height)
TT = size(Shadow_Image);
for i =1:TT(2)
figure;
imshow(uint8(reshape(Shadow_Image(:,i),Sub_width,Sub_height)));
cmd = sprintf('title(''Shadow Image %d\'')',i);
eval(cmd);
end;
%%get Sharing_Polynomial
function Shadow_Image = GetShadow_Image(P_Im,w,base)
TT = int32(size(P_Im));
P_Im = int32(P_Im);
w=int32(w);
base = int32(base);
Shadow_Image = int32(zeros(TT(1),w));
for i=1:w
for j = 1:TT(2)
Shadow_Image(:,i) = Shadow_Image(:,i) + mod(P_Im(:,j)*i^(j-1), base);
end;
end;
Shadow_Image = mod(Shadow_Image,base);
%%get PreparedIm
function P_Im = GetPreparedIm(Im_linear,t)
if(mod(length(Im_linear),t) ~= 0)
error('invalid length')
end;
P_Im = reshape(Im_linear,[],t);
%%Read Image
function [Im_data, Im]=ReadImage(Im_Name,Height,Width)
Im_fp = fopen(Im_Name,'rb');
Im_data = fread(Im_fp,'uint8');
h = figure; % create a new figure
Im = uint8(reshape(Im_data,Height,Width));
imshow(Im);
title('Original Image');
function y = powermod(a,z,n)
% This function calculates y = a^z mod n
% If a is a matrix, it calculates a(j,k)^z mod for every element in a
[ax,ay]=size(a);
% If a is negative, put it back to between 0 and n-1
a=mod(a,n);
% Take care of any cases where the exponent is negative
if (z<0),
z=-z;
for j=1:ax,
for k=1:ay,
a(j,k)=invmodn(a(j,k),n);
end;
end;
end;
for j=1:ax,
for k=1:ay,
x=1;
a1=a(j,k);
z1=z;
while (z1 ~= 0),
while (mod(z1,2) ==0),
z1=(z1/2);
a1=mod((a1*a1), n);
end; %end while
z1=z1-1;
x=x*a1;
x=mod(x,n);
end;
y(j,k)=x;
end; %end for k
end; %end for j
function y = invmodn( b,n);
% This function calculates the inverse of an element b mod n
% It uses the extended euclidean algorithm
n0=n;
b0=b;
t0=0;
t=1;
q=floor(n0/b0);
r=n0-q*b0;
while r>0,
temp=t0-q*t;
if (temp >=0),
temp=mod(temp,n);
end;
if (temp < 0),
temp= n - ( mod(-temp,n));
end;
t0=t;
t=temp;
n0=b0;
b0=r;
q=floor(n0/b0);
r=n0-q*b0;
end;
if b0 ~=1,
y=[];
disp('No inverse');
else
y=mod(t,n);
end;
Here is the error:
??? Error using ==> reshape
To RESHAPE the number of elements must not change.
Error in ==> ImageSharing>ReadImage at 127 Im = uint8(reshape(Im_data,Height,Width));
Error in ==> ImageSharing at 24 [Im_linear, Im_Square] = ReadImage(Im_Name,Height,Width); Whenever i run this with an image i get the error in the reshape statement. Please help.
  3 Comments
Imdad Choudhury
Imdad Choudhury on 8 Jun 2016
Here it is.
??? Error using ==> reshape To RESHAPE the number of elements must not change.
Error in ==> ImageSharing>ReadImage at 127 Im = uint8(reshape(Im_data,Height,Width));
Error in ==> ImageSharing at 24 [Im_linear, Im_Square] = ReadImage(Im_Name,Height,Width);
Stephen23
Stephen23 on 9 Jun 2016
Perhaps there might be some confusion between reshape and resizing the image... note that reshape does not change how many array elements there are (you can think of this as the number of pixels). If you want to change how many pixels your image has (e.g. to make any image have 255x255 pixels), then reshape is the wrong tool for you: correct would be imresize.

Sign in to comment.

Answers (1)

dpb
dpb on 8 Jun 2016
All the code is superfluous and a red herring to wade through excepting for the function containing the error..
function [Im_data, Im]=ReadImage(Im_Name,Height,Width)
Im_fp = fopen(Im_Name,'rb');
Im_data = fread(Im_fp,'uint8');
h = figure; % create a new figure
Im = uint8(reshape(Im_data,Height,Width));
You've passed in an x- and y-dimension which together define the precise number of elements but you read a file which can be any dimension and then try to reshape it to that exact size. If they're not the same, this is going to fail (which it did in your case).
Use debugger and step through to see what
size(Im_data)
returns, unfortunately you're going to find that numel(Im_data) is not going to be Height*Width
I can't tell you what you want to do to compensate for the difference in sizes, but that's your error.
  16 Comments
Imdad Choudhury
Imdad Choudhury on 9 Jun 2016
Edited: Stephen23 on 9 Jun 2016
i tried all of these but it didn't solve the problem. the error then shifts to the other reshape lines.. i got the code from here http://in.mathworks.com/matlabcentral/fileexchange/39660-secret-image-sharing-scheme The author isn't available so i had to ask here.
aisha khan
aisha khan on 14 Feb 2020

I also need help with this code...i am stuck at the last part..what does the function powermod and invmodn do..is it related to overflow prevention?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!