开发者

Matlab- zero padding

开发者 https://www.devze.com 2023-04-08 17:53 出处:网络
How to do this on matlab? zero pad the face image with a five‐pixel thick rim around the borders of the

How to do this on matlab?

zero pad the face image with a five‐pixel thick rim around the borders of the image. s开发者_高级运维how the resulting image.

Must be manual codes on script.


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)


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.


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)

Matlab- zero padding

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号