Reanimation
Reanimation

Reputation: 3336

Adding Padding to Image - Matlab

I have a small image that is 89x56px in size and is RGB.

I'm trying to add padding around the image until both (x,y) are greater than 64px.

I've tried this by reading this question: but with no luck:

  img = subImage{1};              %small image 89x56
  new(size(subImage{1},1),64)=0;  %zero matrix for padding
  size(new);
  merged = img;                   %also tried adding img to new
  imshow(merged)

Ideally I would like even padding each side of the image. eg 64 - 56 = 8; so 4 columns of 0 each side (or just 8 on the end if it's too difficult.)

Any help would be appreciate. Thanks in advance.

Upvotes: 2

Views: 2877

Answers (2)

Divakar
Divakar

Reputation: 221584

Code

limit = 64; %%// Padding limit

[sz1 sz2 C] = size(img);
ad1 = round((limit - min([sz1 sz2]))/2);
img1 = uint8(zeros(sz1+2*ad1,sz2+2*ad1,3));
img1(ad1:ad1+sz1-1,ad1:ad1+sz2-1,:) = img;

figure,imshow(img1)

The code assumes that you want to pad equally on left and right and at top and bottom.

Upvotes: 0

Notlikethat
Notlikethat

Reputation: 20934

Since you have imshow, you should have padarray, too:

pad = [64 64 0] - size(img);
pad(pad<0) = 0;
merged = padarray(img, floor(pad./2));

Upvotes: 7

Related Questions