ph=imread('1y5j1j.jpg')
image=ph;
image1=rgb2hsv(image);
I=image1(:,:,2);
subplot(3,3,1);imshow(image1);
bw=im2bw(I,0.4);
subplot(3,3,2);imshow(I)
subplot(3,3,3);imshow(bw)
I1=imclose(bw,strel('disk',10));
I2=imopen(I1,strel('disk',10));
I3=bwareaopen(I2, 10000);
subplot(3,3,4);imshow(I3);
I4=imerode(I3,ones(1,1));
subplot(3,3,5);imshow(I4);
[L,numa]=bwlabel(I4,8);
image=ph;
I=rgb2gray(image);
figure;subplot(2,4,1);imshow(I);
bw=im2bw(I,graythresh(I));
I1=bwareaopen(bw, 10000);
I2=imclose(I1,strel('disk',5));
I3=imopen(I2,strel('disk',5));
subplot(2,4,2);imshow(I3)
I4=medfilt2(I3,[5,5]);
I5=bwareaopen(I4, 10000);
subplot(2,4,3),imshow(I5);
I6=edge(I5,'canny');
I7=imfill(I6 ,'holes');
I9=bwareaopen(~I7, 10000);
I10=bwareaopen(~I9, 10000);
subplot(2,4,4);imshow(I10)
[L,T]=bwlabel(I10,8);
if T==1
if numa==1;
numa=1;c=0;i=0;
disp(['一角的数量为']);
disp(c);
disp(['一元的数量为']);
disp(i);
disp(['五角的数量为']);
disp(numa);
else
load templet; %加载数字特征
%A = imp; %得到待识别图片
Image=ph;
I1= rgb2gray(Image);
I9=im2bw(I1,graythresh(I1));
bw=imfill(~I9,'holes');
pictureCut = bwareaopen(bw, 1000);
pictureRe = regionprops(pictureCut, 'area', 'boundingbox');
rects = cat(1, pictureRe.BoundingBox);
%将面积对象的边界条件链接并保存到rects,顺序为[起始点x坐标, 起始点y坐标, 面积对象长度(x), 面积
rectangle('position', rects(1, :), 'EdgeColor', 'r'); %定位车牌区域,并用红色的框标记
pictureOut = imcrop(I1, rects(1, :)); %按照红线框切割车牌区域
p1=im2bw(pictureOut,graythresh(pictureOut)+0.05);
SE = strel('disk',1) ;
a=imerode(p1,SE);
b=imdilate(p1,SE);
p3=b-a;
p=~p3;
I4=imresize(p,[500 500]);
figure(1),subplot(121),imshow(Image),title('待识别的图片:');
B=zeros(1,100); %创建1列100行的特征向量
[row,col] = size(I4); %得到待测样本的行列
cellRow = row/10; %将其降维10x10的特征图片
cellCol = col/10;
count = 0; %每1/100个格子中为0的像素点个数
currentCell = 1; %当前计算为第1个1/100格子部分
for currentRow = 0:9
for currentCol = 0:9
for i = 1:cellRow %计算每1/100部分中为0的数量
for j = 1:cellCol
if(I4(currentRow*cellRow+i,currentCol*cellCol+j)==0)
count=count+1;
end
end
end
ratio = count/(cellRow*cellCol); %计算1/100部分中黑色像素的占比
B(1,currentCell) = ratio; %将每个占比统计在B特征向量中
currentCell = currentCell+1; %新的1/100部分开始计算
count = 0; %像素点计数置0
end
end
class = bayesBinary(B); %将该特征利用贝叶斯分类器分类,返回类的个数
subplot(122),imshow(['C:\Users\feng\Desktop\硬币\yangben\',num2str(class),'.jpg']),title('该数字被识别为:');
disp(['该数字被识别为:',pattern(class).name]);
if class==1||class==4
c=0;i=1;numa=0;
end
if class==2||class==5
c=0;i=0;numa=1;
end
if class==3||class==6
c=1;i=0;numa=0;
end
disp(['一角的数量为']);
disp(c);
disp(['一元的数量为']);
disp(i);
disp(['五角的数量为']);
disp(numa);
end
else
I11=edge(I10,'sobel');
subplot(2,4,5);imshow(I11)
stepr = 1;
stepa = 0.1;
minr =3;
maxr =300;
percent= 0.8;
circlefind=findcircle(I11,minr,maxr,stepr,stepa,percent);
[w,h]=size(circlefind);
for g=1:w-1
for q=g+1:w
if abs(circlefind(g,1)-circlefind(q,1))<5&&abs(circlefind(g,2)-circlefind(q,2))<5
circlefind(q,1)=0;
circlefind(q,2)=0;
circlefind(q,3)=0;
end
end
end
circlefind(all(circlefind==0,2),:)=[];
[j,k]=size(circlefind);
i=0;
[l,num]=bwlabel(I10,8);
[my_max] = max(circlefind(:,3));
[my_min] = min(circlefind(:,3));
c=0;
c1=c;
i1=i;
numa1=numa;
if my_max-my_min>15
for n=1:j
if my_max-circlefind(n,3)<10&&circlefind(n,3)>30
i1=i1+1;i=i+1;
end
end
c1=T-numa1-i1;
c=T-numa-i;
disp(['一角的数量为']);
disp(c1);
disp(['一元的数量为']);
disp(i1);
disp(['五角的数量为']);
disp(numa1);
else
for n=1:j
if my_max-circlefind(n,3)<10&&circlefind(n,3)>30
i=i+1;i1=i1+1;
end
end
c1=T-numa1;
c=T-numa-i;
disp(['一角的数量为']);
disp(c1);
disp(['一元的数量为']);
disp(0);
disp(['五角的数量为']);
disp(numa1);
end
if c==0&&numa==0 %%c=0说明非五角大小相同
%利用贝叶斯分类器对手写图片识别
load templet; %加载数字特征
%A = imp; %得到待识别图片
Image=ph;
I1= rgb2gray(Image);
I9=im2bw(I1,graythresh(I1));
bw=imfill(~I9,'holes');
pictureCut = bwareaopen(bw, 1000);
pictureRe = regionprops(pictureCut, 'area', 'boundingbox');
% areas = [pictureRe.Area]; %将面积对象保存到areas里
rects = cat(1, pictureRe.BoundingBox);
%将面积对象的边界条件链接并保存到rects,顺序为[起始点x坐标, 起始点y坐标, 面积对象长度(x), 面积
rectangle('position', rects(1, :), 'EdgeColor', 'r'); %定位车牌区域,并用红色的框标记
pictureOut = imcrop(I1, rects(1, :)); %按照红线框切割车牌区域
p1=im2bw(pictureOut,graythresh(pictureOut)+0.05);
SE = strel('disk',1) ;
a=imerode(p1,SE);
b=imdilate(p1,SE);
p3=b-a;
p=~p3;
I4=imresize(p,[500 500]);
figure(1),subplot(121),imshow(Image),title('待识别的图片:');
B=zeros(1,100); %创建1列100行的特征向量
[row,col] = size(I4); %得到待测样本的行列
cellRow = row/10; %将其降维10x10的特征图片
cellCol = col/10;
count = 0; %每1/100个格子中为0的像素点个数
currentCell = 1; %当前计算为第1个1/100格子部分
for currentRow = 0:9
for currentCol = 0:9
for i = 1:cellRow %计算每1/100部分中为0的数量
for j = 1:cellCol
if(I4(currentRow*cellRow+i,currentCol*cellCol+j)==0)
count=count+1;
end
end
end
ratio = count/(cellRow*cellCol); %计算1/100部分中黑色像素的占比
B(1,currentCell) = ratio; %将每个占比统计在B特征向量中
currentCell = currentCell+1; %新的1/100部分开始计算
count = 0; %像素点计数置0
end
end
class = bayesBinary(B); %将该特征利用贝叶斯分类器分类,返回类的个数
subplot(122),imshow(['C:\Users\feng\Desktop\硬币\yangben\',num2str(class),'.jpg']),title('该数字被识别为:');
%disp(['该数字被识别为:',pattern(class).name]);
if class==1||class==4
c=0;i=T;numa=0;
end
if class==2||class==5
c=0;i=0;numa=T;
end
if class==3||class==6
c=T;i=0;numa=0;
end
disp(['一角的数量为']);
disp(c);
disp(['一元的数量为']);
disp(i);
disp(['五角的数量为']);
disp(numa);
end
end