MATLAB: Mean of neighbor pixel

mean intensity valueneighbor pixel

Hi guys,
I try to calculate the average intensity value of the neighbors of a pixel(either 8 or not). I am confused with the way matlab uses matrices(i-th,j-th position of a pixel is at j-th,i-th position of the matrix) and I have difficulties find the neighbors.
Thanks

Best Answer

Just use convolution,
k=[1 1 1; 1 0 1; 1 1 1]/8;
averageIntensities = conv2(double(yourImage),k,'same');