clear all;clc;
I=imread('Car4.JPG');
figure(1);imshow(I);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%颜色空间转换
img=rgb2hsv(I);
figure(2);imshow(img);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%构造五级灰度图
[m,n,l]=size(img);
for i=1:m
for j=1:n
if img(i,j,1)>=0.55 && img(i,j,1)<=0.70 && img(i,j,2)>=0.25 && img(i,j,2)<=1 && img(i,j,3)>=0.3 && img(i,j,3)<=1
Grayimg(i,j)=255;
% elseif img(i,j,1)>=0.06 && img(i,j,1)<=0.15 && img(i,j,2)>=0.9 && img(i,j,2)<=1 && img(i,j,3)>=0.3 && img(i,j,3)<=0.7
elseif img(i,j,1)>=0.06 && img(i,j,1)<=0.16 && img(i,j,2)>=0.9 && img(i,j,2)<=1 && img(i,j,3)>=0.3 && img(i,j,3)<=1
Grayimg(i,j)=200;
elseif img(i,j,3)>=0.9 && img(i,j,3)<=1
Grayimg(i,j)=150;
elseif img(i,j,3)>=0 && img(i,j,3)<=0.2
Grayimg(i,j)=100;
else
Grayimg(i,j)=0;
end
end
end
Grayimg=uint8(Grayimg);
figure(3);imshow(Grayimg);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%根据不同的灰度级进行二值化,得到四幅而值化图像
BW1=Grayimg;
for i=1:m
for j=1:n
if Grayimg(i,j)~=255
BW1(i,j)=0;
end
end
end
BW2=Grayimg;
for i=1:m
for j=1:n
if Grayimg(i,j)==200
BW2(i,j)=255;
else
BW2(i,j)=0;
end
end
end
BW3=Grayimg;
for i=1:m
for j=1:n
if Grayimg(i,j)==150
BW3(i,j)=255;
else
BW3(i,j)=0;
end
end
end
BW4=Grayimg;
for i=1:m
for j=1:n
if Grayimg(i,j)==100
BW4(i,j)=255;
else
BW4(i,j)=0;
end
end
end
figure(4);subplot(221);imshow(BW1);subplot(222);imshow(BW2);
subplot(223);imshow(BW3);subplot(224);imshow(BW4);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%BW1
%利用开闭运算的方法消除噪声
se=strel('square',5);
BW1=imopen(BW1,se);
BW1=imclose(BW1,se);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%填孔处理
BW1=imfill(BW1,'holes');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
se1=strel('square',10);
% BW1=imerode(BW1,se1);
BW1=imdilate(BW1,se1);
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%BW2
%利用开闭运算的方法消除噪声
se=strel('square',5);
BW2=imopen(BW2,se);
BW2=imclose(BW2,se);
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%填孔处理
BW2=imfill(BW2,'holes');
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
se1=strel('square',10);
% BW2=imerode(BW2,se1);
BW2=imdilate(BW2,se1);
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%BW3
%利用开闭运算的方法消除噪声
se=strel('square',10);
BW3=imopen(BW3,se);
BW3=imclose(BW3,se);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%填孔处理
BW3=imfill(BW3,'holes');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
se1=strel('square',16);
BW3=imdilate(BW3,se1);
% BW3=imerode(BW3,se1);
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%BW4
%利用开闭运算的方法消除噪声
se=strel('square',5);
BW4=imopen(BW4,se);
BW4=imclose(BW4,se);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%填孔处理
BW4=imfill(BW4,'holes');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
se1=strel('square',8);
BW4=imerode(BW4,se1);
BW4=imdilate(BW4,se1);
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure(5);subplot(221);imshow(BW1);subplot(222);imshow(BW2);
subplot(223);imshow(BW3);subplot(224);imshow(BW4);
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[L,num]=bwlabeln(BW1,4);
for i=1:num;
[r,c]=find(L==i);
r_temp=max(r)-min(r);
c_temp=max(c)-min(c);
temp=size(r);
if (c_temp/r_temp>4)|(c_temp/r_temp<3)
%车牌区域的长宽比在3~4范围内
for j=1:temp(1);
L(r(j),c(j))=0;
end
else
continue;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if L==0
[L,num]=bwlabeln(BW2,4);
for i=1:num;
[r,c]=find(L==i);
r_temp=max(r)-min(r);
c_temp=max(c)-min(c);
temp=size(r);
if (c_temp/r_temp>4)|(c_temp/r_temp<3)
%车牌区域的长宽比在3~4范围内
for j=1:temp(1);
L(r(j),c(j))=0;
end
else
continue;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if L==0
[L,num]=bwlabeln(BW3,4);
for i=1:num;
[r,c]=find(L==i);
r_temp=max(r)-min(r);
c_temp=max(c)-min(c);
temp=size(r);
if (c_temp/r_temp>4)|(c_temp/r_temp<3)
%车牌区域的长宽比在3~4范围内
for j=1:temp(1);
L(r(j),c(j))=0;
end
else
continue;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure(6);imshow(L);
BW=L;
%水平投影
[M,N]=size(BW);
for i=1:M
sum1(i)=0;
end
for i=1:M
for j=1:N
sum1(i)=BW(i,j)+sum1(i);
end
end
figure;plot(sum1);
for i=1:1:M
if(sum1(i)>60)
top=i;
break;
end
end
for i=M:-1:1
if( sum1(i)>60)
bottom=i;
break;
end
end
top=top-2;
bottom=bottom+2;
BW1=BW(top:bottom,:);
figure;imshow(BW1);
%垂直投影
[M,N]=size(BW1);
for i=1:N
sum2(i)=0;
end
for i=1:N
for j=1:M
sum2(i)=BW1(j,i)+sum2(i);
end
end
figure;plot(sum2);
for i=1:N
if( sum2(i)>5)
left=i;
break
end
end
for i=N:-1:1
if(sum2(i)>8)
right=i;
break
end
end
left=left-5;
right=right+5;
BW2=I(top:bottom,left:right,:);
figure;imshow(BW2);
- 1
- 2
- 3
前往页