MATLAB: I’m working with image ,i cant load 512*512 image

image

all time i face a error
Attempted to access N(1,1,3); index out of bounds because size(N)=[256,256,1].my code is here:
function [mg_length,mes] = embed_file(image_file1,text_file1)
clc
M=imread(image_file1); [X,Y,V] = size(M);
disp(sprintf('%d %d %d',X,Y,V));
%N=rgb2gray(M);
N=M;
T=N;
str={};
z='';
count=0;
fid = fopen(text_file1);
tline = fgetl(fid);
while ischar(tline)
if(length(tline)==0)
z=[z,char(dec2bin(13,8))];
z=[z,char(dec2bin(10,8))];
else
str=[str,tline];
t= dbinary(str);
if(count~=0)
z=[z,char(dec2bin(13,8))];
z=[z,char(dec2bin(10,8))];
end
z=[z,t];
end
count=count+1;
tline = fgetl(fid);
str='';
end
mes='';
disp(z); %disp(N);
imageSize=X*Y;
key=length(z);
mg_length=key;
disp('message length');
disp(key);
if(imageSize>key)
mes='embed successful';
d=1;
for i=1:X
for j=1:Y
a=N(i,j,3);
b=z(1,d);
[T(i,j,3),p]=changeImage2(a,b,key);
%a=N(i,j,2);
%[T(i,j,2),p]=changeImage2(a,b,length_Z);
%a=N(i,j,3);
%[T(i,j,3),p]=changeImage2(a,b,length_Z);
if(d==key)
break;
end
if(p==1)
d=d+1;
end
end
if(d==key)
break;
end
end
how can i fix it ?

Best Answer

Farjana - the error message is occurring because of the line of code
a=N(i,j,3);
which is trying to access the third dimension of the matrix N when it only has two dimensions (your input image is grayscale, 256x256, and not colour, 256x256x3). Either change the line of code to
a=N(i,j);
or select a colour image.