Page 161 - python
P. 161

135




                   11.6 การดําเนินการเกี่ยวกับบิต (Bit)

                                         ี่
                          การดําเนินการเกยวกับบต ประกอบดวยคําสั่ง bitwise_and() , bitwise_or(), bitwise_xor()
                                                ิ
                   และ bitwise_not() โดยที่โอเปอรเรเตอร AND OR และ Exclusive OR เขียนแทนดวย & | และ ^
                   ตามลําดับ


                   ตัวอยางที่ 11.3 การดําเนินการเกี่ยวกับบิต
                    %pylab inline
                    import cv2


                    rectangle = np.zeros((300, 300), dtype = "uint8")
                    cv2.rectangle(rectangle, (25, 25), (275, 275), 255, -1)
                    circle = np.zeros((300, 300), dtype = "uint8")
                    cv2.circle(circle, (150, 150), 150, 255, -1)


                    rectangle = cv2.cvtColor(rectangle, cv2.COLOR_BGR2RGB)
                    circle = cv2.cvtColor(circle, cv2.COLOR_BGR2RGB)


                    bitwise_and = cv2.bitwise_and(rectangle, circle)
                    bitwise_or = cv2.bitwise_or(rectangle, circle)
                    bitwise_xor = cv2.bitwise_xor(rectangle, circle)

                    bitwise_not = cv2.bitwise_not(rectangle)

                    fig = plt.figure(figsize=(20,20))
                    ax = fig.add_subplot(1,6,1)

                    ax.imshow(rectangle)
                    ax.set_title('rectangle')

                    ax1 = fig.add_subplot(1,6,2)

                    ax1.imshow(circle)
                    ax1.set_title('circle')

                    ax2 = fig.add_subplot(1,6,3)

                    ax2.imshow(bitwise_and)
                    ax2.set_title('and')

                    ax3 = fig.add_subplot(1,6,4)

                    ax3.imshow(bitwise_or)
   156   157   158   159   160   161   162   163   164   165   166