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 )
0 commit comments