-
Notifications
You must be signed in to change notification settings - Fork 11
/
ImagePreprocessing
46 lines (32 loc) · 1.19 KB
/
ImagePreprocessing
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
# preprocessing
# gray scale
def gray(img):
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imwrite(r"./preprocess/img_gray.png",img)
return img
# blur
def blur(img) :
img_blur = cv2.GaussianBlur(img,(5,5),0)
cv2.imwrite(r"./preprocess/img_blur.png",img)
return img_blur
# threshold
def threshold(img):
#pixels with value below 100 are turned black (0) and those with higher value are turned white (255)
img = cv2.threshold(img, 100, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY)[1]
cv2.imwrite(r"./preprocess/img_threshold.png",img)
return img
# Finding contours
im_gray = gray(im)
im_blur = blur(im_gray)
im_thresh = threshold(im_blur)
contours, _ = cv2.findContours(im_thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# text detection
def contours_text(orig, img, contours):
for cnt in contours:
x, y, w, h = cv2.boundingRect(cnt)
# Drawing a rectangle on copied image
rect = cv2.rectangle(orig, (x, y), (x + w, y + h), (0, 255, 255), 2)
cv2.imshow('cnt',rect)
cv2.waitKey()
# Cropping the text block for giving input to OCR
cropped = orig[y:y + h, x:x + w]