Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCenter_code.py
More file actions
Latest commit
48 lines (37 loc) · 1.26 KB
/
Copy pathCenter_code.py
File metadata and controls
48 lines (37 loc) · 1.26 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
importcv2
importnumpyasnp
defcode(center):
orginal=np.uint8(center)
orginal=cv2.resize(orginal,(267,583))
h,w=orginal.shape
crop=orginal[158:h-5,18:w-18]
th, im_th=cv2.threshold(crop,127,255,0)
im_th=~im_th
kernel=np.ones((5,5), np.uint8)
binary=cv2.erode(im_th, kernel, iterations=2)
h1,w1=crop.shape
code= []
foryinrange(0, w1,np.uint(np.floor(w1/4))):
if (y+int(w1/4) >w1):
break
column=binary[0:h1, y: y+int(w1/4)]
visc=crop[0:h1, y: y+int(w1/4)]
count=0
forxinrange(0, h1,np.uint(np.floor(h1/10))):
if (x+int(h1/10) >h1):
break
row=column[x:x+int(h1/10),:]
visr=visc[x:x+int(h1/10),:]
count+=1
# cv2.imshow("Foreground", visr)
# cv2.waitKey(0)
_,cnts, _=cv2.findContours(row, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
iflen(cnts) ==1:
code.append(str(count))
iflen(code)>0:
code=['0'ifx=='10'elsexforxincode]
code=''.join(code)
code=int(code)
else:
code=""
returncode