assignment no.b1

advertisement
ASSIGNMENT NO.B1
PROBLEM STATEMENT:
Write a Python program to apply the edge detection algorithm on image and to find the edges
use BBB / ARM Cortex A5/A9/M4 Mobile Boards. Use Sobel and Laplacian variant for edge
detection.
OBJECTIVE:
1. To learn concepts of image processing.
2. To learn the various edge detection algorithms.
3. To learn programming in embedded hardware using BBB/ARM Cortex.
PRIREQUISITES:
Software: Python, Remote Desktop Client, Opencv.
Hardware: BBB / ARM Cortex A5/A9/M4 Mobile Boards
THEORY:
Edge Detection
Edge detection is one of the fundamental operations when we perform image processing. It
helps us reduce the amount of data (pixels) to process and maintains the structural aspect of
the image. We're going to look into two commonly used edge detection schemes - the
gradient (Sobel - first order derivatives) based edge detector and the Laplacian (2nd order
derivative, so it is extremely sensitive to noise) based edge detector. Both of them work with
convolutions and achieve the same end goal - Edge Detection.
Sobel Edge Detection
Sobel edge detector is a gradient based method based on the first order derivatives. It
calculates the first derivatives of the image separately for the X and Y axes.
The operator uses two 3X3 kernels which are convolved with the original image to calculate
approximations of the derivatives - one for horizontal changes, and one for vertical. The
picture below shows Sobel Kernels in x-dir and y-dir:
Laplacian Edge Detection Sobel
Unlike the Sobel edge detector, the Laplacian edge detector uses only one kernel. It calculates
second order derivatives in a single pass. A kernel used in this Laplacian detection looks like
this:
If we want to consider the diagonals, we can use the kernel below:
Algorithm for Edge Detection
Step 1:- loading image.
Step 2:- converting to gray scale.
Step3:- remove noise.
Step4:- convolute with proper kernels.
Step5:- Display image.
Algorithm for Edge Detection using Sobel operator
Step 1: Accept the input image
Step 2: Apply maskGx,Gy to the input image
Step 3: Apply Sobel edge detection algorithm and the gradient
Step 4: Masks manipulation of Gx,Gy separately on the input image
Step 5: Results combined to find the absolute magnitude of the gradient
|𝐺| = √𝐺𝑋 2 + πΊπ‘Œ 2
Step 6: the absolute magnitude is the output edges.
Conclusion: - In this way we perform edge detection algorithm on image and to find the
edges using BBB Use Sobel and Laplacian variant for edge detection.
import cv2
import numpy as np
from matplotlib import pyplot as plt
# loading image
#img0 = cv2.imread('SanFrancisco.jpg',)
img0 = cv2.imread('windows.jpg',)
# converting to gray scale
gray = cv2.cvtColor(img0, cv2.COLOR_BGR2GRAY)
# remove noise
img = cv2.GaussianBlur(gray,(3,3),0)
# convolute with proper kernels
laplacian = cv2.Laplacian(img,cv2.CV_64F)
sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=5) # x
sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=5) # y
plt.subplot(2,2,1),plt.imshow(img,cmap = 'gray')
plt.title('Original'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,2),plt.imshow(laplacian,cmap = 'gray')
plt.title('Laplacian'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,3),plt.imshow(sobelx,cmap = 'gray')
plt.title('Sobel X'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,4),plt.imshow(sobely,cmap = 'gray')
plt.title('Sobel Y'), plt.xticks([]), plt.yticks([])
plt.show()
nput:
Images used
Output:
Download