jsjuan commited on
Commit
e3565a1
1 Parent(s): ba6f338

Upload segementing_Letter_Using_CV2.py

Browse files
Files changed (1) hide show
  1. segementing_Letter_Using_CV2.py +76 -0
segementing_Letter_Using_CV2.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ from local_utils import detect_lp
3
+ from get_plate import get_plate
4
+ #for test
5
+ # from transfer import load_model
6
+ # from preprocessing import preprocess_image
7
+ # import glob
8
+ # import matplotlib.pyplot as plt
9
+ # from os.path import splitext,basename
10
+ # import matplotlib.gridspec as gridspec
11
+
12
+
13
+ # 1. see what it looks like in different types: plate_image, gray, blur, binary,thre_mor
14
+ def DiffImage(LpImg):
15
+ if (len(LpImg)): #check if there is at least one license image
16
+ # Scales, calculates absolute values, and converts the result to 8-bit.
17
+ plate_image = cv2.convertScaleAbs(LpImg[0], alpha=(255.0))
18
+
19
+ # convert to grayscale and blur the image
20
+ #
21
+ gray = cv2.cvtColor(plate_image, cv2.COLOR_BGR2GRAY)
22
+ blur = cv2.GaussianBlur(gray,(7,7),0)
23
+
24
+ # Applied inversed thresh_binary
25
+ binary = cv2.threshold(blur, 180, 255,
26
+ cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
27
+
28
+ kernel3 = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
29
+ thre_mor = cv2.morphologyEx(binary, cv2.MORPH_DILATE, kernel3)
30
+
31
+ return plate_image,gray,blur,binary,thre_mor
32
+
33
+
34
+
35
+ # Create sort_contours() function to grab the contour of each digit from left to right
36
+ def sort_contours(cnts,reverse = False):
37
+ i = 0
38
+ boundingBoxes = [cv2.boundingRect(c) for c in cnts]
39
+ (cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes),
40
+ key=lambda b: b[1][i], reverse=reverse))
41
+ return cnts
42
+
43
+
44
+ def get_Crop_Letter(wpod_net,image):
45
+
46
+ vehicle, LpImg,cor = get_plate(wpod_net,image)
47
+ plate_image,gray,blur,binary,thre_mor=DiffImage(LpImg)
48
+ cont, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
49
+ #print(len(cont))
50
+ # creat a copy version "test_roi" of plat_image to draw bounding box
51
+ test_roi = plate_image.copy()
52
+
53
+ # Initialize a list which will be used to append charater image
54
+ crop_characters = []
55
+
56
+ # define standard width and height of character
57
+ digit_w, digit_h = 30, 60
58
+
59
+ for c in sort_contours(cont):
60
+ (x, y, w, h) = cv2.boundingRect(c)
61
+ ratio = h/w
62
+ if 1<=ratio<=5: # Only select contour with defined ratio
63
+ if h/plate_image.shape[0]>=0.5: # Select contour which has the height larger than XXX% of the plate
64
+ # Draw bounding box arroung digit number
65
+ cv2.rectangle(test_roi, (x, y), (x + w, y + h), (0, 255,0), 2 )
66
+
67
+ # Sperate number and gibe prediction
68
+ curr_num = thre_mor[y:y+h,x:x+w]
69
+ curr_num = cv2.resize(curr_num, dsize=(digit_w, digit_h))
70
+ _, curr_num = cv2.threshold(curr_num, 200, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
71
+ crop_characters.append(curr_num)
72
+ #print("Detect {} numbers!".format(len(crop_characters)))
73
+ return test_roi,crop_characters
74
+
75
+
76
+