-
Notifications
You must be signed in to change notification settings - Fork 0
/
CPdetect.m
229 lines (213 loc) · 6.47 KB
/
CPdetect.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
function id = CPdetect(img,letter_templates)
%img = imread('016_CP10_HP10_SD200_5622_8.jpg');
% if size(img,3)==3
% gray_img = min(img,[],3);
% end
% % bw_img = imbinarize(gray_img);
% % bw = bwareaopen(bw_img,50);
% % se = strel('disk',1);
% % bw = imclose(bw,se);
% % bw = imfill(bw,'holes');
% [x1,y1] = size(gray_img);
% %image =bw((x1/100:x1/8),(y1/10:y1/1.2));
% image =gray_img((x1/100:x1/5),(y1/10:y1/1.2));
% [r_image c_image] = size(image);
% for rol= 1:r_image
% for col= 1:c_image
% if image(rol,col)<150
% image(rol,col)=0;
% else
% image(rol,col)=255;
% end
% end
% end
% image = bwareaopen(image,50);
if size(img,3)==3
gray_img = min(img,[],3);
else
gray_img = img;
end
gray_img = imresize(gray_img, [1000 2000]);
%imshow(gray_img);
%pause(3)
%bw_img = imbinarize(gray_img);
[x1,y1] = size(gray_img);
%image =gray_img((x1/30:x1/8),(y1/10:y1/1.2));
%image =gray_img((x1/30:x1/8),(y1/3:y1/1.6));CP
%image =gray_img((x1/30:x1/8),(y1/3:y1/1.6));
%image =gray_img((x1/30:x1/2),(y1/3:y1/1.6));CP
image =gray_img((x1/30:x1/8),(y1/3:y1/1.6));
% bw = bwareaopen(image,5);
% se = strel('disk',1);
% bw = imclose(bw,se);
% bw = imfill(bw,'holes');
[r_image c_image] = size(image);
for rol= 1:r_image
for col= 1:c_image
if image(rol,col)<180
image(rol,col)=0;
end
end
end
bw = bwareaopen(image,50);
%bw = bwareaopen(image,20);
%imshow(bw);
%pause(3)
% to find the correct number after cp in the cropped Image
found_chars =[];
iteration =0;
first_line1 ='';
final_str ='';
while 1
num_chars_here = 0;
cropped_image_line = getnonzerocomponents(bw);
% finding the total size of the character size
line_size = size(cropped_image_line,1);
for i=1:line_size
sum_char_line = sum(cropped_image_line(i,:));
if sum_char_line ==0
first_line = cropped_image_line(1:i-1,:);
first_line1 = getnonzerocomponents(first_line);
rest_line = cropped_image_line(i:end,:);
rest_line1 = getnonzerocomponents(rest_line);
break;
else
first_line1=cropped_image_line;rest_line1=[ ];
end
end
if isempty(first_line1)
break;
end
cropped_image=first_line1;
while 1
num_chars_here= num_chars_here+1;
% getting the non zero element from the cropped image
cropped_image = getnonzerocomponents(cropped_image);
% finding the total size of the character size
char_size = size(cropped_image,2);
for i=1:char_size
sum_char = sum(cropped_image(:,i));
if sum_char ==0
first_char = cropped_image(:,1:i-1);
first_char1 = getnonzerocomponents(first_char);
rest_char = cropped_image(:,i:end);
rest_char1 = getnonzerocomponents(rest_char);
break;
else
first_char1=cropped_image;rest_char1=[ ];
end
end
current_char2 = imresize(first_char1,[42 24]);
%imshow(current_char2);
%pause(8)
cropped_image = rest_char1;
total_tem_char = size(letter_templates,2);
mat_chars =[];
for it = 1:total_tem_char
mat_char = corr2(letter_templates{1,it},current_char2) ;
mat_chars=[mat_chars mat_char];
end
idx = find(mat_chars==max(mat_chars));
if idx == 1
character_matched = 'C';
elseif idx == 2
character_matched = 'C';
elseif idx == 3
character_matched = 'C';
elseif idx == 4
character_matched = 'H';
elseif idx == 5
character_matched = 'P';
elseif idx == 6
character_matched = 'P';
elseif idx == 7
character_matched = 'P';
elseif idx == 8
character_matched = '/';
elseif idx == 9
character_matched = '1';
elseif idx == 10
character_matched = '1';
elseif idx == 11
character_matched = '2';
elseif idx == 12
character_matched = '2';
elseif idx == 13
character_matched = '3';
elseif idx == 14
character_matched = '4';
elseif idx == 15
character_matched = '4';
elseif idx == 16
character_matched = '5';
elseif idx == 17
character_matched = '5';
elseif idx == 18
character_matched = '6';
elseif idx == 19
character_matched = '6';
elseif idx == 20
character_matched = '7';
elseif idx == 21
character_matched = '8';
elseif idx == 22
character_matched = '8';
elseif idx == 23
character_matched = '9';
elseif idx == 24
character_matched = '9';
elseif idx == 25
character_matched = '0';
elseif idx == 26
character_matched = 'H';
elseif idx == 27
character_matched = 'E';
elseif idx == 28
character_matched = 'E';
elseif idx == 29
character_matched = '00';
else
character_matched = ' ';
end
found_chars=[found_chars character_matched];
if isempty(rest_char1)
break;
end
end
if (isempty(rest_line1)) || (~isempty(findstr(found_chars,'C')) && ~isempty(findstr(found_chars,'P'))) || iteration>10000
final_str = found_chars;
break;
end
found_chars=[];
bw = rest_line1;
iteration=iteration+1;
end
if isempty((final_str)) || isempty(findstr(final_str,'CP'))
final_str='CP1541';
end
% idx_cp =findstr(final_str,'CP');
% id_now =final_str((idx_cp(1)+2) :end)
% id_numr =[];
% for str =1:size(id_now,2)
% if findstr(id_now(1,str),'0987654321')>1
% id_numr = [id_numr id_now(1,str)]
% end
% end
% id =str2num((id_numr));
idx_cp =findstr(final_str,'CP');
%number= final_str((idx_cp(1)+2) :end)
%id =str2num(final_str((idx_cp(1)+2) :end));
id_now =final_str((idx_cp(1)+2) :end);
id_numr =[];
for str =1:size(id_now,2)
if findstr(id_now(1,str),'0987654321')>0
id_numr = [id_numr id_now(1,str)];
end
end
if isempty(id_numr)
id_numr='222';
end
id =str2num((id_numr));
function im = getnonzerocomponents(com)
[row col] = find(com);
im = com(min(row):max(row),min(col):max(col));