kyosuke ng
kyosuke ng

Reputation: 13

Matlab- zero padding

How to do this on matlab?

zero pad the face image with a five‐pixel thick rim around the borders of the image. show the resulting image.

Must be manual codes on script.

Upvotes: 0

Views: 6332

Answers (3)

Amro
Amro

Reputation: 124553

I realize you want to code this yourself, but for reference, you can use the PADARRAY function. Example:

I = imread('coins.png');
II = padarray(I,[5 5],0,'both');
imshow(II)

screenshot

Note this works also for multidimensional matrices (RGB images for example)

Upvotes: 1

memyself
memyself

Reputation: 12618

save this function as create_padded_image.m

function padded_image = create_padded_image(image, padding)

if nargin < 2
    % if no padding passed - define it.
    padding = 5;
end

if nargin < 1
    % let's create an image if none is given
    image = rand(5, 4)
end

% what are the image dimensions?
image_size = size(image);


% allocate zero array of new padded image
padded_image = zeros(2*padding + image_size(1), 2*padding + image_size(2))

% write image into the center of padded image
padded_image(padding+1:padding+image_size(1), padding+1:padding+image_size(2)) = image;

end

Then call it like this:

% read in image - assuming that your image is a grayscale image
$ image = imread(filename);
$ padded_image = create_padded_image(image)

Upvotes: 1

Hannes Ovr&#233;n
Hannes Ovr&#233;n

Reputation: 21831

This sounds like homework, so I will just give you a hint:

In MATLAB it is very easy to put the content of one matrix into another at precisely the correct place. Check out the help for matrix indexing and you should be able to solve it.

Upvotes: 1

Related Questions