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