% function I=preproc(filename)
clear;
clc;
close all;
[fn,pn,fi]=uigetfile('*.jpg','选择图片');
filename=[pn fn];
%%%%%
%函数功能:对输入的车牌图像进行预处理
%提取图像的边缘信息以以及对图像进行开操作运算;
%输入:filename是数据图像的文件名;
% list=dir('车牌数据1');
% filename1=list(i,1).name;
% filename=['E:\工作\车牌数据1\',filename1];
C=imread(filename);
Inimage = imread(filename);
if(isrgb(Inimage))
Inimage = rgb2gray(Inimage);
end
%J = adapthisteq(Inimage);%进行对比度增强处理;
%J=double(Inimage);
J=double(Inimage);
%求梯度信息;
[FX FY] = gradient(J);
FX=abs(FX);
FY=abs(FY);
Inopen=FX;
A=Inopen;
B=imread(filename);
[m,n]= size(A);
Hproj=zeros(m,1);
Vproj=zeros(1,n);
for h=1:m
Hproj(h) = sum(A(h,:));
end;
% hf=figure('NumberTitle','off','name','Horizontal Pjoection of A Gray Image');
% plot(Hproj);
% xlabel('像素点标号(从竖直方向看)');
% ylabel('每行像素的累积和');
for v=1:n
Vproj(v) = sum(A(:,v));
end;
% vf=figure('NumberTitle','off','name','Vertical Pjoection of A Gray Image');
% plot(Vproj);
% xlabel('像素点标号(从水平方向看)');
% ylabel('每列像素的累积和');
%%%%求积分
Hp=zeros(1,m);
for i=1:m
if(i==1)
Hp(i)=Hproj(i);
else
Hp(i)=Hp(i-1)+Hproj(i);
end
end
% pf=figure('NumberTitle','off','name','camulate of the Gray Image in Horizontal');
% plot(Hp);
% xlabel('像素点标号(从竖直方向看)');ylabel('水平方向上的像素累积和的积分');
%%%定位Y坐标
plus=zeros(1,m-30+1);indey=1;
for i=0:m-30;
filter=[zeros(1,i),ones(1,30),zeros(1,m-i-30)];
plus(i+1)=filter*Hproj;
if(i>=1)
if(plus(i+1)>plus(i))
indey=i+1;
end
end
end;
cordinatey=[indey,indey+40];
%%%%%%%定位x坐标
plus=zeros(1,n-150+1);index=1;
for i=0:n-150;
filter=[zeros(i,1);ones(150,1);zeros(n-i-150,1)];
plus(i+1)=Vproj*filter;
if(i>=1)
if(plus(i+1)>plus(i))
index=i+1;
end
end
end;
cordinatex=[index,index+130];
%%%%%%%%%%%
img1 = imcrop(B,[cordinatex(1)-5,cordinatey(1),145 40]);
imshow(img1)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% append the crop image and the orignal images .
img2=img1;
rows1 = size(img2,1);
rows2 = size(C,1);
if (rows1 < rows2)
img2(rows2,1) = 255;
else
C(rows1,1) =255;
end
% Now append both images side-by-side.
im=[C img2];
figure;
imshow(im);title('crop image和orignal image 的拼接');
%%%%
flag=0;
Ihimage=rgb2hsv(img1);
[couth,h]=imhist(Ihimage(:,:,1));
cnth=0;
num=sum(couth);
for k=1:50
cnth=cnth+couth(k);
end;
cnth=cnth/num;
if(cnth<=0.25)
display('this is a blue plate!');
msgbox('车牌为蓝色','车牌颜色识别');
elseif(cnth>0.25&&cnth<=0.3)
% display('this is a yellow plate!');
msgbox('无法识别','车牌颜色识别');
else
display('this is a yellow plate!');
msgbox('车牌为黄色','车牌颜色识别');
end;