Amazing-Python-Scripts

Форк
0
101 строка · 2.9 Кб
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,
23
                       cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
24

25
# get structuring element
26

27
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (25, 1))
28
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 25))
29
print('horizontal kernel : {}'.format(horizontal_kernel))
30
print('vertical kernel : {}'.format(vertical_kernel))
31

32
# opening (erosion followed by dilation)
33

34
horizontal_lines = cv2.morphologyEx(thresh,
35
                                    cv2.MORPH_OPEN,
36
                                    horizontal_kernel,
37
                                    iterations=2)
38
vertical_lines = cv2.morphologyEx(thresh,
39
                                  cv2.MORPH_OPEN,
40
                                  vertical_kernel,
41
                                  iterations=2)
42

43
# contours apply on detected lines
44
# First one is source image, second is contour retrieval mode, third is contour approximation method
45

46
cnts = cv2.findContours(horizontal_lines, cv2.RETR_EXTERNAL,
47
                        cv2.CHAIN_APPROX_SIMPLE)
48
cntsv = cv2.findContours(vertical_lines, cv2.RETR_EXTERNAL,
49
                         cv2.CHAIN_APPROX_SIMPLE)
50

51
# find contours
52
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
53
cntsv = cntsv[0] if len(cntsv) == 2 else cntsv[1]
54

55
for c in cnts:
56
    cv2.drawContours(frame, [c], -1, (255, 255, 255), 2)
57
for c in cntsv:
58
    cv2.drawContours(frame, [c], -1, (255, 255, 255), 2)
59

60
# imshow
61
cv2.imshow('thresh', thresh)
62
cv2.imshow('horizontal_lines', horizontal_lines)
63
cv2.imshow('vertical_lines', vertical_lines)
64
cv2.imshow('frame', frame)
65

66
# grayscale
67

68
gray1 = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
69
thresh1 = cv2.adaptiveThreshold(gray1, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
70
                                cv2.THRESH_BINARY, 23, 30)
71
canny = imutils.auto_canny(thresh1)
72

73
output = cv2.bitwise_not(canny)
74
kernel = np.ones((5, 5), np.uint8)
75

76
opening = cv2.morphologyEx(canny, cv2.MORPH_CLOSE, kernel)
77

78
dilation = cv2.dilate(canny, kernel, iterations=1)
79

80
contour, hierachy = cv2.findContours(dilation, cv2.RETR_TREE,
81
                                     cv2.CHAIN_APPROX_SIMPLE)
82

83
for i in contour:
84
    area = cv2.contourArea(i)
85
    if area > 20:
86
        x, y, w, h = cv2.boundingRect(i)
87
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 120, 255), 2)
88

89
cv2.imshow('output', output)
90
cv2.imshow('dilate', dilation)
91
cv2.imshow('opening', opening)
92
cv2.imshow('original_frame', frame)
93
cv2.imshow('canny', canny)
94
cv2.imshow('thresh1', thresh1)
95

96
# Saving output image
97
cv2.imwrite('output.jpg', frame)
98

99
# destroy all window
100
cv2.waitKey(0)
101
cv2.destroyAllWindows()
102

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.