Why this program generates error..?

1 view (last 30 days)
Nimisha
Nimisha on 24 Sep 2014
Edited: per isakson on 24 Sep 2014
function out_put = program(in_put,block_size)
X=imread(in_put);
Y=imfinfo(in_put);
K=block_size;
X1=double(X);
y1=size(X);
n=y1(1);
m=y1(2);
k=1;l=1;
for i=1:K:n
for j=1:K:m
tmp([1:K],[1:K])=X1([i:i+(K-1)],[j:j+(K-1)]);
mn=mean(mean(tmp));
tmp1([i:i+(K-1)],[j:j+(K-1)])=tmp>mn;
Lsmat=(tmp<mn);
Mrmat=(tmp>=mn);
Lsmn=sum(sum(Lsmat));
Mrmn=sum(sum(Mrmat));
Mu(k)=sum(sum(Lsmat.*tmp))/(Lsmn+.5);k=k+1;
Mi(l)=sum(sum(Mrmat.*tmp))/Mrmn;l=l+1;
end
end
subplot(132);
imshow(tmp1);
title('ENCODED');
% IMAGE DECODING
k=1;l=1;
for i=1:K:n
for j=1:K:m
tmp21([1:K],[1:K])=tmp1([i:i+(K-1)],[j:j+(K-1)]);
tmp22=(tmp21*round(Mu(k)));k=k+1;
tmp21=((tmp21==0)*round(Mi(l)));l=l+1;
tmp21=tmp21+tmp22;
out_put([i:i+(K-1)],[j:j+(K-1)])=tmp21;
end
end
subplot(133),imshow(uint8(out_put));title('DECODED');
% FOR COLORED IMAGES
R=X(:,:,1);
G=X(:,:,2);
B=X(:,:,3);
% IMAGE ENCODING
subplot(131),imshow(X),title('ORIGINAL');
for b=1:3
for i=1:K:n
for j=1:K:m
tmp([1:K],[1:K])=X1([i:i+(K-1)],[j:j+(K-1)],b);
mn=mean(mean(tmp));
tmp1([i:i+(K-1)],[j:j+(K-1)],b)=tmp>mn;
Lsmat=(tmp<mn);
Mrmat=(tmp>=mn);
Lsmn=sum(sum(Lsmat));
Mrmn=sum(sum(Mrmat));
Mu(b,k)=sum(sum(Lsmat.*tmp))/(Lsmn+.5);k=k+1;
Mi(b,l)=sum(sum(Mrmat.*tmp))/Mrmn;l=l+1;
end
end
subplot(132),imshow(tmp1);title('ENCODED');
% IMAGE DECODING
k=1;l=1;
for b=1:3
for i=1:K:n
for j=1:K:m
tmp21([1:K],[1:K])=tmp1([i:i+(K-1)],[j:j+(K-1)]);
tmp22=(tmp21*round(Mu(b,k)));k=k+1;
tmp21=((tmp21==0)*round(Mi(b,l)));l=l+1;
tmp21=tmp21+tmp22;
out_put([i:i+(K-1)],[j:j+(K-1)],b)=tmp21;
end
end
end
subplot(133),imshow(uint8(out_put));title('DECODED');
errordlg('IMAGE TYPE NOT SUPPORTED');
end
end
I have this program. when i run it for attached image, it generates error, as well it gives gray image as output. I required coloured.
What modification is required..?
  1 Comment
per isakson
per isakson on 24 Sep 2014
Edited: per isakson on 24 Sep 2014
Experiment a bit with the markup button {}Code. With the help of the "paper-clip-button" both the code and the image can be attached. There is also the ? Help button.

Sign in to comment.

Answers (1)

Geoff Hayes
Geoff Hayes on 24 Sep 2014
Nimisha - it would be helpful to include what this program is supposed to do, what block size you are using (second input to this program), and what is the error message?
If I run the code as
>> program('scene.jpg',20);
I get the following error
Attempted to access Mu(2,4801); index out of bounds because size(Mu)=[1,9600].
Error in program (line 66)
tmp22=(tmp21*round(Mu(b,k)));k=k+1;
The error message is clear - the code is trying to access Mu(2,4801) in an array that is of dimension 1x9600. Look closely at the following code which does the image encoding for a colour image
% IMAGE ENCODING
subplot(131),imshow(X),title('ORIGINAL');
for b=1:3
for i=1:K:n
for j=1:K:m
tmp([1:K],[1:K])=X1([i:i+(K-1)],[j:j+(K-1)],b);
mn=mean(mean(tmp));
tmp1([i:i+(K-1)],[j:j+(K-1)],b)=tmp>mn;
Lsmat=(tmp<mn);
Mrmat=(tmp>=mn);
Lsmn=sum(sum(Lsmat));
Mrmn=sum(sum(Mrmat));
Mu(b,k)=sum(sum(Lsmat.*tmp))/(Lsmn+.5);k=k+1;
Mi(b,l)=sum(sum(Mrmat.*tmp))/Mrmn;l=l+1;
end
end
subplot(132),imshow(tmp1);title('ENCODED');
% IMAGE DECODING
k=1;l=1;
for b=1:3
% etc.
The outer for loop iterates from 1 to 3 using the index variable b. There is a subsequent for loop with its own inner loop, and then we attempt to plot the encoded image before starting on the image decoding...which starts iterating from 1 to 3 using the index b. So we have yet to complete the first for loop for the image encoding before we start on the decoding with the same index b! You must correctly terminate the first for loop of the encoding before starting the decoding
% IMAGE ENCODING
subplot(131),imshow(X),title('ORIGINAL');
for b=1:3
for i=1:K:n
for j=1:K:m
tmp([1:K],[1:K])=X1([i:i+(K-1)],[j:j+(K-1)],b);
mn=mean(mean(tmp));
tmp1([i:i+(K-1)],[j:j+(K-1)],b)=tmp>mn;
Lsmat=(tmp<mn);
Mrmat=(tmp>=mn);
Lsmn=sum(sum(Lsmat));
Mrmn=sum(sum(Mrmat));
Mu(b,k)=sum(sum(Lsmat.*tmp))/(Lsmn+.5);k=k+1;
Mi(b,l)=sum(sum(Mrmat.*tmp))/Mrmn;l=l+1;
end
end
end % <---- THIS END WAS ADDED
subplot(132),imshow(tmp1);title('ENCODED');
% IMAGE DECODING
k=1;l=1;
for b=1:3
% etc.
You will have to remove an end from the last line of the file to compensate for the one that was inserted above.
That will fix the error and allow you to proceed.
As for the image appearing in grayscale - there are two blocks in this code: one which considers the input image in grayscale (and so ignores the colour dimension) and one which considers the image as in colour. Upon completion of the second block (once you fix the bug) you will notice the encoded image and its decoded equivalent...which will be in red only. You will have to review the code more closely, adding comments to it, to see where it went wrong.

Community Treasure Hunt

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

Start Hunting!