# Laplacian filter

The Laplacian is a "Second-Order Derivative filter" ; this means that it operates upon a convolution and not an image. There are several methods to implement Laplacian filter but given the context of this module we are going to be using the DoG or Difference of Gaussian method, and it is what it sounds like, it words by subtracting a image of lower Gaussian sigma by a higher one, effectively giving us the difference. As we recall previously, we did something similar in the sharpening algorithm, except the [minuend](https://www.mathsisfun.com/definitions/minuend.html) was the base image, in this case it's a blurred image of a smaller sigma.

> **Note** If you are having trouble understanding this, I suggest you to refresh upon the [Gaussian Filter](/computer-vision-guide/image-smoothing/gaussian-filter.md)

## Implementation

The Laplacian filter is implemented at [src/edge-detection/laplacian.c](https://github.com/Aadv1k/cv.c/blob/master/src/edge-detection/laplacian.c)

```c
void cv_apply_laplacian_filter(Image* img, float sigma, int kernSize) {
  /* ... */

  float sigma1 = sigma;
  int kernSize1 = kernSize * 1.5 + 1;

  cv_apply_gaussian_blur(img, sigma1, kernSize1);
 
  /* ... */

  cv_apply_gaussian_blur(&copiedImage, sigma, kernSize);

  for (int i = 0; i < height; i++) {
	for (int j = 0; j < width; j++) {
  	for (int c = 0; c < channels; c++) {
    	int index = (i * width + j) * channels + c;
    	img->bytes[index] = img->bytes[index] - copiedImage.bytes[index];
  	}
	}
  }

  /* ... */
}
```

* We compute a different `sigma1`, and `kernSize1` since we don't want the user to specify both of these. `kernSize * 1.5 + 1` is really arbitrary and I suggest you mess around with different variations.
* We loop through and subtract the less blurred image from the one which is more blurred.

And that is really it! Let's look at what this produces.

## Result

```shell
.\bin\cv --laplacian --kernel 3 --sigma 1.5 .\data\img1.jpg .\output.jpg
```

<div><figure><img src="/files/ZAjlEb0s1WQsjygmQXiJ" alt=""><figcaption><p>Original image</p></figcaption></figure> <figure><img src="/files/IbIjDEPnlNSy4j0Mp7iO" alt=""><figcaption><p>Laplacian filter of Sigma 1.5, Kernel 3</p></figcaption></figure></div>

## Problems

> It is somewhat computationally slow

Like the previous filters an image of higher size and/or a higher kernel will directly affect the speed of the function (benchmarked on a larger image)

```
Laplacian Filter
----------------
kernelSize = 3: 10.4806246 sec
```

> False noise can cause bad edge detection

As the title suggests, even a slight noise will translate into the result, which is generally not good for a edge detector


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://aadv1k.gitbook.io/computer-vision-guide/edge-detection/laplacian-filter.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
