封装的函数getSplitPic
function [splitfs,points]=getSplitPic(bw)
%函数说明
%input:bw为需要分割的二值图
%output: splitfs 为元数据 分割好的图片都保存在splitfs中
% points 为分割好的坐标 a,b,c,d a:b,c:d
%right by Seven 2017.12.3
splitfs={};
points=[];
hs=sum(bw);
[~,w]=size(bw);
c=1;d=1;i=1;
while(c<w)
while(hs(c)==0&&c<w)
c=c+1;
end
d=c;
while(hs(d)>0)
d=d+1;
end
if((d-c)>1)
x=bw(:,c:d-1);
[r,~]=find(x);
a=min(r);b=max(r);
xi=x(a:b,:);
splitfs{i}=xi;
points=[points;a,b,c,d-1];
i=i+1;
end
c=d;
end
程序代码
clear,clc,close all;
f=imread('car1.jpg');
imshow(f);
g=rgb2gray(f);
figure,imshow(g);
%方块算子
se=strel('disk',5);
%顶帽操作去除背景
gth=imtophat(g,se);
figure,imshow(gth);
%边缘提取
e=edge(gth);
figure,imshow(e);
%线性算子
se=strel('line',6,50);
%腐蚀操作
ee=imerode(e,se);
figure,imshow(ee);
se=strel('disk',20);
%膨胀操作
ed=imdilate(ee,se);
figure,imshow(ed);
%去除小对象
eao=bwareaopen(ed,3500);
figure,imshow(eao);
[hs,ws]=find(eao);
a=min(hs);b=max(hs);
c=min(ws);d=max(ws);
%提取车牌没有到左边界,所以向左边界扩展
carNumArea=gth(a:b,c-100:d);
figure,imshow(carNumArea);
bwcarNumArea=im2bw(carNumArea,0.3);
figure,imshow(bwcarNumArea);
bao=bwareaopen(bwcarNumArea,40);
figure,imshow(bao);
%去除边框(边框必须贴边,如果不贴边,可以用find找到边界进行截取)
%bcb=imclearborder(bao);
%figure,imshow(bcb);
% [hs1,ws1]=find(bao);
% a1=min(hs1);b1=max(hs1);
% c1=min(ws1);d1=max(ws1);
% bc=bao(a1:b1,c1:d1);
% bcb=imclearborder(bc);
% figure,imshow(bc);
[ss,~]=getSplitPic(bao);
for i=1:size(ss,2)
s=ss{i};
figure,imshow(s);
end
部分运行结果图片