MATLAB: How can I reduce the padding

MATLAB Compilerpadding

Hi! Can anybody show me what can I do to reduce the padding? I mean, the final result must contain the image matrix in the center and zeros around it. Something like:
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 2 6 7 0 0
0 0 6 8 8 0 0
0 0 1 3 5 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
image=imread('Laborator4.jpg');
image_zg=imnoise(image,'gaussian',0,0.005);
image_out=filtrare_liniara(image_zg,e);
figure;
subplot(1,3,1);imshow(image);
subplot(1,3,2);imshow(image_zg);
subplot(1,3,3);imshow(image_out);
and the linear function:
function im_out = filtrare_liniara(im_in,se)
im_in = padarray(im_in,[2 2],0,'both')
[m,n]=size(im_in);
im_out=zeros(m,n);
im_in=double(im_in);
for i=2:m-1
for j=2:n-1
vecinatate = im_in(i-1:i+1,j-1:j+1);
temp=se.*vecinatate;
temp=sum(sum(temp));
im_out(i,j)=temp;
end
end
im_out=uint8(im_out);
end

Best Answer

  • Try this
    A = [ ...
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 2 6 7 0 0
    0 0 6 8 8 0 0
    0 0 1 3 5 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0];
    [r,c] = find(A);
    rs = min(r):max(r);
    cs = min(c):max(c);
    A_compact = A(rs,cs);
    Result
    >> A_compact
    A_compact =
    2 6 7
    6 8 8
    1 3 5