MATLAB: Averaging image array values

image processing

I am trying to create an average filter to blur an image. I have my image array which has been padded stored in Y. Below is a section of my code trying to average individual red pixel values with its eight nearest neighbors. After running the code I get the error reading "Cannot convert double value 150 to a handle" I understand that something is wrong but I must compute the average manually in a similar fashion to the code below. (I cannot use a built in function to apply the filter.) Thanks in advance
Y = cat(3, newRed, newGreen, newBlue); %Re-stitches the RGB parts together [r,c,p]=size(Y) rows=r; columns=c; planes=p;
for i=2:r-1
for j=2:c-1
Red(1,i,j)=Y(1,mean2(i-1:i+1),mean2(j-1:j+1))
end
end

Best Answer

I hope you are aware that images in Matlab generally have the following structure: (row,column,color channel). If I were you I would take a look at the solution someone else presented . It sounds like you have the same homework assignment. If it is indeed the same assignment, you should replace that inner loop command with Red(i,j,1)=mean2(Y((i-1):(i+1),(j-1):(j+1),1);
You should give more of the code that is actually causing the error if you want other to be able to troubleshoot that one.