This might be what you are after -
Approach 1: Using nlfilter
for sliding-neighborhood operations
fun1 = @(x) sum([x(2,2) x(3,2) x(2,3)]);
OUTPUT_IMG = nlfilter(INPUT_IMG, [3 3], fun1);%%// INPUT_IMG is a gray image
Approach 2: Using direct summations by padding zeros with padarray
to match matrix sizes
southNeighbors = padarray(INPUT_IMG(2:end,1:end),[1 0],'post');
eastNeighbors = padarray(INPUT_IMG(1:end,2:end),[0 1],'post');
OUTPUT_IMG = INPUT_IMG + eastNeighbors + southNeighbors;
General Notes:
If you are looking for interpolation
, maybe you would want to find mean of those three pixels, so divide by 3
. Take care of the uint8
datatype as it will clip off values at 255
when summing up.
Also, you might want to keep the last row and column same as in the original image, as they don't have both east and south neighboring pixels, so use this -
OUTPUT_IMG(:,end) = INPUT_IMG(:,end);
OUTPUT_IMG(end,:) = INPUT_IMG(end,:);