-
Notifications
You must be signed in to change notification settings - Fork 0
/
code2.py
64 lines (46 loc) · 1.79 KB
/
code2.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
from cv2 import *
img = imread('TextDocument.png')
grey = cvtColor(img,COLOR_RGB2GRAY)
ret, thresh = cv2.threshold(grey,0,255,cv2.THRESH_OTSU|cv2.THRESH_BINARY_INV)
rect_kernel = getStructuringElement(MORPH_RECT,(13,3))
dilation = dilate(thresh,rect_kernel, iterations=1)
contours, hierarchy = findContours(dilation, RETR_LIST, CHAIN_APPROX_NONE)
lines = []
img2 = img.copy()
for contour in contours:
x,y,w,h = boundingRect(contour)
if h<20 and w>17:
lines.append((x,y,w,h))
rectangle(img2, (x,y), (x+w,y+h), (0,255,0),2)
imshow('',img2)
waitKey(0)
lines = lines[::-1]
rect_kernel = getStructuringElement(MORPH_RECT,(3,3))
image_count = 1
for l in lines:
(x,y,w,h) = l
crop = thresh[y:y+h,x:x+w]
dilation = dilate(crop,rect_kernel, iterations=1)
contours, hierarchy = findContours(dilation, RETR_LIST, CHAIN_APPROX_NONE)
for contour in contours[::-1]:
x1,y1,w1,h1 = boundingRect(contour)
if h1>5 and w1>5:
rectangle(img2, (x+x1,y+y1), (x+x1+w1,y+y1+h1), (255,0,0),1)
word = thresh[y+y1:y+y1+h1,x+x1:x+x1+w1]
col = 0
while col<w1:
if any(x==255 for x in word[:,col]):
beg = col
while any(x==255 for x in word[:,col]):
col+=1
start = y+y1
end = y+y1+h1-1
while all(c==0 for c in thresh[start,x+x1+beg:x+x1+col]):
start+=1
while all(c==0 for c in thresh[end,x+x1+beg:x+x1+col]):
end-=1
imwrite('letters2/character'+str(image_count).zfill(4)+'.png' ,img[start:end+1,x+x1+beg:x+x1+col])
image_count+=1
col+=1
imshow('',img2)
waitKey(0)