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
| import cv2 import matplotlib.pyplot as plt
def image_show(name, img): cv2.imshow(name, img) cv2.waitKey(0) cv2.destroyAllWindows()
img = cv2.imread("./images/cat.png") ret1, dst1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) ret2, dst2 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV) ret3, dst3 = cv2.threshold(img, 127, 255, cv2.THRESH_TRUNC) ret4, dst4 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO) ret5, dst5 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO_INV)
images = [img, dst1, dst2, dst3, dst4, dst5] title = ['img', 'BINARY', 'BINARY_INV', 'TRUNC', 'TOZERO', 'TOZERO_INV']
for i in range(6): plt.subplot(2, 3, i+1) plt.imshow(images[i][:, :, ::-1], 'gray') plt.title(title[i]) plt.show()
|