纸牌识别的matlab程序

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

clc;
clear;

s1=[0.0000,0.1446,0.9626,0.2045,0.0000,0.0000,0.4728,0.7300,0.5505,0.0000,0.0000,0.8117,0.1141,0.8231,0.0020,0.1517,0.8692,0.5498,0.8380,0.1794,0.7140,0.7585,0.0444,0.6550,0.6635];
s2=[0.4563,0.7148,0.5958,0.6707,0.3893,0.4329,0.0766,0.0000,0.1787,0.8545,0.0000,0.0824,0.5567,0.6215,0.1796,0.2874,0.7629,0.1916,0.0104,0.1637,0.9314,0.6341,0.5133,0.5192,0.7787];
s3=[0.8048,0.5795,0.5766,0.9455,0.4252,0.1924,0.2611,0.8293,0.5822,0.0099,0.0000,0.2133,0.1141,0.3623,0.6481,0.2623,0.0115,0.0000,0.0173,0.7745,0.4037,0.6727,0.5775,0.6275,0.2753];
s4=[0.0000,0.0000,0.2512,0.9694,0.0925,0.0000,0.2679,0.7604,0.8132,0.0793,0.2578,0.7657,0.0673,0.6848,0.0815,0.7123,0.6293,0.5001,0.8908,0.4875,0.0000,0.0000,0.1970,0.8778,0.3822];
s5=[0.7039,0.6596,0.5034,0.4646,0.1023,0.8060,0.6680,0.5384,0.4561,0.0633,0.2086,0.0107,0.0000,0.2077,0.7516,0.1847,0.0065,0.0000,0.0345,0.8317,0.4281,0.6702,0.5493,0.6205,0.2588];
s6=[0.2206,0.6390,0.4877,0.4859,0.4061,0.8526,0.4073,0.4508,0.2684,0.0067,0.9437,0.4014,0.0677,0.3790,0.5781,0.9054,0.0782,0.0000,0.0258,0.8557,0.2642,0.6373,0.5243,0.6021,0.2515];
s7=[0.9829,0.5057,0.5034,0.7162,0.6391,0.0343,0.0000,0.1325,0.7773,0.1340,0.0000,0.0190,0.7875,0.2710,0.0000,0.0000,0.3674,0.6683,0.0000,0.0000,0.0000,0.7005,0.2741,0.0000,0.0000];
s8=[0.3474,0.5626,0.4298,0.5290,0.3658,0.6687,0.3057,0.0211,0.1745,0.7241,0.2298,0.8588,0.7106,0.8442,0.2611,0.9135,0.0538,0.0000,0.0145,0.8050,0.4115,0.5710,0.4861,0.5291,0.4489];
s9=[0.2310,0.6701,0.5639,0.6509,0.1929,0.8901,0.1526,0.0000,0.1146,0.8076,0.6415,0.5049,0.1595,0.4821,0.8817,0.0726,0.2839,0.4043,0.4021,0.7325,0.4269,0.6033,0.5382,0.6600,0.1638];
s10=[0.8391,0.0807,0.7685,0.5479,0.6610,0.8722,0.2315,0.7579,0.0000,0.8927,0.9299,0.2438,0.7971,0.0000,0.9650,0.8658,0.2524,0.7517,0.0000,0.8948,0.8447,0.0890,0.7746,0.5354,0.6651];
s11=[0.0000,0.0976,0.5995,1.0050,0.5204,0.0000,0.0000,0.0000,0.9274,0.0933,0.0000,0.0000,0.0000,0.9744,0.0886,0.4431,0.0224,0.0040,0.9578,0.0404,0.5647,0.7080,0.6217,0.6633,0.0000];
s12=[0.0937,0.6697,0.5485,0.6488,0.2629,0.4493,0.5576,0.0000,0.1536,0.7293,0.8065,0.7322,0.1586,0.1410,0.7641,0.4590,0.6630,0.5528,0.6927,0.7192,0.1038,0.6898,0.5364,0.7713,0.6583];
s13=[0.7259,0.8182,0.1451,0.7965,0.6160,0.3906,0.6440,0.4310,0.5432,0.0061,0.3722,0.9290,0.8952,0.1752,0.0000,0.3906,0.7070,0.1027,0.7466,0.0402,0.6934,0.8025,0.1532,0.6957,0.6501];
s14=[0.0000,0.0000,0.0000,0.3861,0.2775,0.0000,0.0000,0.0000,0.4585,0.3861,0.0000,0.0000,0.0000,0.4223,0.3378,0.0000,0.0241,0.0000,0.5068,0.3378,0.0724,0.6153,0.5912,0.5912,0.0603];
s=[s1;s2;s3;s4;s5;s6;s7;s8;s9;s10;s11;s12;s13;s14;]; %样本数组

% ss1=[0.0029,0.0155,0.0136,0.0053,0.0034,0.0141,0.0117,0.0024,0.0131,0.0175,0.0146,0.0136,0.0136,0.0175,0.0146,0.0112,0.0126,0.0175,0.0146,0.0146,0.0146,0.0175,0.0146,0.0146,0.0078,0.0204,0.0170,0.0170,0.0170,0.0204,0.0170,0.0083,0.0000,0.0136,0.0146,0.0146,0.0146,0.0175,0.0126,0.0015,0.0000,0.0024,0.0136,0.0146,0.0146,0.0170,0.0024,0.0000,0.0000,0.0000,0.00

49,0.0146,0.0146,0.0083,0.0000,0.0000,0.0000,0.0000,0.0000,0.0063,0.0102,0.0005,0.0000,0.0000];
% ss2=[0.0000,0.0000,0.0000,0.0064,0.0037,0.0000,0.0000,0.0000,0.0000,0.0000,0.0026,0.0175,0.0143,0.0037,0.0000,0.0000,0.0000,0.0026,0.0143,0.0159,0.0127,0.0148,0.0026,0.0000,0.0101,0.0148,0.0185,0.0185,0.0148,0.0185,0.0148,0.0101,0.0095,0.0127,0.0159,0.0159,0.0127,0.0159,0.0127,0.0106,0.0000,0.0032,0.0143,0.0159,0.0127,0.0154,0.0037,0.0000,0.0000,0.0000,0.0037,0.0180,0.0148,0.0064,0.0000,0.0000,0.0000,0.0000,0.0000,0.0074,0.0064,0.0000,0.0000,0.0000];
% ss3=[0.0000,0.0000,0.0043,0.0161,0.0138,0.0076,0.0000,0.0000,0.0000,0.0000,0.0114,0.0171,0.0142,0.0142,0.0000,0.0000,0.0000,0.0019,0.0095,0.0171,0.0142,0.0128,0.0019,0.0000,0.0085,0.0199,0.0166,0.0199,0.0166,0.0166,0.0195,0.0109,0.0133,0.0171,0.0142,0.0171,0.0142,0.0142,0.0171,0.0142,0.0076,0.0171,0.0142,0.0133,0.0104,0.0142,0.0171,0.0104,0.0000,0.0028,0.0028,0.0066,0.0076,0.0014,0.0047,0.0000,0.0000,0.0000,0.0000,0.0109,0.0119,0.0005,0.0000,0.0000];
% ss4=[0.0000,0.0000,0.0000,0.0075,0.0087,0.0000,0.0000,0.0000,0.0000,0.0000,0.0087,0.0139,0.0174,0.0058,0.0000,0.0000,0.0000,0.0069,0.0174,0.0139,0.0174,0.0139,0.0093,0.0000,0.0075,0.0139,0.0174,0.0139,0.0174,0.0139,0.0174,0.0052,0.0168,0.0139,0.0174,0.0139,0.0174,0.0139,0.0174,0.0122,0.0162,0.0139,0.0174,0.0139,0.0174,0.0139,0.0174,0.0139,0.0052,0.0104,0.0116,0.0081,0.0116,0.0081,0.0110,0.0035,0.0000,0.0000,0.0017,0.0133,0.0168,0.0012,0.0000,0.0000];

ss1=[0.6799,0.9475,0.5020,0.8984,0.6199,0.9467,0.9698,1.0394,0.9698,0.9404,0.5778,0.9891,1.0638,0.9891,0.5700,0.0259,0.7638,1.0394,0.7741,0.0363,0.0000,0.1032,0.8505,0.1213,0.0000];
ss2=[0.0000,0.0456,0.6978,0.0709,0.0000,0.0613,0.7559,0.9662,0.7821,0.0581,0.7436,1.0353,0.9564,1.0353,0.7484,0.0486,0.7427,0.9662,0.7925,0.0634,0.0000,0.0442,0.7221,0.0850,0.0000];
ss3=[0.0000,0.4476,0.9548,0.4543,0.0000,0.0845,0.6965,0.9859,0.7256,0.0635,0.8732,0.9450,0.9454,0.9450,0.8603,0.7361,0.8935,0.7520,0.8669,0.7597,0.0000,0.0304,0.8086,0.0646,0.0012];
ss4=[0.0000,0.1265,0.8252,0.1178,0.0000,0.1424,0.8923,1.0473,0.8808,0.1338,0.8293,1.0056,1.0488,1.0056,0.7885,0.8835,0.9873,0.9209,0.9798,0.8843,0.0500,0.1833,0.8990,0.1732,0.0501];
s2=[ss1;ss2;ss3;ss4;]; %样本数组

load m.out;
in = m;
k=1;

%height=90;width=100; %读入图像的长和宽
height=240;width=320; %读入图像的长和宽
num=0; %识别所得数字初始化
col=0; %识别所得花色初始化
findnumber=0;%找到数字
findcolor=0;%找到花色
ten=0; %识别10标志
king=0; %识别王标志
total=0;
tred=0;
tgreen=0;
tblue=0;
ind=0;
ind2=0;
R=zeros(height,width);
G=zeros(height,width);
B=zeros(height,width);
for i=1:height
for j=1:width
% for i=1:1
% for j=1:1
f1=bitget(in((i-1)*width+j+(k-1)*height*width),16:-1:1);
tt='';
for jj=1:5
if f1(jj)==0
tt=strcat(tt,'0');
else if f1(jj)==1
tt=strcat(tt,'1');
end
end
end

R(i,j)=bin2dec(tt)/32;
tt='';
for jj=6:11
if f1(jj)==0
tt=strcat(tt,'0');
else if f1(jj)==1
tt=strcat(tt,'1');
end
end
end
G(i,j)=bin2dec(tt)/64;
tt='';
for jj=12:16
if f1(jj)==0
tt=strcat(tt,'0');
else if f1(jj)==1
tt=strcat(tt,'1');
end
end
end
B(i,j)=bin2dec(tt)/32;
end
end
A=cat(3,R,G,B);
imshow(A);
f=rgb2gray(A);
% figure,imshow(B);

% f=zeros(height,width);
% for i=1:height
% for j=1:width
% f(i,j)=in((i-1)*width+j+(k-1)*height*width);
% end
% end

%% autothreshold自适应阈值
% thres = 0.5*(double(min(f(:)))+double(max(f(:))));
% done = false;
% while ~done
% g = f>=thres;
% Tnext = 0.5*(mean(f(g))+mean(f(~g)));
% done = abs(thres-Tnext)<0.5;
% thres = Tnext;
% end;
% Ibw = im2bw(f, thres);
% % figure, imshow(Ibw)

%% G自适应阈值
thres = 0.5*(double(min(G(:)))+double(max(G(:))));
done = false;
while ~done
gg = G>=thres;
Tnext = 0.5*(mean(G(gg))+mean(G(~gg)));
done = abs(thres-Tnext)<0.5;
thres = Tnext;
end;
Ibw = im2bw(B, thres);
% figure, imshow(Ibw)

% Ibw = f;

%% Initialization
lef=0; rig=0; wid=0; %lef为左侧线,rig为右侧线,wid为字母宽度
upp=0; bot=0; hig=0; %upp为上线,bot为下线,hig为字母高度
%%%%%%
lef2=0; rig2=0; wid2=0; %lef2为花色左侧线,rig2为花色右侧线,wid2为花色宽度
upp2=0; bot2=0; hig2=0; %upp2为花色上线,bot2为花色下线,hig2为花色高度
%%%%%%
maxwid=50; %最大字母宽度
minwid=30; %最小字母宽度
maxhig=75; %最大字母高度
minhig=65; %最小字母高度
%%%%%%
maxwid2=50; %最大花色宽度
minwid2=30; %最小花色宽度
maxhig2=52; %最大花色高度
minhig2=42; %最小花色高度
%%%%%%
kingmaxwid=30; %king中J最大字母宽度
kingminwid=20; %king中J最小字母宽度
kingmaxhig=40; %king中J最大字母高度
kingminhig=30; %king中J最小字母高度
li=zeros(1,height); %每行像素值
co=zeros(1,width); %每列像素值
li2=zeros(1,height); %每行像素值
co2=zeros(1,width); %每列像素值

%% Find number 1-K
for j=1:width
for i=1:height
if Ibw(i,j)==0
co(j)=co(j)+1; %求每列像素值
end
end
end

for j=width:-1:1
if co(j)>2 && co(j)<(height-5)
if j==5 && widrig=0; wid=0;
elseif j==1 && widrig=0; wid=0;
elseif wid==0
rig=j; wid=1;
elseif wid>0 && wid<=maxwid
wid=wid+1;
elseif wid>maxwid
break;
end
else
if widrig=0; wid=0;
elseif wid>=minwid
break;
end
end
end
if wid>0
for i=1:height
for j=(rig-wid+1):rig
if Ibw(i,j)==0
li(i

)=li(i)+1; %求每列像素值
end
end
end

for i=1:height
if li(i)>0
if hig==0
upp=i; hig=1;
elseif i==height && higupp=0; hig=0;
elseif hig>0 && hig<=maxhig
hig=hig+1;
elseif hig>maxhig
break;
end
else
if higupp=0; hig=0;
elseif hig>=minhig
break;
end
end
end
end

if rig>0 && wid>0 && upp>0 && hig>0
findnumber=1;
lef=rig-wid+1;
bot=upp+hig-1;
else
findnumber=0;
wid=0; hig=0; rig=0; upp=0;
end

%% Find Color
if findnumber==1
for j=1:width
for i=(bot+1):height
if Ibw(i,j)==0
co2(j)=co2(j)+1; %求每列像素值
end
end
end

for j=(lef-2):(rig+2)
if co2(j)>2 && co2(j)<(height-bot)
if wid2==0
lef2=j; wid2=1;
elseif j==(rig+2) && wid2lef2=0; wid2=0;
elseif wid2>0 && wid2<=maxwid2
wid2=wid2+1;
elseif wid2>maxwid2
break;
end
else
if wid2lef2=0; wid2=0;
elseif wid2>=minwid2
break;
end
end
end
if wid2>0
for i=(bot+1):height
for j=lef2:(lef2+wid2-1)
if Ibw(i,j)==0
li2(i)=li2(i)+1; %求每列像素值
end
end
end

for i=(bot+1):height
if li2(i)>0
if hig2==0
upp2=i; hig2=1;
elseif i==height && hig2upp2=0; hig2=0;
elseif hig2>0 && hig2<=maxhig2
hig2=hig2+1;
elseif hig2>maxhig2
break;
end
else
if hig2upp2=0; hig2=0;
elseif hig2>=minhig2
break;
end
end
end
end

if lef2>0 && wid2>0 && upp2>0 && hig2>0
findcolor=1;
rig2=lef2+wid2-1;
bot2=upp2+hig2-1;
else
findcolor=0;
wid2=0; hig2=0; lef2=0; upp2=0;
end
end

%% Find King
if findnumber==0
for j=1:width
% for j=width:-1:1
if co(j)>0 && co(j)<(height-5)
if wid==0
lef=j; wid=1;
elseif j==width && widlef=0; wid=0;
elseif wid>0 && wid<=kingmaxwid
wid=wid+1;
elseif wid>kingmaxwid
break;
end
else
if widlef=0; wid=0;
elseif wid>=kingminwid

break;
end
end
end
if lef>0
rig=lef+wid-1;
% lef=rig-wid+1;
for i=1:height
for j=lef:rig
if Ibw(i,j)==0
li(i)=li(i)+1;
end
end
end
for i=1:height
if li(i)>0
if hig==0
upp=i; hig=1;
elseif i==height && higupp=0; hig=0;
elseif hig>0 && hig<=kingmaxhig
hig=hig+1;
elseif hig>kingmaxhig
king=1;break;
end
else
if higupp=0; hig=0;
elseif hig>=kingminhig
king=1;break;
end
end
end
end
if king==1
findnumber=1;
bot=upp+hig-1;
else
findnumber=0;
wid=0; hig=0; lef=0; rig=0; upp=0; bot=0;
end
end

