MATLAB - Laplacian Filter



The Laplacian filter is a type of image enhancement filter used in image processing. It is used to sharpen images by emphasizing regions of rapid intensity change. The Laplacian filter is based on the Laplacian operator, which is a second-order derivative operator used to detect edges and fine details in images.

In the later part let us see different ways Laplacian filters can be used on images.

Laplacian Filter on Image using imfilter() in Matlab

In MATLAB, the Laplacian filter can be applied to an image using the imfilter function. The imfilter function convolves the image with a kernel that represents the Laplacian operator. This kernel highlights areas of rapid intensity change in the image, making edges appear sharper.

Syntax

output_image = imfilter(input_image, filter_kernel);

input_image − This is the input image to be filtered. It can be a grayscale image (2D array) or a color image (3D array with dimensions [height, width, channels]).

filter_kernel − This is the filter kernel or mask that defines how the filtering operation is performed. It is typically a 2D array (for 2D images) or a 3D array (for 3D images) that represents the weights of the filter. The size of the filter kernel determines the size of the neighborhood over which the filtering is applied. Common filter kernels include the Laplacian filter, Gaussian filter, Sobel filter, etc.

output_image − This is the output image after applying the filter. It has the same size and data type as the input image.

Let us see an example for Laplacian filter in imfilter() function

Example 1: Using Laplacian Filter in imfilter()

The code we have is −

% Read the image
img = imread('peppers.png');

% Display the original image

imshow(img);
title('Original Image');

% Apply the Laplacian filter
laplacian_filter = [0 -1 0; -1 4 -1; 0 -1 0];
filtered_img = imfilter(img, laplacian_filter);

% Display the filtered image
subplot(1, 2, 2);
imshow(filtered_img, []);
title('Laplacian Filtered Image');

In above example,

  • The code uses the imread() function to read an image file named 'peppers.png' from the current directory. The image is stored in the variable img.
  • The code uses the subplot function to create a figure with 1 row and 2 columns of subplots, and then selects the first subplot (1, 2, 1). The imshow() function is used to display the image img in this subplot. The title function sets the title of the subplot to 'Original Image'.
  • The code defines a 3x3 Laplacian filter kernel laplacian_filter which is used to perform image filtering. The Laplacian filter is applied to the original image using the imfilter() function, and the result is stored in the variable filtered_img.
  • Similar to displaying the original image, the code uses subplot to select the second subplot (1, 2, 2) and imshow() to display the filtered image filtered_img. The title of this subplot is set to 'Laplacian Filtered Image'.

The output we get is as follows −

Laplacian Filter on Image Using conv2() in Matlab

conv2() is a MATLAB function used for 2D convolution, which is a fundamental operation in image processing. It's commonly used for applying filters to images, such as blurring, sharpening, edge detection, and more.

Syntax

C = conv2(A, B, 'same');

In the syntax

  • A is the input image or matrix.
  • B is the 2D filter kernel (such as the Laplacian filter).
  • 'same' is an optional parameter that specifies the size of the output matrix C to be the same as the input matrix A.

Let us see an example for same

Example

The code we have is -

% Read the image as grayscale
img_gray = imread('peppers.jpg');

% Define the Laplacian filter kernel
laplacian_filter = [0 -1 0; -1 4 -1; 0 -1 0];

% Apply the Laplacian filter using conv2
filtered_img = conv2(double(img_gray), laplacian_filter, 'same');

% Display the original and filtered images
subplot(1, 2, 1);
imshow(img_gray);
title('Original Image');

subplot(1, 2, 2);
imshow(uint8(filtered_img));
title('Laplacian Filtered Image');

In the example above,

  • Reads the image 'peppers.jpg' as a grayscale image and stores it in the variable img_gray.
  • Defines a 3x3 Laplacian filter kernel laplacian_filter. This kernel is used for edge detection.
  • Laplacian Filter using conv2: Converts the grayscale image to a double precision matrix using double(img_gray).Applies the Laplacian filter to the grayscale image using conv2, which performs 2D convolution.The 'same' option ensures that the output image has the same size as the input image.
  • To display original and filtered images : For this subplot is used to create a figure with two subplots.Displays the original grayscale image in the first subplot.Displays the Laplacian-filtered image in the second subplot after converting it back to uint8 format using uint8(filtered_img).

When we execute the code in matlab commad window the output we get is −

Advertisements