image noise reduction with binomial low-pass filtering in matlab

To perform image noise reduction using binomial low-pass filtering in MATLAB, follow these steps:

  1. Read the input image using the imread function and convert it to grayscale using the rgb2gray function:
main.m
img = imread('input_image.jpg');
img_gray = rgb2gray(img);
59 chars
3 lines
  1. Define the order of the filter (the number of times the filter will be convolved with the image) and the kernel size (the size of the filter):
main.m
order = 5;
kernel_size = 2^order - 1; % The kernel size is calculated using the binomial formula
97 chars
3 lines
  1. Generate the binomial filter kernel using the generateBinomialKernel function:
main.m
kernel = generateBinomialKernel(kernel_size);
46 chars
2 lines
  1. Convolve the image with the filter kernel using the conv2 function:
main.m
filtered_img = conv2(double(img_gray), kernel, 'same');
56 chars
2 lines

Note that we convert the image to double before convolving it with the filter kernel.

  1. Convert the filtered image back to uint8 and display it using the imshow function:
main.m
filtered_img = uint8(filtered_img);
imshow(filtered_img);
58 chars
3 lines

Here's the complete code:

main.m
img = imread('input_image.jpg');
img_gray = rgb2gray(img);

order = 5;
kernel_size = 2^order - 1;
kernel = generateBinomialKernel(kernel_size);

filtered_img = conv2(double(img_gray), kernel, 'same');
filtered_img = uint8(filtered_img);
imshow(filtered_img);

function kernel = generateBinomialKernel(kernel_size)
    % Generates a binomial filter kernel
    % kernel_size: size of the kernel
    % kernel: the generated kernel
    
    kernel = [1 1];
    for i = 1:kernel_size-2
        kernel = conv(kernel, [1 1]);
    end
    kernel = kernel / sum(kernel);
end
566 chars
23 lines

gistlibby LogSnag