-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththresholding.py
More file actions
58 lines (47 loc) · 1.21 KB
/
thresholding.py
File metadata and controls
58 lines (47 loc) · 1.21 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
from pyzbar import pyzbar
# Read Image
img = cv.imread('test2.png', cv.IMREAD_GRAYSCALE)
closed = cv.morphologyEx(img, cv.MORPH_CLOSE, cv.getStructuringElement(cv.MORPH_RECT, (1, 21)))
#Statistics
#print(img.shape)
dens = np.sum(img, axis=0)
mean = np.mean(dens)
#print(mean)
#Thresholding
thresh = closed.copy()
for idx, val in enumerate(dens):
if val< 10800:
print("Needs fixing")
thresh[:,idx] = 0
(_, thresh2) = cv.threshold(thresh, 128, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
#plotting results
'''
plt.figure(num='barcode')
plt.subplot(221)
plt.imshow(img, cmap='gray')
plt.title('Original')
plt.axis('off')
plt.subplot(224)
plt.imshow(thresh, cmap='gray')
plt.title('Thresholded')
plt.axis('off')
'''
plt.subplot(223)
plt.imshow(thresh2, cmap='gray')
plt.title('Result')
plt.axis('off')
'''
plt.subplot(222)
plt.hist(dens)
plt.axvline(dens.mean(), color='k', linestyle='dashed', linewidth=1)
plt.title('dens hist')
'''
#plt.show() # remove comment to show graph thresholded img
barcodes = pyzbar.decode(thresh2)
for barcode in barcodes:
fixed_bc = barcode.data.decode('utf-8')
#print(fixed_bc)
#print(type(barcodes[0]))