MATLAB: How to write code for 3×3 sliding window to process over a Image with Pixel size of 256×256

#3x3windowMATLAB

1. Choose the first pixel. Having it as the center pixel i have to form a 3×3 window. [Being the first pixel i've padded the Matrix with Zeros]
2. If that center Pixel is Noise Pixel it's location should be returned with all its neighborhood pixels. I will convert this 3×3 Matrix into a 1-D array and do my calculations to correct the corrupted pixels.
Here i'm struggling in designing the sliding 3×3 window to move over the image

Best Answer

Hi Manoja,
I understand you want to perform a 3x3 sliding window operation on a padded image. When you encounter noise, you want to flatten the 3x3 matrix and perform corrections.
The below code performs this operation:
%A is your image
B = padarray(A,[1 1]);
for i = 2:m-1
for j = 2:n-1
if mod(B(i,j), 10) == 0 %corrupt pixel condition
mat = B(i-1:i+1,j-1:j+1); % selecting your 3x3 window
arr = reshape(mat, [1 numel(mat)]); %convert to 1-D array
B(i,j) = sum(arr);%replaced with corrections
end
end
end