-
Notifications
You must be signed in to change notification settings - Fork 5
/
image_gradients.py
43 lines (27 loc) · 1.17 KB
/
image_gradients.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Image Gradient : directional change in intensity or color in image.
# img = cv2.imread('test_images/messi5.jpg' , cv2.IMREAD_GRAYSCALE)
img = cv2.imread('test_images/lena.jpg' , cv2.IMREAD_GRAYSCALE)
##### Laplacian Gradient : detect edges in images. ######
# 64 bit float type. It supports negative number
# negative slop due to induced by transformaing the image white to black.
# NOTE : kernelsize must be odd
lap = cv2.Laplacian(img,cv2.CV_64F , ksize=1)
# take absolute value and convert it into unsigned interger 8 bit.
lap = np.uint8(np.absolute(lap))
##### Sobel Gradient ######
sobelX = cv2.Sobel(img , cv2.CV_64F , 1, 0 )
sobelY = cv2.Sobel(img , cv2.CV_64F , 0, 1 )
sobelX = np.uint8(np.absolute(sobelX))
sobelY = np.uint8(np.absolute(sobelY))
sobelCombined = cv2.bitwise_or(sobelX , sobelY)
titles = ['image' , 'Laplacian', 'sobelX' , 'sobelY', 'Sobel Combined']
images = [img , lap , sobelX , sobelY , sobelCombined]
for i in range(len(images)):
plt.subplot(2,3, i+1)
plt.imshow(images[i] , 'gray')
plt.title(titles[i])
plt.xticks([]) , plt.yticks([])
plt.show()