MATLAB: Pixel location to intensity value

Image Processing Toolboxintensity valueneighborspixel location

I'm trying to create a algorithm that calculates the mean intensity of a given pixel and it's neighbors, i use the script beneath to select a slice and pixel in my volume and build a matrix with the pixel and neighbor locations. Can anybody help me to translate this location to an intensity value?
if true
% Slicenumber = round(size(MRI1 ,3)/2);
figure, imshow (MRI1(:,:,Slicenumber),[]);
uiwait(msgbox('Select a point'))
[x,y]=getpts;
Position(1) = round(double(x(1)));
Position(2) = round(double(y(1)));
Position(3) = Slicenumber;
a = [ 1 0 0; -1 0 0; 1 1 0; 0 1 0; -1 1 0; 1 -1 0; 0 -1 0; -1 -1 0];
neighbors = a+repmat(Position,[8 1]);
end

Best Answer

Use convolution:
grayImage = double(MRI1(:,:,Slicenumber));
kernel = ones(3)/9;
neighborhoodMeans = conv2(grayImage, kernel, 'same');
You could also use imfilter().
Related Question