当前位置:网站首页>【OpenCV 例程200篇】233. 区域特征之矩不变量
【OpenCV 例程200篇】233. 区域特征之矩不变量
2022-07-17 11:18:00 【小白YouCans】
『youcans 的 OpenCV 例程200篇 - 总目录』
【youcans 的 OpenCV 例程200篇】233. 区域特征之矩不变量
4.4 区域特征之矩不变量
矩是概率与统计中的一个概念,是随机变量的一种数字特征。矩函数在图像分析中有着广泛的应用,如模式识别、目标分类、图像编码与重构等。
把图像的像素坐标视为二维随机变量 (X,Y),就可以用矩来描述灰度图像的特征。
图像矩是对特征进行参数描述的一种算法,通常描述了图像形状的全局特征,并提供了大量的关于该图像不同类型的几何特性信息,比如大小、位置、方向及形状等。
当图像发生平移时,几何距 m p q m_{pq} mpq 也会发生变化;中心矩 m u p q mu_{pq} mupq 具有平移不变性,但在图像旋转时会发生变化;归一化中心距不仅具有平移不变性,而且具有比例不变性(尺度不变性)。
Hu 利用二阶和三阶归一化中心距构造了 7 个不变矩 M1~M7, 在连续图像下具有平移、灰度、尺度、旋转不变性, 是高度浓缩的图像特征。不变矩能够描述图像的整体性质,从而在边缘提取、图像匹配及目标识别中得到了广泛的应用。
OpenCV 提供了函数 cv2.moments() 计算图像矩 Mu,函数 cv2.HuMoments() 计算目标轮廓的 Hu 不变矩。
函数说明:
cv.moments(array[, binaryImage]) → Mu
cv.HuMoments(Mu[, hu]) → Hu
函数 cv2.moments() 以字典(Dict)形式返回图像的矩。
函数 cv2.HuMoments() 以列表(List)形式返回目标轮廓的 Hu 不变矩。
参数说明:
array:是一幅单通道 8-bits 图像,或一个二维浮点数组(如轮廓列表 contours 中的一个轮廓)
binaryImage:指示输入图像是否二值图像
返回值 Mu 是字典格式,包括 24个键值对。
返回值 Hu 是一个列表(List),包括 7 个不变矩 M1~M7,浮点数。
具体定义和计算公式为:
M 1 = n u 20 + n u 02 M 2 = ( n u 20 − n u 02 ) 2 + 4 ( n u 11 ) 2 M 3 = ( n u 30 − 3 n u 12 ) 2 + ( 3 n u 21 − n u 03 ) 2 M 4 = ( n u 30 + n u 12 ) 2 + ( n u 21 + n u 03 ) 2 M 5 = ( n u 30 − 3 n u 12 ) ( n u 30 + n u 12 ) [ ( n u 30 + n u 12 ) 2 − 3 ( n u 21 + n u 03 ) 2 ] + ( 3 n u 21 − n u 03 ) ( n u 21 + n u 03 ) [ 3 ( n u 30 + n u 12 ) 2 − ( n u 21 + n u 03 ) 2 ] M 6 = ( n u 20 − n u 02 ) [ ( n u 30 + n u 12 ) 2 − ( n u 21 + n u 03 ) 2 ] + 4 n u 11 ( n u 30 + n u 12 ) ( n u 21 + n u 03 ) M 7 = ( 3 n u 21 − n u 03 ) ( n u 30 + n u 12 ) [ ( n u 30 + n u 12 ) 2 − 3 ( n u 21 + n u 03 ) 2 ] − ( n u 30 − 3 n u 12 ) ( n u 21 + n u 03 ) [ 3 ( n u 30 + n u 12 ) 2 − ( n u 21 + n u 03 ) 2 ] \begin{aligned} M1 = & nu_{20} + nu_{02}\\ M2 = & (nu_{20} - nu_{02})^2 + 4(nu_{11})^2\\ M3 = & (nu_{30} - 3nu_{12})^2 + (3nu_{21} - nu_{03})^2 \\ M4 = & (nu_{30} + nu_{12})^2 + (nu_{21} + nu_{03})^2\\ M5 = & (nu_{30}-3nu_{12})(nu_{30}+nu_{12})[(nu_{30}+nu_{12})^{2} -3(nu_{21}+nu_{03})^2]\\ + & (3nu_{21}-nu_{03})(nu_{21}+nu_{03})[3(nu_{30}+nu_{12})^2-(nu_{21}+nu_{03})^2]\\ M6 = & (nu_{20} - nu_{02}) [(nu_{30}+nu_{12})^2-(nu_{21}+nu_{03})^2]\\ + &4nu_{11}(nu_{30} + nu_{12})(nu_{21} + nu_{03})\\ M7 = & (3nu_{21}-nu_{03})(nu_{30} + nu_{12})[(nu_{30}+nu_{12})^2-3(nu_{21}+nu_{03})^2]\\ - & (nu_{30}-3nu_{12})(nu_{21}+nu_{03})[3(nu_{30}+nu_{12})^2-(nu_{21}+nu_{03})^2]\\ \end{aligned} M1=M2=M3=M4=M5=+M6=+M7=−nu20+nu02(nu20−nu02)2+4(nu11)2(nu30−3nu12)2+(3nu21−nu03)2(nu30+nu12)2+(nu21+nu03)2(nu30−3nu12)(nu30+nu12)[(nu30+nu12)2−3(nu21+nu03)2](3nu21−nu03)(nu21+nu03)[3(nu30+nu12)2−(nu21+nu03)2](nu20−nu02)[(nu30+nu12)2−(nu21+nu03)2]4nu11(nu30+nu12)(nu21+nu03)(3nu21−nu03)(nu30+nu12)[(nu30+nu12)2−3(nu21+nu03)2](nu30−3nu12)(nu21+nu03)[3(nu30+nu12)2−(nu21+nu03)2]
不变矩 M1~M7 又称矩不变量, 在连续图像下具有平移、灰度、尺度、旋转不变性, 是高度浓缩的图像特征。
例程 14.14:区域特征之不变矩
# # 14.14 特征描述之不变矩
gray = cv2.imread("../images/Fig1137.tif", flags=0) # 灰度图像
height, width = gray.shape # (568, 568)
# 图像的平移,缩放,旋转和镜像
grayList = []
grayList.append(gray) # [0],原始图像
mat = np.float32([[1, 0, 50], [0, 1, 50]])
grayList.append(cv2.warpAffine(gray, mat, (height, width))) # [1],图像平移
top, bottom, left, right = height//4, height//4, width//4, width//4
grayResize = cv2.resize(gray, (width//2, height//2)) # 图像缩放 (284, 284)
replicate = cv2.copyMakeBorder(grayResize, top, bottom, left, right, cv2.BORDER_CONSTANT, value=0)
grayList.append(replicate) # [2],图像缩放并填充至原来尺寸 (568, 568)
grayList.append(cv2.flip(gray, 1)) # [3],图像镜像,水平翻转
mar = cv2.getRotationMatrix2D((width//2, height//2), angle=45, scale=1) # 图像中心作为旋转中心
rotate = cv2.warpAffine(gray, mar, (height, width)) # 旋转变换,默认为黑色填充
grayList.append(rotate) # [4],图像旋转 45度
grayList.append(cv2.rotate(gray, cv2.ROTATE_90_COUNTERCLOCKWISE)) # [5],图像逆时针旋转90度
print(gray.shape, grayResize.shape, replicate.shape, rotate.shape,len(grayList))
plt.figure(figsize=(9, 6))
for i in range(len(grayList)):
moments = cv2.moments(grayList[i]) # 返回几何矩 mpq, 中心矩 mupq 和归一化矩 nupq
huM = cv2.HuMoments(moments) # 计算 Hu 不变矩
plt.subplot(2,3,i+1), plt.axis('off'), plt.imshow(grayList[i], 'gray')
# print("Moments of gray:\n", moments)
print("HuMoments of gray:\n", np.log10(np.abs(huM.T)).round(4))
plt.show()

运行结果:
[[ -2.8662 -7.1265 -10.4109 -10.3742 -21.3674 -13.9417 -20.7809]]
[[ -2.8662 -7.1265 -10.4109 -10.3742 -21.3674 -13.9417 -20.7809]]
[[ -2.8666 -7.1285 -10.4137 -10.3769 -21.3734 -13.9453 -20.7863]]
[[ -2.8662 -7.1265 -10.4109 -10.3742 -21.3674 -13.9417 -20.7809]]
[[ -2.8662 -7.1266 -10.4106 -10.3743 -21.3693 -13.9418 -20.7808]]
[[ -2.8662 -7.1265 -10.4109 -10.3742 -21.3674 -13.9417 -20.7809]]
运行结果表明,矩不变量 M1~M7 具有平移、灰度、尺度、旋转不变性, 是高度浓缩的图像特征。
【本节完】
版权声明:
[email protected] 原创作品,转载必须标注原文链接:(https://blog.csdn.net/youcans/article/details/125704724)
Copyright 2022 youcans, XUPT
Crated:2022-7-10
边栏推荐
- 金纳米粒子修饰MIL-101骨架材料(AuNPs/MIL-101)/负载COF-TpPa-1(Au NPs/COF-TpPa-1)|齐岳试剂
- Chapter 4 - first order multi-agent system consistency - > continuous time system consistency with time delay [program code]
- Add - before the command in makefile to ignore the error caused by the command and continue to execute the next command
- PTA 1037 change at Hogwarts
- mof定制产品|N-K2Ti4O9/g-C3N4/UiO-66三元复合材料|纸基Au-AgInSe2-ZIF-8纳米复合材料
- 506.相对名次
- Chapter VIII vector of STL
- 第4章-一阶多智体系统一致性 -> 连续时间含时延系统一致性【程序代码】
- CLWY权限管理(二)--- 用户模块
- 工程效能CI/CD之流水线引擎的建设实践
猜你喜欢

Chapter VIII vector of STL

金属有机骨架/碳化氮纳米片(UiO-66/HOCN)复合材料|MIL-101负载Au-Pd合金纳米粒子|化学试剂mof定制
![[fishing artifact] UI library second low code tool - form part (II) sub control](/img/84/f6882fb703613693f85685f4942383.png)
[fishing artifact] UI library second low code tool - form part (II) sub control

Componentized advanced -- slot

ES Restful操作

What is the product power of lantu dreamer?

Huawei wireless devices are configured with static load balancing

Talking about the informatization planning of industrial enterprises

Detailed explanation of C51 common data types

Chapter IX deque of STL
随机推荐
How to correctly execute jedis unit test
Part I - Fundamentals of C language_ 4. Procedure flow structure
【C语言】浅涉常量、变量
Chapter 4 - first order multi-agent system consistency - > switching topology system consistency
CLWY权限管理(二)--- 用户模块
Convert video format to GIF picture format
mof定制材料|双金属CuNi-MOF纳米材料|核—壳结构[email protected]纳米复合材料|ZIF-8/聚偏氟乙烯复合纳米纤维膜PVDF
氮杂环分子改性UiO-66-NH2|聚乙烯亚胺改性UiO-66-NH2|[email protected]@ZIF67纳米材料
Chapter 12 list of STL
硫化镉负载MIL-125(Ti)|链霉亲和素(SA)-锆基卟啉MOF复合材料([email protected])|壳核结构
Brilliant use output
565. Array nesting / Sword finger offer II 001 Integer division
Chapter 4 - consistency of first-order multi-agent systems - > consistency of continuous time systems with time delays
npm使用
水下机器人ROV和AUV
Traffic ranking 100W website
506.相对名次
第4章-一阶多智体系统一致性 -> 切换拓扑系统一致性
Es restful operation
Dedecms dream weaving article list Title repeated display solution