【纸牌识别】基于matlab形态学扑克牌识别【含Matlab源码 1352期】

举报
海神之光 发表于 2022/05/29 00:12:11 2022/05/29
【摘要】 一、获取代码方式 获取代码方式1: 完整代码已上传我的资源:【纸牌识别】基于matlab形态学扑克牌识别【含Matlab源码 1352期】 获取代码方式2: 通过订阅紫极神光博客付费专栏,凭支付凭证,...

一、获取代码方式

获取代码方式1:
完整代码已上传我的资源:【纸牌识别】基于matlab形态学扑克牌识别【含Matlab源码 1352期】

获取代码方式2:
通过订阅紫极神光博客付费专栏,凭支付凭证,私信博主,可获得此代码。

备注:
订阅紫极神光博客付费专栏,可免费获得1份代码(有效期为订阅日起,三天内有效);

二、形态学检测简介

1 图像分析及预处理
拍摄图像会产生随机的扰动,图像有一定的噪声,为消除掉图像中的无关信息,对图像进行预处理。

1.1 灰度化
为降低运算量,需要将拍摄的三通道的RGB图像转化为单通道的灰度图像。采用加权平均法的灰度化方法,其中心理学灰度公式根据人眼对RGB三色的敏感程度选择不同的权重:
在这里插入图片描述
式(1)中,R、G、B分别为RGB三通道灰度值,灰度化结果如图1 (a)所示。

1.2 平滑处理
为了尽可能避免将背景当作缺陷,需要对图像进行平滑处理,这样虽然会使缺陷的边界模糊,但是有利于减少背景的干扰。注意所采用的去噪处理为均值滤波,均值滤波公式为:
在这里插入图片描述
式(2)中,m、n分别为所选择的滤波核的长和宽,Sxy是以(x,y)为中心的滤波核对应像素的位置集合,平滑处理结果如图1 (b)所示。均值滤波的缺点是会使一些细节如边缘等信息丢失,因此在找到种子点后,对没有经过平滑处理的图像进行区域生长,找到缺陷边界。

2 算法原理
2.1 阈值分割

阈值分割是图像分割中最简单、基础的方法,性能比较稳定,计算量较小,运算速度快;它主要有全局阈值分割、局部阈值分割、自适应阈值分割等方法。阈值算法基于阈值T,将像素灰度值大于阈值T和小于阈值T的部分分别叫做前景和背景。变换函数表达式:
在这里插入图片描述
图1 均值滤波处理
在这里插入图片描述
式(3)中,T为阈值,g (x,y)为原图像像素点(x,y)的灰度值,f (x,y)为分割后图像像素点(x,y)的灰度值,阈值分割结果如图2所示。
在这里插入图片描述
图2 阈值分割结果

2.2 形态学开运算降噪
数学形态学简称形态学,其处理方式为领域运算,即把领域结构元素与图像对应位置像素进行逻辑运算,这种运算的影响因素主要有结构元素大小、形状和逻辑运算的规则。形态学操作主要有膨胀、腐蚀、梯度运算、礼帽运算、黑帽运算、开运算和闭运算等,但其基础为腐蚀和膨胀,利用膨胀和腐蚀就能完成不同形式的运算。

腐蚀运算能消除轮廓边界点,使边界向内缩小,主要用于细化二值图像目标轮廓、去除噪声等。
在这里插入图片描述
式(4)中,A为原图像,B为结构元素。首先给结构元素B定义一个原点,当结构元素B的原点移动到图像A的(x,y)上时,如果结构元素B上等于1的像素点对应图像A也等于1,则将图像A的(x,y)的灰度值置为1,否则置为0,腐蚀示意图如图3所示。
在这里插入图片描述
图3 腐蚀示意图
膨胀运算则与腐蚀运算相反,使边界向外扩张,主要用于填补图像分割后的空白,使相近的不相连的轮廓相连。其公式为:
在这里插入图片描述
式(5)中,A为原图像,B为结构元素。首先给结构元素B定义一个原点,当结构元素B的原点移动到图像A的(x,y)上时,如果结构元素B上等于1的像素点对应图像A中至少有一个等于1,则将图像A的(x,y)的灰度值置为1,否则置为0。

