-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchar_split.py
121 lines (95 loc) · 2.79 KB
/
char_split.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import cv2
import numpy as np
img = cv2.imread('./W22207.jpg',cv2.COLOR_BGR2GRAY)
import matplotlib.pyplot as plt
import pdb
def verticalProjector(img):
ret,thresh=cv2.threshold(GrayImage,130,255,cv2.THRESH_BINARY) #圖片二值化(130,255)之間的點變為255(背景)
(h,w)=thresh.shape
a = [0 for z in range(0, w)]
#垂直投影
for j in range(0,w):
for i in range(0,h):
if thresh[i,j]==0:
a[j]+=1
thresh[i,j]=255
for j in range(0,w):
for i in range((h-a[j]),h):
thresh[i,j]= 0
average=[]
for k in range(4,len(a)):
sum=0
for l in range(k-4,k+1):
sum+=a[l]
average.append(sum/5)
print(average)
trough_point=[]
crest_point=[]
for m in range(0,len(average)):
if m<len(average)-2:
if((average[m-1]>average[m-2]) & (average[m]>average[m-1]) & (average[m]>average[m+1]) & (average[m+2]<average[m+1])):
crest_point.append(m)
if((average[m-1]<average[m-2]) & (average[m]<average[m-1]) & (average[m]<average[m+1]) & (average[m+2]>average[m+1])):
#pdb.set_trace()
trough_point.append(m)
for n in range(0,len(trough_point)):
print(n,a[n])
for o in range(0,len(crest_point)):
print(o,a[o])
return thresh
def horizontalProjector(img):
ret,thresh=cv2.threshold(GrayImage,130,255,cv2.THRESH_BINARY) #圖片二值化(130,255)之間的點變為255(背景)
(h,w)=thresh.shape
#水平投影
a = [0 for z in range(0, h)]
for j in range(0,h):
for i in range(0,w):
if thresh[j,i]==0:
a[j]+=1
thresh[j,i]=255
for j in range(0,h):
for i in range(0,a[j]):
thresh[j,i]=0
start=0
start_state=0
end=0
end_state=0
for i in range(0,len(a)):
if (start_state==0) & (a[i]==0):
if(a[i+1]!=0):
start=i
start_state=1
i+=1
if (start_state==1) & (end_state==0) & (a[i]==0):
if(a[i+1]==0):
end=i
end_state=1
crop_img = img[start:end, 0:300]
return crop_img
GrayImage=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #RGB 2 Gray
inputImage=horizontalProjector(GrayImage)
#二值化圖片
blurred = cv2.GaussianBlur(inputImage, (11, 11), 0)
binaryIMG = cv2.Canny(blurred, 20, 160)
"""
cv2.imshow('',blurred)
cv2.waitKey(0)
"""
(cnts, _) = cv2.findContours(binaryIMG.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
clone=inputImage.copy()
for c in cnts:
(x, y, w, h) = cv2.boundingRect(c)
cv2.rectangle(clone, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow('',clone)
cv2.waitKey(0)
plt.figure(figsize=(10,5))
plt.suptitle('compare')
plt.subplot(3,2,1), plt.title('GrayImage')
plt.imshow(GrayImage,cmap ='gray'), plt.axis('off')
plt.subplot(3,2,2), plt.title('horizontal')
plt.imshow(horizontalProjector(GrayImage),cmap ='gray'), plt.axis('off')
plt.subplot(3,2,3), plt.title('vertical')
plt.imshow(verticalProjector(GrayImage),cmap ='gray'), plt.axis('off')
plt.show()