forked from ultralytics/yolov5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcv.py
57 lines (50 loc) · 1.96 KB
/
cv.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
import sys
import numpy as np
import cv2
im = cv2.imread('tt.png')
im3 = im.copy()
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) #先转换为灰度图才能够使用图像阈值化
thresh = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2) #自适应阈值化
################## Now finding Contours ###################
#
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
#边缘查找,找到数字框,但存在误判
samples = np.empty((0,900)) #将每一个识别到的数字所有像素点作为特征,储存到一个30*30的矩阵内
responses = [] #label
pre={}
for i in range(48,58):
pre[i]=-1
keys = [i for i in range(48,58)] #48-58为ASCII码
count =0
for cnt in contours:
if cv2.contourArea(cnt)>80: #使用边缘面积过滤较小边缘框
[x,y,w,h] = cv2.boundingRect(cnt)
if h>25 and h < 30: #使用高过滤小框和大框
count+=1
cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,255),2)
roi = thresh[y:y+h,x:x+w]
roismall = cv2.resize(roi,(30,30))
cv2.imshow('norm',im)
key = cv2.waitKey(0)
#print(key,end=' ')
if key == 27: # (escape to quit)
sys.exit()
elif key in keys:
#print(pre[key])
responses.append(int(chr(key)))
sample = roismall.reshape((1,900))
samples = np.append(samples,sample,0)
print(sample)
if count == 100: #过滤一下过多边缘框,后期可能会尝试极大抑制
break
responses = np.array(responses,np.float32)
responses = responses.reshape((responses.size,1))
print()
print ("training complete")
#print(samples)
#print(responses)
np.savetxt('generalsamples.data',samples)
np.savetxt('generalresponses.data',responses)
#
cv2.waitKey()
cv2.destroyAllWindows()