# The Complete Guide Basic Edge Detection OpenCV RaspberryPi

This tutorial concerned with gradients and edge detection OpenCV RaspberryPi. Formally, edge detection includes mathematical methods to find points in an image where the brightness of pixel intensities changes clearly.

The first thing we are going to do is find the “gradient” of the grayscale image, allowing us to find edge-like regions in the x and y-direction.

Here We’ll then apply Canny edge detection, a multi-stage process of noise reduction (blurring), finding the gradient of the image non-maximum destruction and hysteresis thresholding.

## ≡ Laplacian and Sobel

This theory is better explained through some code for writing code with raspberry pi, Fast open your command terminal on your Raspberry Pi then write this Commend:

`nano`

you can see like this interface Now we can start writing ThisLaplacian and Sobel Edge Detection OpenCV RaspberryPi on GNU nano:

```from __future__ import print_function
import numpy as np
import argparse
import cv2

ap = argparse.ArgumentParser()
help = "Path to the image")
args = vars(ap.parse_args())

image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("Original", image)

lap = cv2.Laplacian(image, cv2.CV_64F)
lap = np.uint8(np.absolute(lap))
cv2.imshow("Laplacian", lap)
cv2.waitKey(0)

sobelX = cv2.Sobel(image, cv2.CV_64F, 1, 0)
sobelY = cv2.Sobel(image, cv2.CV_64F, 0, 1)

sobelX = np.uint8(np.absolute(sobelX))
sobelY = np.uint8(np.absolute(sobelY))

sobelCombined = cv2.bitwise_or(sobelX, sobelY)

cv2.imshow("Sobel X", sobelX)
cv2.imshow("Sobel Y", sobelY)
cv2.imshow("Sobel Combined", sobelCombined)
cv2.waitKey(0)```

Now save this file named sobel_and_laplacian.py and exit using ctrl + x, y, enter.  we simply open up a raspberry terminal window and execute the following command:

`python sobel_and_laplacian.py --image blobs.jpg`

see like this interface ## ≡ Analysis

```from __future__ import print_function
import numpy as np
import argparse
import cv2```

In the tutorial, you’ll see importing the print_ function from the __future__ package. We’ll be using that because of the actual print() function rather than the print statement so that this code will work with both Python 2 and Python 3.

And using argparse to handle parsing our command-line arguments. Then, cv2 is imported – cv2 is our OpenCV library and contains our image processing functions.

```# Construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
help = "Path to the image")
args = vars(ap.parse_args())```

This code handle parsing the command line arguments. The single argument we need is –image: the path to our image on disk. Lastly, we parse the arguments and store them in a dictionary.

```# Load the image and show some basic information on it
```

Now that we have the path to the image, we can load it off the disk using the cv2.imread function. The cv2.imread function returns a NumPy array representing the image that you lode.

```image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("Original", image)```

In this section When computing gradients and edges, we (normally) count them on a single channel – in this case, we are using the grayscale image; but, we can also count gradients for each channel of the RGB image. For the sake of simplicity, let’s stick with the grayscale image since that is what you will use in most cases.

`lap = cv2.Laplacian(image, cv2.CV_64F)`

we apply the Laplacian method to count the gradient magnitude image by calling the cv2.Laplacian function. The first argument is our grayscale image – the image we need to count the gradient magnitude representation for. The second argument is our data type for the output image.

`lap = np.uint8(np.absolute(lap))`

In order to assure you catch all edges, apply a floating-point data type, then take the full value of the gradient image, and convert it back to an 8-bit unsigned integer. This is definitely an important technique to take note of – unless, you’ll be missing edges in your image!

```sobelX = cv2.Sobel(image, cv2.CV_64F, 1, 0)
sobelY = cv2.Sobel(image, cv2.CV_64F, 0, 1)```

applying the cv2.Sobel method. The first argument to the Sobel operator is the image we need to compute the gradient representation for. Then, just like in the Laplacian example above, we apply a floating-point data type. The last two arguments are the order of the derivatives in the x and y-direction, each. Specify a value of 1 and 0 to find vertical edge-like regions and 0 and 1 to find horizontal edge-like regions.

```sobelX = np.uint8(np.absolute(sobelX))
sobelY = np.uint8(np.absolute(sobelY))```

we then secure we find all edges by taking the full value of the floating-point image and then converting it to an 8-bit unsigned integer.

In order to combine the gradient images in both the x and y direction, we can use a bitwise OR. Remember, an OR operation is true when either pixel is greater than zero. Therefore, a given pixel will be True if either a horizontal or vertical edge is present.

```cv2.imshow("Sobel X", sobelX)
cv2.imshow("Sobel Y", sobelY)
cv2.imshow("Sobel Combined", sobelCombined)
cv2.waitKey(0)```

Finally, we show our gradient images

## ≡ Canny Edge Detector

The Canny edge detector is a multi-step process. It involves blurring the image to remove noise, computing Sobel gradient images in the x and y direction, suppressing edges, and finally, a hysteresis thresholding stage that determines if a pixel is “edge-like” or not.

Now we can start writing This Canny Edge Detection OpenCV RaspberryPi on GNU nano:

```import numpy as np
import argparse
import cv2

ap = argparse.ArgumentParser()
help = "Path to the image")
args = vars(ap.parse_args())

image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.GaussianBlur(image, (5, 5), 0)
cv2.imshow("Blurred", image)

canny = cv2.Canny(image, 30, 150)
cv2.imshow("Canny", canny)
cv2.waitKey(0)```

Now save this file named canny.py and exit using ctrl + x, y, enter.  we simply open up a raspberry terminal window and execute the following command:

`python canny.py --image blobs.jpg`

see like this interface ## ≡ Analysis

The first thing we do is import our packages and parse our arguments. We then load our image, convert it to grayscale, and blur it using the Gaussian blurring method. By applying a blur prior to edge detection, we will help remove “noisy” edges in the image that are not of interest to us.

`canny = cv2.Canny(image, 30, 150)`

Applying the Canny edge detector is performed using the cv2.Canny function. The first argument we supply is our blurred, grayscale image. Then, we require to provide two values: threshold1 and threshold2.

Any gradient value larger than threshold2 is considered to be an edge. Any value below threshold1 is considered not to be an edge. Values in between threshold1 and threshold2 are either classified as edges or non-edges based on how their intensities are “connected”. In this case, any gradient values below 30 are considered non-edges whereas any values above 150 are considered edges.

`cv2.imshow("Canny", canny)`

We then show the results of our edge detection

More Computer vision tutorial on raspberry pi click COMPUTER VISION BEGINNER

Visit My Blog

91 total views,  1 views today