-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocument_Word_Detection.py
More file actions
54 lines (47 loc) · 2.01 KB
/
Copy pathDocument_Word_Detection.py
File metadata and controls
54 lines (47 loc) · 2.01 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
49
50
51
52
53
54
import cv2
import numpy as np
import imutils
frame = cv2.imread('test.jpeg')
frame = cv2.imread(frame,(600,600))
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)
thresh = cv2.threshold(blur,0,255,cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(25,1))
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(1,25))
print('horizontal kernel: {}'.format(horizontal_kernel))
print('vertical kernel : {}'.format(vertical_kernel))
horizontal_lines = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,horizontal_kernel,iterations=2)
vertical_lines = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,vertical_kernel,iterations=2)
cnts = cv2.findContours(horizontal_lines,cv2.RETR_EXTERNAL,cv2.findContours(vertical_lines,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE))
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cntsv = cntsv[0] if len(cntsv) == 2 else cntsv[1]
for c in cnts:
cv2.drawContours(frame,[c],-1,(255,255,255),2)
for c in cntsv:
cv2.drawContour(frame,[c],-1,9255,255,255),2)
cv2.imshow('thresh',thresh)
cv2.imshow('horizontal_lines',horizontal_lines)
cv2.imshow('vertical_lines',vertical_lines)
cv2.imshow('frame',frame)
gray1 = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
thresh1 = cv2.adaptiveThreshold(gray1,255,cv2.ADApTIVE_THRESH_GAUSsiAN_C,cv2.THRESH_BINARY,23,30)
canny = imutils.auto_canny(thresh1)
output = cv2.bitwise_not(canny)
kernel = np.ones((5,5),np.uint8)
opening = cv2.morphologyEx(canny,cv2.MORPH_CLOSE,kernel)
dilation = cv2.dilate(canny,kernel,iterations=1)
contour,hierachy = cv2.findContours(dilation,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for i in contour:
area = cv2.contourArea(i)
if area > 20:
x,y,w,h = cv2.boundingRect(i)
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,120,255),2)
cv2.imshow('output',output)
cv2.imshow('dilate',dilation)
cv2.imshow('opening',opening)
cv2.imshow('original_frame',frame)
cv2.imshow('canny',canny)
cv2.imshow('thresh1',thresh1)
cv2.imwrite('output.jpg',frame)
cv2.waitKey(0)
cv2.destroyAllWindows()