Averaging image array values

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

 Accepted Answer

Rik
Rik on 21 Feb 2017
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.

2 Comments

thank you. I got the script to run but it is return an image of a single color (white) its probably an error in conversion but I can't seem to figure out where or why. Please note that the function expand matrix is a user defined function and I am fairly confident thatis not where the error lies. Thanks
if true
% code
end
X = imread('4.2.03.tiff'); %Reads in the image file
X = double(X); %Coverts the image data
redValues = X(:,:,1); %Extracts the red values from the matrix
greenValues = X(:,:,2); %Extracts the green values from the matrix
blueValues = X(:,:,3); %Extracts the blue values from the matrix
newRed = expandmatrix(redValues); %Calls function 'expand' to increase the dimensions on either side by one
newGreen = expandmatrix(greenValues); % ''' ''
newBlue = expandmatrix(blueValues); % '' '' '
Y =cat(3, newRed, newGreen, newBlue); %Re-stitches the RGB parts together
[r,c,p]=size(Y);
for v=1:p
for i=2:c-1
for j=2:r-1
Y2(i,j,v)=mean2(Y((i-1):(i+1),(j-1):(j+1),1));
end
end
end
blurredimage=Y2(2:r-1,2:c-1,3);
X1=im2uint8(blurredimage)
imshow(X1);
You should either use im2double(X), or rescale yourself (X=double(x)/255;). The problem you see is that im2uint8 assumed a scale of [0 1], which it scaled up to [0 255]. Your data was already that, so it scaled to [0 255*255], but uint8 can't hold that, so it clipped all values.

Sign in to comment.

More Answers (0)

Asked:

on 21 Feb 2017

Commented:

Rik
on 21 Feb 2017

Community Treasure Hunt

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

Start Hunting!