先进行腐蚀操作,然后在腐蚀的基础上进行膨胀操作,主要用于去噪和计数等。其公式为:
在这里插入图片描述
式(6)中,A为原图像,B、C为结构元素。开运算效果如图4所示,图5为开运算处理的结果。
在这里插入图片描述
图4 开运算效果
2.3 区域生长法
区域生长的思想就是把领域(四领域、八领域等)相同的化为一个区域。首先需要一个种子点作为生长的开始,然后将种子点领域内满足相似准则要求的像素点合并到种子的区域,将这个区域的像素做为种子点,继续进行生长,直到没有符合要求的点,生长结束,所有种子点像素作为生长的区域。分割的好坏由初始种子点和相似准则决定。
在这里插入图片描述
图5 形态学开运算结果
2.3.1 种子点选择与检测
经过阈值分割和形态学处理后,将二值图像各轮廓中心作为待定种子点。如果选择的种子点位于缺陷的绝对区域,那么种子点总有一个方向各像素的深度值呈现高-低-高的形态。设计检测模板如图6所示,计算出种子点在0°、45°、90°、135°方向上的深度变化,判断其变化是否呈高低高形态。
在这里插入图片描述
图6 检测模板
种子点左右两侧r个像素的灰度平均值分别为:
在这里插入图片描述
各方向的灰度变化为:
在这里插入图片描述
深度形Si态变化判定:
在这里插入图片描述
式(10)中,I (u)为检测模板中第u个像素的灰度值,w=1,2,3,4,分别代表0°,45°,90°,135°方向,mwm为w方向两侧的最小灰度值,T1为形态变化阈值。如果种子点不满足深度形态变化判定,则去除该待定种子点。

2.3.2 生长过程
区域生长的具体流程如下:
(1)将种子点坐标放入种子点集seeds。
(2)顶出种子点集中的一个种子点,对种子点八邻域的像素点进行相似准则判断;满足相似准则条件的点,视为种子点放入种子点集seeds。
(3)将顶出的种子点存入种子集S。
(4)如果种子点集内没有元素,则跳到步骤(4);如果种子点集中还有元素,则跳到步骤(2)。
(5)生成一张和输入图像长宽一致,像素值全为0的图像I。
(6)将图像I中对应种子集S坐标的像素值置为255,得到分割图像I’。
其中生长的相似准则为:
在这里插入图片描述
式(11)中,gray (seed)为当轮种子点的灰度值,gray (8_n)为其八邻域各点的像素值,Thresh为设置的阈值。区域生长结果如图7所示。
在这里插入图片描述
图7 区域生长结果
3 实验过程
图像分割就是按照预先设定的规则,将图像分割为有意义的前景和背景的过程。区域生长是一个分割效果比较好的算法,但前提是需要找到适合的种子点。单一的分割算法就容易遇到这种不足的情况,结合使用形态学和阈值分割的方法来找到合适的种子点,帮助区域生长算法完成分割任务,达到满足要求的分割效果,分割方法流程如图8所示。
在这里插入图片描述
图8 分割流程图
首先对输入的图片进行灰度化处理,变成单通道的灰度图片,然后滤波去除噪声,使图像更平滑,选择合适的阈值进行阈值分割,在利用开运算去除掉分割后较小的前景,以各前景区域的中心点为起始种子点,进行区域生长,得到最终所满足要求的前景。

三、部分源代码

clear;clc;
A = imread('../images/R_8.JPG');

% 分辨数字
% 三个参数分别代表:行数,列数,RGB三值(彩色图像固定为3)
% 比例是调出来的 ^_^
[x,y,z] = size(A);
Number_Left_Top = A(1 : x/6, 1 : y/6, 1 : z);
Shape_Left_Top = A(x/6 : x/2, 1 : y/6, 1 : z);
% 截取图像
Number_BW = CutImage(Number_Left_Top);
Shape_BW = CutImage(Shape_Left_Top); 
% 获取投影模板
Number_Model = GetModel(Number_BW);
Shape_Model = GetModel(Shape_BW);

% 解析花色
Shape = ParseShape(Shape_Model);
% 解析数字
Number = ParseNumber(Number_Model);

subplot(2,2,1),imshow(A),title('源图像');
subplot(2,2,2),imshow(Number_BW),title('左上角数字二值化图像');
subplot(2,2,3),imshow(Shape_BW),title('左上角花色二值化图像');
subplot(2,2,4),imshow(A),title(strcat(Shape,num2str(Number)));
function  Number  = ParseNumber( Number_Model )
%PARSENUMBER 此处显示有关此函数的摘要
% 功能: 解析数字

