Skip to content

Commit f7225ee

Browse files
committed
Less noise and better contours
1 parent e0a97fe commit f7225ee

6 files changed

+10
-17
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
*.pyc
22
*.npz
3-
*.png
43
*.csv
54
*.py.swp
65
*.idea
Loading
Loading
Loading
Loading

Character_Segmentation/main.py

+10-16
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,16 @@ def line_array(array):
4040

4141
grey_img = cv2.cvtColor(src_img, cv2.COLOR_BGR2GRAY)
4242

43-
# gud_img = cv2.adaptiveThreshold(grey_img,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV,1001,2)
44-
gud_img = cv2.adaptiveThreshold(grey_img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,1001,2)
43+
gud_img = cv2.adaptiveThreshold(grey_img,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV,101,2)
4544

4645
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
4746
noise_remove = cv2.erode(gud_img,kernel,iterations = 2)
4847

49-
50-
# kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
5148
kernel1 = np.array([[1,0,1],[0,1,0],[1,0,1]], dtype = np.uint8)
52-
final_thr = cv2.dilate(gud_img,kernel1,iterations = 2)
53-
54-
55-
56-
# closing = cv2.morphologyEx(gud_img, cv2.MORPH_OPEN, kernel, iterations = 1) # To remove "pepper-noise"
57-
# final_thr = np.empty(final.shape, dtype=np.uint8)
58-
# cv2.bitwise_not(final1, final_thr)
5949

50+
opening = cv2.morphologyEx(gud_img, cv2.MORPH_OPEN, kernel, iterations = 2) # To remove "pepper-noise"
51+
kernel1 = np.array([[1,0,1],[0,1,0],[1,0,1]], dtype = np.uint8)
52+
final_thr = cv2.dilate(noise_remove,kernel1,iterations = 3)
6053

6154
#-------------/Thresholding Image-------------#
6255

@@ -71,14 +64,12 @@ def line_array(array):
7164
# print(count_x[y])
7265

7366
line_list = line_array(count_x)
74-
print(line_list)
67+
# print(line_list)
7568

7669
# t = np.arange(0,height, 1)
7770
# plt.plot(t, count_x[t])
7871
# plt.axis([0, height, 0, 350])
79-
# plt.show()
8072

81-
# k = cv2.waitKey(0)
8273

8374
# for y in range(len(line_list)):
8475
# if :
@@ -118,8 +109,9 @@ def line_array(array):
118109
cv2.drawContours(final_contr, contours, -1, (0,255,0), 3)
119110

120111
for cnt in contours:
121-
x,y,w,h = cv2.boundingRect(cnt)
122-
cv2.rectangle(src_img,(x,y),(x+w,y+h),(0,255,0),2)
112+
if cv2.contourArea(cnt) > 100:
113+
x,y,w,h = cv2.boundingRect(cnt)
114+
cv2.rectangle(src_img,(x,y),(x+w,y+h),(0,255,0),2)
123115

124116
#-------------/Character segmenting-----------#
125117

@@ -137,6 +129,8 @@ def line_array(array):
137129
cv2.imshow("Threshold Image", final_thr)
138130
cv2.imshow("Contour Image", final_contr)
139131

132+
# plt.show()
133+
140134
#-------------/Displaying Image---------------#
141135

142136

0 commit comments

Comments
 (0)