-
Notifications
You must be signed in to change notification settings - Fork 1
/
japbend.py
executable file
·58 lines (41 loc) · 1.55 KB
/
japbend.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
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python
# japbend.py
#
# Copyright(c) Exequiel Ceasar Navarrete <[email protected]>
# Licensed under MIT
# Version 1.0.1
import os
import cv2
import numpy as np
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 large contours present
if h > 300 and w > 300:
return False
# remove any small contours present
if h < 10 or w < 10:
return False
return True
# read the image
image = os.path.join(os.getcwd(), "assets/img/japbend.jpg")
cv_image = cv2.imread(image)
# convert to grayscale
gray_image = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)
# apply some smoothing and thresholding to convert to binary image (inverted)
blur = cv2.GaussianBlur(gray_image, (5, 5), 0)
processed = cv2.bilateralFilter(blur, 15, 75, 75)
filter_kernel = np.ones((5, 5), np.float32) / 25
processed = cv2.filter2D(processed, -1, filter_kernel)
thresh = cv2.adaptiveThreshold(processed, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
# apply some morphological operation (dilation)
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))
dilated = cv2.dilate(thresh, kernel, iterations=1)
# 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()