-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlocate.m
executable file
·141 lines (123 loc) · 2.88 KB
/
locate.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
function [result,plate,rs,cs,h,w]=locate(input,set)
%% prepare data
origimg=input;
%origimgin=imread(input);
%[height,width]=size(origimgin);
% resizee=floor(width*800/height/3);
% origimg = imresize(origimgin, [800 resizee]);
[height,width]=size(origimg);
bw=zeros(height,width/3);
pixel=height*width/3;
%{
%% image pre-process
r_row = sort(reshape(origimg(:,:,1),[pixel,1]));
g_row = sort(reshape(origimg(:,:,2),[pixel,1]));
b_row = sort(reshape(origimg(:,:,3),[pixel,1]));
r_topmark=(sum(r_row(floor(pixel*0.75):pixel))/(pixel-floor(pixel*0.75)))/255;
g_topmark=(sum(g_row(floor(pixel*0.75):pixel))/(pixel-floor(pixel*0.75)))/255;
b_topmark=(sum(b_row(floor(pixel*0.75):pixel))/(pixel-floor(pixel*0.75)))/255;
img = imadjust(origimg,[.3 .3 .3; r_topmark g_topmark b_topmark],[]);
%}
img = imadjust(origimg,[.3 .3 .3; 1 1 1],[]);
%threshold=graythresh(img);
bw=im2bw(img);
% undot
undot=ceil(pixel*0.00001);
newbw=bwareaopen(~bw,undot);
newbw=~newbw;
%newbw=bw;
% label
[bw_cont,num_cont]=bwlabel(newbw);
for i=1:num_cont
[r,c]=find(bw_cont==i);
xr=max(r);
nr=min(r);
xc=max(c);
nc=min(c);
htemp=xr-nr;
wtemp=xc-nc;
ratio=wtemp/htemp;
if ratio>1.5 && ratio<4
temp=newbw(nr:xr,nc:xc);
turn = countturn(temp);
if turn>10 && turn<=25
%platebw=newbw(min(r):max(r),min(c):max(c));
%platepre=origimg(min(r):max(r),min(c):max(c));
%plate=rotate(platebw,platepre);
platte=origimg(nr:xr,nc:xc,:);
result_temp=readnumber(platte);
if result_temp~=('x')
rs=nr;
cs=nc;
h=htemp;
w=wtemp;
result=result_temp;
plate=platte;
break;
end
end
end
end
if exist('plate','var')==0
plate=0;
rs=0;
cs=0;
h=0;
w=0;
result='x';
end
function turn=countturn(imgin)
imgin=~imgin;
[h,w]=size(imgin);
high=ceil(h*0.55);
low=ceil(h*0.45);
turn=0;
b=zeros(1,w);
for i=1:w
if sum(imgin(low:high,i))==0
b(i)=0;
else
b(i)=1;
end
end
for i=1:w-1
if (b(i)~=b(i+1))
turn=turn+1;
end
end
function final=rotate(ref,todo)
[h,w]=size(ref);
state=0;
low=h;
while state==0
if ref(low,1)==0
low=low-1;
else
state=1;
end
end
high=floor(low/2);
right=1;
state=0;
while state==0
if ref(high,right)==0
right=right+1;
else
state=1;
end
end
angle=radtodeg(atan(right/high));
if angle>1
if angle<45
afterrotate=imrotate(todo,angle);
cut=low*sin(angle*pi/180);
else
afterrotate=imrotate(todo,angle-45);
cut=(h-low)*sin((angle-45)*pi/180);
end
[ah,aw]=size(afterrotate);
addcut=aw*0.025;
final=afterrotate(:,cut+addcut:aw-cut-addcut);
else
final=todo;
end