Skip to content

Commit 43fee93

Browse files
author
Nipuna Sudharaka
committed
Add sample seven seg variations, and preset operations to prepair them for OCR
1 parent 61b7de6 commit 43fee93

7 files changed

+59
-1
lines changed

learn-opencv.py

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def initTool():
4747
img_cropped = cv2.morphologyEx(img_cropped, cv2.MORPH_CLOSE, kernel)
4848
img_cropped = cv2.morphologyEx(img_cropped, cv2.MORPH_OPEN, kernel)
4949
# ------------------------
50+
# ------------------------
5051
txt = tool.image_to_string(
5152
CV2PIL(img_cropped),
5253
lang=lang,

preprocessings.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# ============================= ONE (light red chars on dark red background)===============
2+
3+
# img_ori = cv2.imread('processing/digits copy.jpg')
4+
img_ori = cv2.imread('processing/digits-dot.jpg')
5+
img_cropped = img_ori[146:201, 48: 304].copy()
6+
img_marked = cv2.rectangle(img_ori, (48, 146), (304, 201), (0, 255, 0), 1)
7+
8+
img_cropped = cv2.medianBlur(img_cropped, 5)
9+
img_cropped = cv2.cvtColor(img_cropped, cv2.COLOR_BGR2GRAY)
10+
img_cropped = cv2.bitwise_not(img_cropped)
11+
img_cropped = cv2.adaptiveThreshold(img_cropped, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 7, 2)
12+
cv2.imshow("after threshold", img_cropped)
13+
cv2.moveWindow("after threshold", 50, 650)
14+
# ------------------------
15+
kernel = np.ones((3, 3), np.uint8) # building a kernel, a unit of operation
16+
# --------------------------------
17+
img_cropped = cv2.morphologyEx(img_cropped, cv2.MORPH_CLOSE, kernel)
18+
img_cropped = cv2.morphologyEx(img_cropped, cv2.MORPH_OPEN, kernel)
19+
# ------------------------
20+
21+
# ============================= TWO (pure red char on black)================================
22+
23+
img_cropped = cv2.medianBlur(img_cropped, 5)
24+
# img_cropped = cv2.cvtColor(img_cropped, cv2.COLOR_BGR2GRAY)
25+
img_cropped = cv2.cvtColor(img_cropped, cv2.COLOR_BGR2HSV)
26+
lower_red = np.array([0, 50, 50])
27+
upper_red = np.array([15, 255, 255])
28+
29+
img_cropped = cv2.inRange(img_cropped, lower_red, upper_red)
30+
31+
img_cropped = cv2.bitwise_not(img_cropped)
32+
# ------------------------
33+
kernel = np.ones((3, 3), np.uint8) # building a kernel, a unit of operation
34+
# --------------------------------
35+
img_cropped = cv2.morphologyEx(img_cropped, cv2.MORPH_CLOSE, kernel)
36+
img_cropped = cv2.morphologyEx(img_cropped, cv2.MORPH_OPEN, kernel)
37+
# ------------------------
38+
39+
#============================== THREE (too bright digits, off segments visible)
40+
41+
img_cropped = cv2.medianBlur(img_cropped, 5)
42+
lower_red = np.array([0, 0, 200])
43+
upper_red = np.array([50, 255, 255])
44+
45+
ret, img_cropped = cv2.threshold(img_cropped, 127, 255, cv2.THRESH_BINARY)
46+
cv2.imshow("after threshold", img_cropped)
47+
cv2.moveWindow("after threshold", 50, 650)
48+
img_cropped = cv2.bitwise_not(img_cropped)
49+
img_cropped = cv2.cvtColor(img_cropped, cv2.COLOR_BGR2GRAY)
50+
ret, img_cropped = cv2.threshold(img_cropped, 127, 255, cv2.THRESH_BINARY)
51+
# ------------------------
52+
kernel = np.ones((3, 3), np.uint8) # building a kernel, a unit of operation
53+
# --------------------------------
54+
img_cropped = cv2.morphologyEx(img_cropped, cv2.MORPH_CLOSE, kernel)
55+
img_cropped = cv2.morphologyEx(img_cropped, cv2.MORPH_OPEN, kernel)

processing/digits2.jpg

22 KB
Loading

processing/digits3.jpg

11.6 KB
Loading

processing/digits4.jpg

16.8 KB
Loading

processing/digits5.jpg

15.9 KB
Loading

requirements.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
numpy
22
pyocr
3-
pillow
3+
pillow
4+
tesseract
5+
pytesseract

0 commit comments

Comments
 (0)