Filling an image in MATLAB

I have an image in size 61x56 and I want to put the image in size 392x392.

I am trying to use padarray , but since I am getting a non-integer value, I cannot do this. Can anyone help me with this. Thank you very much! I have attached what I want to do below.

 K = imread('test.jpg'); K = rgb2gray(K); [mn] = size(K); p = 392; q = 392; K_pad = padarray(K, [(pm)/2 (qn)/2], 'replicate'); 
+7
source share
4 answers

You can split the padarray into two calls:

 K_pad = padarray(K, [floor((pm)/2) floor((qn)/2)], 'replicate','post'); K_pad = padarray(K_pad, [ceil((pm)/2) ceil((qn)/2)], 'replicate','pre'); 

But you can check what happens in the corners of the image to see if it is normal to do with it.

+5
source

Here is another way to complement it without using padarray .

 imgSize=size(img); %#img is your image matrix finalSize=392; padImg=zeros(finalSize); padImg(finalSize/2+(1:imgSize(1))-floor(imgSize(1)/2),... finalSize/2+(1:imgSize(2))-floor(imgSize(2)/2))=img; 
+6
source

You can try this function:

 function out1 = myresize(in1) %% Sa1habibi@gmail.com %% resize an image to closest power of 2 [m,n] = size(in1); if(rem(m,2)~=0) in1(1,:)=[]; end if(rem(n,2)~=0) in1(:,1)=[]; end [m,n] = size(in1); a = max(m,n); if(log2(a)~=nextpow2(a) || m~=n) s1 = 2^nextpow2(a); n_row = (s1 - m)/2; n_col = (s1 - n)/2; dimension = [n_row,n_col]; out1 = padarray(in1,dimension); end end 

eg:

 A = ones(2,8); out1 = myresize(A); 

first it finds a maximum of rows and columns, then a paddarray matrix in both directions.

0
source

I found this in a matlab document which is cleaner

 gcam = (imread('cameraman.tif')); padcam = padarray(gcam,[50 50],'both'); imshow(padcam) 

this can also be done in gpu with slight modifications, which is faster for large images

 gcam = gpuArray(imread('cameraman.tif')); padcam = padarray(gcam,[50 50],'both'); imshow(padcam) 
0
source

All Articles