% 数字投影30+20模板
Model_2 = [0.42045,0.60227,0.72727,0.81818,0.625,  0.5,    0.44318,0.43181,0.40909,0.40909,0.38636,0.27272,0.20454,0.19318,0.22727,0.25,   0.27272,0.27272,0.29545,0.31818,0.31818,0.29545,0.27272,0.26136,0.25,  0.23863,0.23863,0.22727,0.21590,0.66148,0.28571,0.42207,0.5,    0.55194,0.55844,0.44805,0.40909,0.38311,0.36363,0.35064,0.34415,0.34415,0.33766,0.35714,0.36363,0.38311,0.40259,0.56493,0.64935,0.50432];
Model_3 = [0.91666,0.9375, 0.91666,0.6875, 0.41666,0.41666,0.40625,0.41666,0.36458,0.22916,0.22916,0.25,   0.40625,0.53125,0.625,  0.58333,0.35416,0.20833,0.20833,0.1875, 0.19791,0.1875, 0.1875, 0.1875, 0.1875,0.1875, 0.19791,0.22916,0.32291,0.50911,0.33108,0.38513,0.42567,0.39864,0.25675,0.29054,0.32432,0.34459,0.34459,0.35811,0.37162,0.39864,0.42567,0.44594,0.45945,0.47297,0.49324,0.5,    0.52702,0.46756];
Model_4 = [0.22093,0.24418,0.27906,0.31395,0.33720,0.36046,0.39534,0.41860,0.45348,0.45348,0.45348,0.45348,0.43023,0.44186,0.45348,0.44186,0.44186,0.44186,0.44186,0.44186,0.44186,0.44186,0.43023,0.75581,1,     1,      1,      0.61627,0.20930,0.32421,0.12666,0.17333,0.21333,0.26,   0.3    ,0.32666,0.31333,0.32666,0.32,   0.32,   0.32666,0.36,   0.43333,0.43333,0.74666,1,      1,      1,      1,      0.208];
Model_5 = [0.84091,0.86363,0.86363,0.85227,0.21590,0.20454,0.20454,0.20454,0.20454,0.20454,0.32954,0.65909,0.77272,0.82954,0.65909,0.47727,0.38636,0.30681,0.21590,0.20454,0.20454,0.19318,0.18181,0.18181,0.1818,0.18181,0.18181,0.20454,0.20454,0.48863,0.35897,0.60256,0.60897,0.57692,0.53205,0.33974,0.30769,0.3141, 0.30128,0.29487,0.29487,0.30128,0.32051,0.33333,0.35256,0.37179,0.41025,0.50641,0.66025,0.43162];
Model_6 = [0.29787,0.51063,0.61702,0.63829,0.46808,0.40425,0.36171,0.26595,0.19148,0.17021,0.15957,0.17021,0.31914,0.57446,0.71276,0.79787,0.70212,0.57446,0.52127,0.45744,0.43617,0.41489,0.40425,0.39361,0.3829,0.37234,0.3617, 0.37234,0.38297,0.46136,0.33766,0.57792,0.70129,0.77922,0.77922,0.51948,0.42857,0.37662,0.33116,0.2987, 0.29221,0.27922,0.27922,0.2987, 0.32467,0.32467,0.36363,0.3961, 0.50649,0.46031];
Model_7 = [0.95238,1,      1,      0.73809,0.44047,0.44047,0.45238,0.45238,0.30952,0.22619,0.22619,0.21428,0.21428,0.22619,0.21428,0.21428,0.20238,0.19047,0.20238,0.21428,0.20238,0.21428,0.21428,0.21428,0.2142,0.21428,0.21428,0.22619,0.21428,0.20308,0.21333,0.22666,0.22666,0.22666,0.16,   0.15333,0.28,   0.39333,0.48,   0.51333,0.46666,0.44666,0.40666,0.38666,0.36666,0.35333,0.36666,0.31333,0.26666,0.17666];
Model_8 = [0.34693,0.5102, 0.62244,0.59183,0.40816,0.35714,0.31632,0.30612,0.30612,0.28571,0.30612,0.31632,0.33673,0.38775,0.42857,0.62244,0.61224,0.53061,0.5102, 0.61224,0.71428,0.5,    0.41836,0.38775,0.3469,0.33673,0.35714,0.32653,0.32653,0.46079,0.16233,0.36363,0.55194,0.70779,0.66233,0.51948,0.46103,0.42857,0.37012,0.36363,0.33116,0.32467,0.32467,0.32467,0.33766,0.38311,0.3961, 0.45454,0.51948,0.49232];
Model_9 = [0.25531,0.45744,0.57446,0.65957,0.53191,0.46808,0.41489,0.41489,0.38297,0.39361,0.38297,0.38297,0.38297,0.38297,0.38297,0.39361,0.39361,0.42553,0.44681,0.48936,0.51063,0.61702,0.75531,0.79787,0.7127,0.53191,0.2553, 0.18085,0.17021,0.38857,0.20779,0.37662,0.51298,0.61688,0.58441,0.43506,0.38961,0.34415,0.31818,0.30519,0.2987, 0.2987, 0.2987 ,0.31168,0.32467,0.35714,0.38961,0.48701,0.64935,0.61616];
Model_10 =[0.31914,0.59574,0.68085,0.74468,0.65957,0.58511,0.56382,0.55319,0.55319,0.55319,0.55319,0.55319,0.55319,0.55319,0.55319,0.55319,0.55319,0.55319,0.55319,0.55319,0.55319,0.55319,0.55319,0.55319,0.5532,0.55319,0.55319,0.55319,0.55319,0.60471,0.97402,0.98701,0.98701,0.98701,0.48051,0,      0,      0,      0.35714,0.81818,0.8961, 0.94155,0.96753,0.25324,0.21428,0.19481,0.19481,0.22077,0.25324,0.83116];

