-
Notifications
You must be signed in to change notification settings - Fork 1
/
digits_overlay.py
executable file
·47 lines (33 loc) · 1.22 KB
/
digits_overlay.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
#!/usr/bin/env python
# digits_overlay.py
#
# Copyright(c) Exequiel Ceasar Navarrete <[email protected]>
# Licensed under MIT
# Version 1.0.1
import os
import cv2
from app import recognize_characters
# function to filter out contours on an image
def filter_contours(contour):
# get rectangle bounding contour
[_, _, w, h] = cv2.boundingRect(contour)
# remove any small contours present
if h < 40 or w < 40:
return False
return True
# read the image
image = os.path.join(os.getcwd(), "assets/img/digits-overlay.jpg")
cv_image = cv2.imread(image)
# convert to grayscale
gray_image = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)
# apply some thresholding to convert to binary image (inverted)
_, thresh = cv2.threshold(gray_image, 150, 255, cv2.THRESH_BINARY_INV)
# apply some morphological operation (dilation)
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))
dilated = cv2.dilate(thresh, kernel, iterations=5)
# box out all possible characters present in the image
num_characters = recognize_characters(dilated, cv_image, random_shape_color=True, filter_fn=filter_contours)
print(f"Number of Boxes/Connected Components: {num_characters}")
cv2.imshow('Output', cv_image)
cv2.waitKey(0)
cv2.destroyAllWindows()