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)

相关推荐
非凡ghost3 分钟前
MPC-QT视频播放器(基于Qt框架播放器)
开发语言·windows·qt·音视频·软件需求
转基因4 分钟前
C++的IO流
开发语言·c++
一碗绿豆汤6 分钟前
Java语言概述和开发环境-1
java·开发语言
愈努力俞幸运10 分钟前
rust安装
开发语言·后端·rust
清水白石00819 分钟前
深入 Python 的底层世界:从 C 扩展到 ctypes 与 Cython 的本质差异全解析
c语言·python·neo4j
Amelia11111126 分钟前
day49
python
天天进步201533 分钟前
【Nanobrowser源码分析4】交互篇: 从指令到动作:模拟点击、滚动与输入的底层实现
开发语言·javascript·ecmascript
console.log('npc')40 分钟前
vue2中子组件父组件的修改参数
开发语言·前端·javascript
码点40 分钟前
【无标题】日文字库Japan.ini
开发语言
IT=>小脑虎44 分钟前
2026版 Python零基础小白学习知识点【基础版详解】
开发语言·python·学习