How to detect edges in an image using opencv in Python
Tags: python, machinelearning, datascience, tutorial
Published: 2020-11-09 10:13:10

### what is edge detection?

Edge detection is an image processing technique for finding the boundaries of objects within images. It works by detecting discontinuities in brightness. 

### where edge detection is used?

Edge detection is used for *image segmentation* and *data extraction* in areas such as *image processing*, *computer vision*, and* machine vision*, so knowing how to do it will eventually pay you off.

### Requirements

There are several edge detection algorithms and different libraries supporting it but in this tutorial, I'm going to show you how to do it using [OpenCV](https://opencv.org/) using the Canny algorithm.

-   [OpenCV](https://opencv.org/)
-   [Matplotlib](https://matplotlib.org/)

### Installation

For window just use normal ```pip``` while for Linux & Mac use ```pip3``` to install the dependencies  just as shown below;

```bash
pip install opencv-python
pip install matplotlib
```

## Let's get started

Once we have installed now we ready to go to detecting edges with python using *Canny algorithms*.

we are going to use the OpenCV method *imread()* to load an image from the file, use *Canny() *to detect the edges, and then finally visualizing the images before detection and after using **Matplotlib**

### Reading images with OpenCV

To read an image from a file using the *imread()* method you need to provide two-parameter, one is for the path to our raw image, and the next one is a mode of reading which can either by GRAY, RGB, HSV, HSL, and etc.

#### OpenCV syntax to read image

```python
import cv2​

image = cv2.imread(path_to_image, mode_of_reading)
```

**Canny algorithms** usually work well when the image is in grayscale, there is a shortcut in OpenCV to open an image in a grayscale mode which is done by putting 0 on the mode of reading.

#### Using Canny algorithms to detect the edges

To detect edges with *Canny* you have to specify your raw image, lower pixel threshold, and higher pixel threshold in the order shown below;

```python
image_with_edges = cv2.Canny(raw_image, l_threshold, h_theshold)
```

### How threshold affect edge detection?

The intensity gradient of a pixel is greater than the higher threshold, it will be added as an edge pixel in the output image otherwise it will be rejected completely.

#### Finalizing our code and Visualizing it with Matplotlib

Now that's we learned the basics of OpenCV together with Canny detection algorithms now let's put them together to sample real-world application

Let's detect the edges in the below sample case image road.jpg

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mlmxju9564oxe6kuyg5x.jpg)

Using the knowledge we just learned above, I have bundled everything together to come up with below final code below, when you run it, it will load an image, perform detection and display it using Matplotlib.

```python
import cv2
import matplotlib.pyplot as plt 

def detect_edge(image):
    ''' function Detecting Edges '''
    
    image_with_edges = cv2.Canny(image , 100, 200)
    
    images = [image , image_with_edges]
    
    location = [121, 122]
    
    for loc, img in zip(location, images):
        plt.subplot(loc)
        plt.imshow(img, cmap='gray')
    
    plt.savefig('edge.png')
    plt.show()
    
image = cv2.imread('road.jpg', 0)
detect_edge(image)
```

#### Ouput 
Once executed it will render a picture with edges just as shown in the picture below;

![](https://kalebujordan.com/wp-content/uploads/2020/04/edrge.png)

🎉🎉🎉 Congratulations you have just learned how to detect edges in python, you should be proud of yourself, [Tweet now](https://twitter.com/intent/tweet?text=Hello guys just completed how tutorial on how to dee using python by @j_kalebu, check it out at https://kalebujordan.dev/edge-detection-using-gaussianblur-algorithm-in-python/) to share this good news with your fellow developers.

Also in case of anything, drop it in the comment box below and I will get back to you ASAP

{% github https://github.com/Kalebu/Edge-Detection %}