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
31
32
33
34
35
36
37
38 | import cv2
import numpy as np
import matplotlib.pyplot as plt
try:
path = r'F:\img\coins.jpg'
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
thresh, BW1 = cv2.threshold(img,85,255,cv2.THRESH_BINARY)
thresh, BW2 = cv2.threshold(img,85,255,cv2.THRESH_BINARY_INV)
thresh, BW3 = cv2.threshold(img,85,255,cv2.THRESH_TOZERO)
thresh, BW4 = cv2.threshold(img,85,255,cv2.THRESH_TOZERO_INV)
thresh, BW5 = cv2.threshold(img,85,255,cv2.THRESH_TRUNC)
fig, axs = plt.subplots(2,3,figsize=(16,8))
axs[0,0].imshow(img,cmap='gray')
axs[0,0].axis('off')
axs[0,0].set_title('Original Image',fontsize='medium')
axs[0,1].imshow(BW1,cmap='gray')
axs[0,1].axis('off')
axs[0,1].set_title('THRESH_BINARY',fontsize='medium')
axs[0,2].imshow(BW2,cmap='gray')
axs[0,2].axis('off')
axs[0,2].set_title('THRESH_BINARY_INV',fontsize='medium')
axs[1,0].imshow(BW3,cmap='gray')
axs[1,0].axis('off')
axs[1,0].set_title('THRESH_TOZERO',fontsize='medium')
axs[1,1].imshow(BW4,cmap='gray')
axs[1,1].axis('off')
axs[1,1].set_title('THRESH_TOZERO_INV',fontsize='medium')
axs[1,2].imshow(BW5,cmap='gray')
axs[1,2].axis('off')
axs[1,2].set_title('THRESH_TRUNC',fontsize='medium')
except Exception as e:
print('Unable to read image.' + e)
|