You are on page 1of 3

clear;

clc;
close all;
% lay anh dau vao
[file,path]=uigetfile({'*.jpg;*.bmp;*.png;*.tif'},'Choose an image');

s=[path,file];
image=imread(s);
subplot(2,3,1);
imshow(image);
if size(image,3)==3
graypic=rgb2gray(image);
end
picture = medfilt2(graypic,[3 3]);
threshold = graythresh(picture); % Tim nguong xam bang pp otsu
picture = imbinarize(picture,threshold); % Chuyen anh xam sang bw bang nguong xam
picture = imfill(picture,'hole');
% dien tich cua bien so xe nam trong khoang tu 1500 den 6500 pixel
picture1 = bwareaopen(picture,6500); % Xoa cac vung mau trang nho hon 6500 pixel
bsx = picture - picture1; % Anh chua nhung vung mau trang nho hn 6500 pixel
picture = bwareaopen(bsx,1500); % Xoa cac vung mau trang nho hon 1500 pixel
subplot(2,3,4);
imshow(picture);

LP_MAX_RATIO = 0.94;
LP_MIN_RATIO = 0.73;
dilated_pic = imdilate(picture, strel('square', 5));
stat = regionprops(logical(dilated_pic));
L = length([stat.Area]); % So vung mau trang
r = [];
% kiem tra cac tinh chat dac trung cua cac vung trang
for i = 1 : L
S = stat(i).Area; % dien tich cua vung trang thu i
W = stat(i).BoundingBox(3); % Chieu ngang cua vung hinh chu nhat chua vung trang
thu i
H = stat(i).BoundingBox(4); % Chieu cao cua vung hinh chu nhat chua vung trang thu
i
T1 = (LP_MAX_RATIO)*W; % Ty so thu nhat
T2 = (LP_MIN_RATIO)*W; % Ty so thu hai
T3 = S/(H*W); % Ty so thu 3
M = max([stat.Area]); % Dien tich vung trang lon nhat
if H <= T1 && H >= T2 && T3 <1 && T3 > 0.4 && H*W <10000 % Kiem tra dieu kien
r = [r stat(i).Area]; % Mang mot chieu r
end
end
l= length(r);
if(l == 0) % Neu mang r khong co gia tri nao
% thi bao khong tim dc bien so
delete(s);
file = fopen('number_Plate.txt', 'wt');
fprintf(file,'%s',' Khong xac dinh duoc bien so xe');
fclose(file);
winopen('number_Plate.txt')
return
else % Nguoc lai bien so se vung thu i ...
%sao cho vung do == vung lon nhat trong mang r
index = find([stat.Area] == max(r));
end
x = ceil(stat(index).BoundingBox(1));
y = ceil(stat(index).BoundingBox(2));
width = ceil(stat(index).BoundingBox(3));
height = ceil(stat(index).BoundingBox(4));
y2 = y + height;
x2 = x + width;
a = picture(y:y2, x:x2, :);
subplot(2,3,2);
imshow(a);
licen_plate = image(y:y2, x:x2, :);
subplot(2,3,5);
imshow(licen_plate);
% su dung correlation de nhan dang so
% Xoay bien so
R = [];
% Anh nam ngang thi chieu cao h cua boundingbox la min = hmin
% Ta xoay anh 180 lan , de tim hmin
for i=0:180
pic = imrotate(a ,i, 'bilinear');
stat2 = regionprops(logical(pic));
h = stat2.BoundingBox(4);
R = [R h];
end
hmin = min(R);
i1 = imrotate(a, 0, 'bilinear');% xoay anh 1 goc alpha bat ky --> i1
i2 = imrotate(a, 3, 'bilinear');% xoay anh 1 goc beta > alpha --> i2
I1 = regionprops(logical(i1));
I2 = regionprops(logical(i2));
h1 = I1.BoundingBox(4);% tim chieu cao cua boundingbox cua i1 --> h1
h2 = I2.BoundingBox(4);% tim chieu cao cua boundingbox cua i2 --> h2
i = 0;
while(h2<=h1)
% so sanh h1 voi h2 :
% Neu h2<=h1 co nghia la ta nen xoay anh voi goc' tang dan den khi h=hmin
pic = imrotate(a, i, 'bilinear');
pic_stat = regionprops(logical(pic));
C = pic_stat.BoundingBox(4);
if C == hmin
bsx = imrotate(licen_plate, i, 'bilinear');
break
end
i=i+1;
end
while(h2>h1) % Neu h2>h1 thi co nghia la ta phai xoay anh theo chieu nguoc lai de h
giam dan ve hmin
pic = imrotate(a, i, 'bilinear');
pic_stat = regionprops(logical(pic));
C = pic_stat.BoundingBox(4);
if C == hmin
bsx = imrotate(licen_plate, i, 'bilinear');
break
end
i = i-1;
end
subplot(2,3,3);
imshow(bsx);
pic = rgb2gray(bsx);
threshold_pic = graythresh(pic);
bsx = ~im2bw(pic,threshold_pic);
bsx1 = bwareaopen(bsx,200);
BSX = bsx - bsx1;
BSX = bwareaopen(BSX,20);
subplot(2,3,6);
imshow(BSX);

You might also like