Python Opencv实践 - 图像的距(Moments,Hu Moments)

参考资料:​​​​​​矩特征---OpenCV-Python开发指南(25)_cv2.moments_李元静的博客-CSDN博客

探究opencv中的moments函数和HuMoments函数_opencv moment_傲笑风的博客-CSDN博客

复制代码
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt


img = cv.imread("../SampleImages/stars.png", cv.IMREAD_GRAYSCALE)
plt.imshow(img, cmap=plt.cm.gray)

#直接计算灰度图的距
#cv.moments(array, binaryImage=False)
#array:输入的数组,可以是灰度图像或二维数组,比如轮廓提取结果
#binaryImage:默认False,若为True,则所有非零的像素会按值1对待,
#            也就是相当于对图像进行了二值化,阈值为1,次参数仅对图像生效
#参考资料:https://blog.csdn.net/liyuanjinglyj/article/details/113916287
img_gray_moments = cv.moments(img)
print("Image Moments:\n", img_gray_moments)
#计算灰度图的Hu距
#cv.HuMoments(moments)
#参考资料:https://blog.csdn.net/Caesar6666/article/details/103257632
img_gray_huMoments = cv.HuMoments(img_gray_moments)
print("Image HuMoments:\n", img_gray_huMoments)

#转换为二值图
ret,img_bin = cv.threshold(img, 127, 255, cv.THRESH_BINARY)
#提取轮廓
contours,hierarchy = cv.findContours(img_bin, 1, 2)
#计算轮廓的Hu距
for i in range(len(contours)):
    contour_moments = cv.moments(contours[i])
    contour_huMoments = cv.HuMoments(contour_moments)
    print("Contour HuMoments", str(i), ":\n", contour_huMoments)

相关推荐
Katecat996632 分钟前
YOLO11-SEG-AFPN-P345改进采血装置检测与识别系统
python
m0_5312371715 分钟前
C语言-操作符进阶
c语言·开发语言
q12345678909829 分钟前
FNN sin predict
开发语言·python
沐知全栈开发32 分钟前
C++ 多态
开发语言
zihan032137 分钟前
若依(RuoYi)框架核心升级:全面适配 SpringData JPA,替换 MyBatis 持久层方案
java·开发语言·前端框架·mybatis·若依升级springboot
先做个垃圾出来………42 分钟前
Python字节串“b“前缀
开发语言·python
无限进步_1 小时前
21. 合并两个有序链表 - 题解与详细分析
c语言·开发语言·数据结构·git·链表·github·visual studio
dreams_dream1 小时前
什么是迭代器和生成器
python
神奇大叔1 小时前
Java 配置文件记录
java·开发语言
三水彡彡彡彡1 小时前
C++拷贝函数:const与引用的高效实践
开发语言·c++