Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5441c5e

Browse files
Word Detection in Document
1 parent 3cd3191 commit 5441c5e

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

‎Document-Word-Detection/README.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Document Word Detection
2+
3+
<table>
4+
<tr>
5+
<td align="center"><b>Input image</b></td>
6+
<td align="center"><b>Output image</b></td>
7+
</tr>
8+
<tr>
9+
<td><img src='test.jpeg' width=100%></td>
10+
<td><img src='output.jpg' width=100%></td>
11+
</tr>
12+
</table>
13+
14+
## Prerequisite
15+
16+
1. **OpenCV :** `$ pip install opencv-python`
17+
2. **Numpy :** `$ pip install numpy`
18+
3. **Imutils :** `$ pip install imutils`
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
'''
2+
output : word detection on document (Simple OCR type of application)
3+
'''
4+
5+
import cv2
6+
import numpy as np
7+
import imutils
8+
9+
# frame read
10+
frame = cv2.imread('test.jpeg')
11+
12+
# resize
13+
frame = cv2.resize(frame , (600,600))
14+
15+
# grayscale
16+
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
17+
18+
# remove noise
19+
blur = cv2.GaussianBlur(gray,(5,5),0)
20+
21+
# otsu thresh (bimodel thresold)
22+
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
23+
24+
# get structuring element
25+
26+
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (25,1))
27+
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,25))
28+
print('horizontal kernel : {}'.format(horizontal_kernel))
29+
print('vertical kernel : {}'.format(vertical_kernel))
30+
31+
# opening (erosion followed by dilation)
32+
33+
horizontal_lines = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
34+
vertical_lines = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
35+
36+
# contours apply on detected lines
37+
# First one is source image, second is contour retrieval mode, third is contour approximation method
38+
39+
cnts = cv2.findContours(horizontal_lines ,cv2.RETR_EXTERNAL , cv2.CHAIN_APPROX_SIMPLE)
40+
cntsv = cv2.findContours(vertical_lines ,cv2.RETR_EXTERNAL , cv2.CHAIN_APPROX_SIMPLE)
41+
42+
# find contours
43+
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
44+
cntsv = cntsv[0] if len(cntsv) == 2 else cntsv[1]
45+
46+
for c in cnts:
47+
cv2.drawContours(frame, [c], -1, (255,255,255), 2)
48+
for c in cntsv:
49+
cv2.drawContours(frame, [c], -1, (255,255,255), 2)
50+
51+
52+
# imshow
53+
cv2.imshow('thresh', thresh)
54+
cv2.imshow('horizontal_lines', horizontal_lines)
55+
cv2.imshow('vertical_lines', vertical_lines)
56+
cv2.imshow('frame', frame)
57+
58+
# grayscale
59+
60+
gray1 = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
61+
thresh1 = cv2.adaptiveThreshold(gray1, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 23, 30)
62+
canny = imutils.auto_canny(thresh1)
63+
64+
output = cv2.bitwise_not(canny)
65+
kernel = np.ones((5,5),np.uint8)
66+
67+
opening = cv2.morphologyEx(canny, cv2.MORPH_CLOSE, kernel)
68+
69+
dilation = cv2.dilate(canny,kernel,iterations = 1)
70+
71+
72+
contour,hierachy=cv2.findContours(dilation,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
73+
74+
for i in contour:
75+
area= cv2.contourArea(i)
76+
if area>20:
77+
x,y,w,h = cv2.boundingRect(i)
78+
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,120,255),2)
79+
80+
81+
cv2.imshow('output' ,output)
82+
cv2.imshow('dilate' ,dilation)
83+
cv2.imshow('opening' ,opening)
84+
cv2.imshow('original_frame' ,frame)
85+
cv2.imshow('canny' ,canny)
86+
cv2.imshow('thresh1' ,thresh1)
87+
88+
# Saving output image
89+
cv2.imwrite('output.jpg', frame)
90+
91+
# destroy all window
92+
cv2.waitKey(0)
93+
cv2.destroyAllWindows()
94+
95+
96+
97+
98+
99+
100+
101+
102+
103+
104+
105+
106+
107+
108+
109+
110+
111+

‎Document-Word-Detection/output.jpg‎

289 KB
Loading[フレーム]

‎Document-Word-Detection/test.jpeg‎

143 KB
Loading[フレーム]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /