How can i use zero-padding in an image?

T
Travis · Oct 16, 2020 · 2.2K views
Question
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.
Expert Answer
Profile picture of Prashant Kumar
Prashant Kumar PhD Expert
Answered Aug 23, 2026

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
100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!