Page 170 - python
P. 170
144
product ไดผลลัพธเปนตัวเลขสเกลารหนึ่งตัวจากนั้นนําไปสรางเปนจุดสีของผลลัพธ วนทําจากซายไป
ั
ขวาและบนลงลาง จากตัวอยางนี้ เคอรเนลมีคา [[3,2,1],[-1,-2,-3],[2,1,-1]] เมื่อนําไปวางทาบกบภาพ
ตนฉบับแลวคํานวณผลรวมของ dot product จะมีคา (0x3) + (2x2) + (5x1) + (9x-1) + (-1x-2) +
(29x-3) + (0x2) + (34x1) + (7x -1) = (0, 4, 5, -9, 2, -87, 0, 34, -7) = -58 จากตัวอยางนี้ผลลัพธการ
ั
ั
ทําคอนโวลูชั่นกบเคอรเนลมีคาเทากบ -58 ใหเลื่อนเคอรเนลไปทางขวาและลงดานลางจนครบจะได
ผลลัพธเปนภาพที่ผานการคอนโวลูชั่น
ภาพประกอบที่ 11.12 แสดงการคอนโวลูชั่น
ตัวอยางที่ 11.9 การคอนโวลูชั่นดวยเคอรเนลแบบตาง ๆ
%pylab inline
import cv2
!wget "http://dsdi.msu.ac.th/articles/programming/lena.jpg"
img = cv2.imread('lena.jpg')
sobel_kernel_y = np.array([[+1, +2, +1], [0, 0, 0], [-1, -2, -1]])
img1 = cv2.filter2D(src=img, ddepth=-1, kernel=sobel_kernel_y)
sharpen_kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
img2 = cv2.filter2D(src=img, ddepth=-1, kernel=sharpen_kernel)
blur_kernel = np.array([[0.1, 0.1, 0.1], [0.1, 0.1, 0.1], [0.1, 0.1, 0.1]])
img3 = cv2.filter2D(src=img, ddepth=-1, kernel=blur_kernel)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)