Skip to content

Commit e0a97fe

Browse files
committed
Line segmention and basic contours usage
1 parent 3bb4569 commit e0a97fe

7 files changed

+120
-16
lines changed

Character_Segmentation/data/img_2.jpg

-907 KB
Loading
Loading
Loading
Loading

Character_Segmentation/main.py

+120-16
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,145 @@
11
import cv2
22
import numpy as np
3-
import sys
4-
import time
3+
import matplotlib.pyplot as plt
4+
5+
np.set_printoptions(threshold='nan')
6+
7+
8+
def closewindows():
9+
k = cv2.waitKey(0)
10+
if k & 0xFF == ord('s'):
11+
comment = input("Comment:-\n ")
12+
cv2.imwrite('./data/test_result/'+comment+'_thres'+'.jpg',final_thr)
13+
cv2.imwrite('./data/test_result/'+comment+'_src'+'.jpg',src_img)
14+
cv2.imwrite('./data/test_result/'+comment+'_contr'+'.jpg',final_contr)
15+
print("Completed")
16+
elif k & 0xFF == int(27):
17+
cv2.destroyAllWindows()
18+
else:
19+
closewindows()
20+
21+
def line_array(array):
22+
list_x = []
23+
for y in range(len(array)):
24+
if all(i >= 3 for i in array[y:y+9]) == True:
25+
list_x.append(y-1)
26+
return list_x
27+
528

629
#-------------Thresholding Image--------------#
730

831
src_img = cv2.imread('./data/img_2.jpg', 1)
32+
# copy = src_img.copy()
33+
# src_img = cv2.resize(copy, dsize =(1500, 1000), interpolation = cv2.INTER_AREA)
34+
height = src_img.shape[0]
35+
width = src_img.shape[1]
36+
print("#----------------------------#")
37+
print("Image Info:-")
38+
print("Height =",height,"\nWidth =",width)
39+
print("#----------------------------#")
40+
941
grey_img = cv2.cvtColor(src_img, cv2.COLOR_BGR2GRAY)
1042

11-
gud_img = cv2.adaptiveThreshold(grey_img,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,101,2)
12-
# ret3,thr_img = cv2.threshold(gud_img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
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)
1345

1446
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
15-
closing = cv2.morphologyEx(gud_img, cv2.MORPH_CLOSE, kernel, iterations = 2) # To remove "pepper-noise"
47+
noise_remove = cv2.erode(gud_img,kernel,iterations = 2)
1648

49+
50+
# kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
1751
kernel1 = np.array([[1,0,1],[0,1,0],[1,0,1]], dtype = np.uint8)
18-
final = cv2.erode(closing,kernel1,iterations = 1)
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)
59+
60+
61+
#-------------/Thresholding Image-------------#
62+
63+
64+
#-------------Line Detection------------------#
65+
66+
count_x = np.zeros(shape= (height))
67+
for y in range(height):
68+
for x in range(width):
69+
if noise_remove[y][x] == 255 :
70+
count_x[y] = count_x[y]+1
71+
# print(count_x[y])
72+
73+
line_list = line_array(count_x)
74+
print(line_list)
75+
76+
# t = np.arange(0,height, 1)
77+
# plt.plot(t, count_x[t])
78+
# plt.axis([0, height, 0, 350])
79+
# plt.show()
80+
81+
# k = cv2.waitKey(0)
82+
83+
# for y in range(len(line_list)):
84+
# if :
85+
# main_list.append(line_list[y-1]+10)
86+
# main_list.append(line_list[y]-5)
87+
88+
# main_list.append(line_list[-1]+10)
89+
90+
# print(main_list)
91+
92+
# for y in main_list:
93+
# src_img[y][:] = (0 , 255,0)
94+
95+
96+
97+
# final_thr = cv2.erode(final_thr,kernel1,iterations = 1)
98+
99+
100+
#----------------------------------------------------------#
101+
102+
103+
104+
105+
106+
107+
108+
#-------------------------------------------------------------#
109+
110+
111+
#-------------Character segmenting------------#
112+
113+
chr_img = final_thr.copy()
114+
115+
contr_img, contours, hierarchy = cv2.findContours(chr_img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
116+
# print(len(contours))
117+
final_contr = np.zeros((final_thr.shape[0],final_thr.shape[1],3), dtype = np.uint8)
118+
cv2.drawContours(final_contr, contours, -1, (0,255,0), 3)
119+
120+
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)
123+
124+
#-------------/Character segmenting-----------#
125+
126+
19127

20-
# blur = cv2.bilateralFilter(erosion,9,75,75)
21128

22129

23130
#-------------Displaying Image----------------#
24131

25132
cv2.namedWindow('Source Image', cv2.WINDOW_NORMAL)
26133
cv2.namedWindow('Threshold Image', cv2.WINDOW_NORMAL)
134+
cv2.namedWindow('Contour Image', cv2.WINDOW_NORMAL)
27135

28136
cv2.imshow("Source Image", src_img)
29-
cv2.imshow("Threshold Image", final)
137+
cv2.imshow("Threshold Image", final_thr)
138+
cv2.imshow("Contour Image", final_contr)
139+
140+
#-------------/Displaying Image---------------#
30141

31142

32143
#-------------Closing Windows-----------------#
33144

34-
k = cv2.waitKey(0)
35-
if k & 0xFF == ord('s'):
36-
comment = input("Comment:-\n ")
37-
cv2.imwrite('./data/test_result/'+comment+'.jpg',final)
38-
# cv2.imwrite('OKAY.jpg',final)
39-
print("Completed")
40-
else:
41-
cv2.destroyAllWindows()
145+
closewindows()
Binary file not shown.

0 commit comments

Comments
 (0)