%% 找到与否
if findnumber==0
disp('Can not find any numbers!');
end

if findnumber == 1
Ibw2=Ibw;
for i=upp:bot
Ibw2(i,lef)=0;
Ibw2(i,rig)=0;
end
for j=lef:rig
Ibw2(upp,j)=0;
Ibw2(bot,j)=0;
end
src=zeros(hig,wid);
for i=1:hig
for j=1:wid
src(i,j)=Ibw(upp+i-1,lef+j-1);
end
end
if upp2 > 0
for i=upp2:bot2
Ibw2(i,lef2)=0;
Ibw2(i,rig2)=0;
end
for j=lef2:rig2
Ibw2(upp2,j)=0;
Ibw2(bot2,j)=0;
end
src2=zeros(hig2,wid2);
for i=1:hig2
for j=1:wid2
src2(i,j)=Ibw(upp2+i-1,lef2+j-1);
end
end
end
% figure, imshow(Ibw2);
figure, imshow(src);
end

%% red or black
% if findnumber == 1
% for j=lef:rig
% for i=1:height
% if Ibw(i,j)==0
% total=total+1;
% tred=tred+R(i,j);
% end
% end
% end
% tred=tred/total;
% end

%% Create a 5*5 square matrix
if findnumber == 1 && num==0
square=zeros(1,25);
swid=wid/5;
shig=hig/5;
for i=1:5
for j=1:5
supp=round((i-1)*shig+1);
sbot=round(i*shig);
slef=round((j-1)*swid+1);
srig=round(j*swid);
if i==5
sbot=hig;
end
if j==5
srig=wid;
end
for ii=supp:sbot
for jj=slef:srig
if src(ii,jj)==0
square((i-1)*5+j)=square((i-1)*5+j)+1;
end
end
end
square((i-1)*5+j)=square((i-1)*5+j)/(swid*shig);
end
end
end

%% Create a 8*8 square matrix for color
%%%%%%
if findnumber == 1 && col==0
square2=zeros(1,25);
swid2=wid2

/5;
shig2=hig2/5;
for i=1:5
for j=1:5
supp2=round((i-1)*shig2+1);
sbot2=round(i*shig2);
slef2=round((j-1)*swid2+1);
srig2=round(j*swid2);
if i==5
sbot2=hig2;
end
if j==5
srig2=wid2;
end
for ii=supp2:sbot2
for jj=slef2:srig2
if src2(ii,jj)==0
square2((i-1)*5+j)=square2((i-1)*5+j)+1;
end
end
end
square2((i-1)*5+j)=square2((i-1)*5+j)/(swid2*shig2);
% divide
for ii=supp2:sbot2
src2(ii,slef2)=0;
end
if j==5
for ii=supp2:sbot2
src2(ii,srig2)=0;
end
end
for jj=slef2:srig2
src2(supp2,jj)=0;
end
if i==5
for jj=slef2:srig2
src2(sbot2,jj)=0;
end
end
end
end
end
figure, imshow(src2);
%%%%%%

%% matching
ff=0;
if findnumber == 1 && num==0
sum=zeros(1,14);
for card=1:14
for ss=1:25
sum(card)=sum(card)+(square(ss)-s(card,ss))^2;
end
end
if num==0
[ind, num] = min(sum(1,:));
if ind<0.2
ff=num;
end
if ff==11
if king==1
ff=14;
end
end
end
end

%%%%%%
cc=0;
if findcolor == 1 && col==0
sum2=zeros(1,4);
for color=1:4
for ss=1:25
sum2(color)=sum2(color)+(square2(ss)-s2(color,ss))^2;
end
end
if col==0
[ind2, col] = min(sum2(1,:));
if ind2<0.2
cc=col;
end
if ff==14
if tred >0.3
cc=1;
end
if tred <= 0.3
cc=2;
end
end
end
end
%%%%%%
ff
cc

相关文档
最新文档