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)
   165   166   167   168   169   170   171   172   173   174   175