I have an image(512x512) and i want to do zero-padding in order to covolute it with a filter . The problem here is that i dont know how to do that ,meaning that i dont know where the zeros should be (around the image or next to it) and furthermore the size of the zero-padding.
Prashant Kumar answered .
2025-11-20
There is a built-in function for that. It's called padarray().
Demo
grayImage = imread('cameraman.tif');
whos grayImage % 256 x 256
windowSize = 3; % Filter window's full width.
% Compute the number of layers of pixels with value 0 to surround the image with.
numLayers = floor(windowSize/2) % Will be 1
grayImage2 = padarray(grayImage, [numLayers, numLayers]); % Create new image, or use img if you want to replace the image instead of creating a new one.
whos grayImage2 % Will be 258 x 258M