Model = [Model_2;Model_3;Model_4;Model_5;Model_6;Model_7;Model_8;Model_9;Model_10];
% 存储矩阵间的相关系数

for i=1 : 9
    rel = corrcoef(Model(i,:),Number_Model);
    correlation(i) = abs(rel(2));
end

%     corrcoef(a,b)
%     ans = 1.0000 0.9976 0.9976 1.0000
%     第一个1是a与a的相关系数,左边第一个0.9976是a与b相关系数,第二个0.9976是b与a相关系数,第二个1是b与b的相关系数
%     相关系数      相关程度
%     0.00-±0.30    微相关
%     ±0.30-±0.50  实相关
%     ±0.50-±0.80  显著相关
%     ±0.80-±1.00  高度相关

d = dist(1,correlation);
[min_dist,index] = min(d);

Number = index + 1;

end

 function Shape = ParseShape( Shape_Model )
%PARSESHAPE 此处显示有关此函数的摘要
% 功能: 解析花色

% 花色投影30+20模板 R--红桃 B--黑桃 F--方片 M--梅花
Model_R = [0.2439, 0.4878, 0.60975,0.70731,0.78048,0.82926,0.85365,0.90243,0.92682,0.97561,1,      1,      1,      1,      1,      1,      1,      0.97561,0.95121,0.92682,0.90243,0.90243,0.85365,0.82926,0.80487,0.78048,0.75609,0.68292,0.68292,0.44947,0.25581,0.44186,0.56976,0.66279,0.76744,0.83721,0.89534,0.96511,0.94186,0.83721,0.77906,0.88372,0.93023,0.89534,0.84883,0.77906,0.7093, 0.60465,0.51162,0.33333];
Model_B = [0.23684,0.26315,0.31578,0.34211,0.36842,0.42105,0.44736,0.47368,0.52631,0.57894,0.63157,0.65789,0.68421,0.73684,0.76315,0.78947,0.84211,0.86842,0.89473,0.94736,0.94736,0.97368,1,      1,      1,      1,      1,      1,      1,      0.44078,0.22222,0.31111,0.35555,0.42222,0.46666,0.51111,0.53333,0.57777,0.6,    0.62222,0.64444,0.68888,0.68888,0.71111,0.73333,0.77777,0.8,    1,      1,      0.61988];
Model_F = [0.3,    0.36666,0.36666,0.43333,0.5,    0.5,    0.56666,0.63333,0.7,    0.73333,0.76666,0.83333,0.9,    0.96666,1,      1,      1,      1,      1,      1,      1,      1,      1,      1,      0.96666,0.9,    0.83333,0.76666,0.7,    0.47037,0.26315,0.31578,0.36842,0.42105,0.47368,0.55263,0.63157,0.68421,0.76315,0.81578,0.94736,1,      1,      1,      1,      1,      1,      1,      1,      0.64114];
Model_M = [0.24444,0.28888,0.33333,0.37777,0.4,    0.44444,0.46666,0.46666,0.46666,0.48888,0.48888,0.46666,0.46666,0.46666,0.46666,0.42222,0.37777,0.37777,0.33333,0.66666,0.77777,0.86666,0.91111,0.95555,0.95555,1,      1,      1,      1,      0.51746,0.23,   0.3,    0.38,   0.42,   0.42,   0.44,   0.64,   0.71,   0.74,   0.8,    0.96,   1,      0.85,   0.77,   0.7,    0.66,   0.51,   0.42,   0.42,   0.32571];

Model = [Model_R;Model_B;Model_F;Model_M];
% 存储矩阵间的相关系数
correlation = [0,0,0,0];
for i=1 : 4
    
end

d = dist(1,correlation);
[min_dist,index] = min(d);
switch index
    case 1
        Shape = '红桃';
    case 2
        Shape = '黑桃';
    case 3
        Shape = '方片';
    case 4
        Shape = '梅花';
end
end




  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97

四、运行结果

在这里插入图片描述

五、matlab版本及参考文献

1 matlab版本
2014a

2 参考文献
[1] 蔡利梅.MATLAB图像处理——理论、算法与实例分析[M].清华大学出版社,2020.
[2]杨丹,赵海滨,龙哲.MATLAB图像处理实例详解[M].清华大学出版社,2013.
[3]周品.MATLAB图像处理与图形用户界面设计[M].清华大学出版社,2013.
[4]刘成龙.精通MATLAB图像处理[M].清华大学出版社,2015.

文章来源: qq912100926.blog.csdn.net,作者:海神之光,版权归原作者所有,如需转载,请联系作者。

原文链接:qq912100926.blog.csdn.net/article/details/120598243

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。