Maroun
Maroun

Reputation: 96018

Matlab padarray function

I have an image of size 350X450. I'm trying to pad with zeros the matrix which represents the image in a way that I'll have the original matrix in the center of the new padded matrix with a new dimensions of 700X900. Here is what I want to do:

Original image and desired image

I'm trying to implement that using padarray function:

(Suppose w is the desired width, h is the desired height and im is the image(matrix))

new_image=paddarray(im, [0.5*w 0.5*h]);

I don't get the desired result. What am I missing? Is there a better way to do this?

Upvotes: 2

Views: 1795

Answers (2)

Pete
Pete

Reputation: 2344

As the HELP entry says:

B = padarray(A,PADSIZE)

pads array A with PADSIZE(k) number of zeros along the k-th dimension of A.

padarray([1 2; 3 4],[1 1]) %makes a 4x4 matrix

You don't want to pad with w and h, you want to pad with

(wDesired - wCurrent)/2 %floor or ceil, depending on your mood.

Upvotes: 1

Acorbe
Acorbe

Reputation: 8391

Your syntax is right, you should set w = ceil((700-350)/2) and h = ceil((900-450)/2).

Upvotes: 3

Related Questions