Image filtering with zero padding

Hello, I need to implement imge filtering on a grayscale image with some mask, but the size of the output image is not equal to the input's. I tried to use zero-padding by using padarray, but it just puts both images in a 'frame' and do not resize the output.
x=imread('123.png'); % grayscale image
x_padded = padarray(x, [2 2]); % my 'zero-padding'
mask = [1 2 3; 4 5 6; 7 8 9]; % chosen mask
x1=x_padded(1:256,1:256);
y1=conv2(x1,mask);
M1=max(max(y1));
y1=255.*(y1./M1);
subplot(2,2,1);imshow(x_padded);title('Image')
subplot(2,2,2);imshow(uint8((y1)));title('Filtered Image')
Input and ouput images:
pls send help

 采纳的回答

Use the 'same' option in conv2() or else use imfilter(). You need to normalize your mask so that the mean intensity does not change, and then you don't need to scale y1.
mask = [1 2 3; 4 5 6; 7 8 9]; % chosen mask
mask = mask / sum(mask(:)); % Normalize
y1= uint8(conv2(double(x1),mask, 'same'));
imshow(y1, [])

更多回答(0 个)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by