humoments.flatten
时间: 2023-08-14 13:02:22 浏览: 46
`Hu Moments`是一组用于图像处理和计算机视觉中的特征描述符,它们是一组基于图像的区域矩计算的七个矩不变量的归一化形式。这些归一化矩不受图像旋转、平移和缩放的影响,因此在物体识别和跟踪等方面具有广泛的应用。
`Hu Moments`通常是以向量的形式表示的,其中包含7个不变量,即`h1`到`h7`。而`flatten`函数则是numpy库中的一个函数,用于将一个多维数组展平为一维数组。因此,`humoments.flatten()`的作用是将`Hu Moments`向量展平为一维数组,方便后续的计算和处理。
相关问题
python Hu矩轮廓匹配
Hu矩是一种对图像进行形状匹配的方法,它是一组可旋转、平移和缩放不变的图像特征。在Python中,我们可以使用OpenCV库来计算Hu矩,以下是Hu矩轮廓匹配的基本步骤:
1. 读取待匹配图像和模板图像,并进行灰度化。
2. 对待匹配图像和模板图像进行二值化处理。
3. 使用findContours()函数找到待匹配图像和模板图像的轮廓。
4. 计算待匹配图像和模板图像的Hu矩。
5. 使用matchShapes()函数计算待匹配图像和模板图像的形状相似度。
下面是一个示例代码,演示了如何使用OpenCV计算Hu矩并进行轮廓匹配:
```python
import cv2
# 读取待匹配图像和模板图像
img1 = cv2.imread('image1.jpg')
img2 = cv2.imread('image2.jpg')
# 灰度化
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
# 二值化
ret1, thresh1 = cv2.threshold(gray1, 127, 255, cv2.THRESH_BINARY)
ret2, thresh2 = cv2.threshold(gray2, 127, 255, cv2.THRESH_BINARY)
# 轮廓提取
contours1, hierarchy1 = cv2.findContours(thresh1, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
contours2, hierarchy2 = cv2.findContours(thresh2, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
# 计算Hu矩
moments1 = cv2.moments(contours1[0])
hu1 = cv2.HuMoments(moments1).flatten()
moments2 = cv2.moments(contours2[0])
hu2 = cv2.HuMoments(moments2).flatten()
# 计算形状相似度
match_value = cv2.matchShapes(contours1[0], contours2[0], cv2.CONTOURS_MATCH_I1, 0)
# 输出结果
print('Hu moments of image1:', hu1)
print('Hu moments of image2:', hu2)
print('Match value:', match_value)
```
在上面的示例代码中,我们首先读取了两张图像并进行了灰度化和二值化处理,然后使用findContours()函数找到了两张图像的轮廓。接着,我们分别计算了两张图像的Hu矩,并使用matchShapes()函数计算了它们的形状相似度。最后,我们输出了Hu矩和形状相似度的值。
用python实现基于概率分布的超声图像与自然图像性质差异分析这一课题,使用opencv,两种图像的文件类型都是jpg,超声图像的文件路径是‘D:\zzz\zus2\’,自然图像的文件路径是‘D:\zzz\zna2\’,两种图像都有84张,需要对比两种图像的特征必须有颜色,纹理,形状,差异性分析方法也需要多种,包括完整详细代码
首先需要安装Opencv库,可以使用以下指令安装:
```python
!pip install opencv-python
```
然后开始实现特征提取和差异性分析方法。
1. 颜色特征提取
使用HSV颜色空间提取颜色特征,计算每张图像的颜色直方图,并将其归一化。
```python
import cv2
import numpy as np
def color_histogram(image):
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
hist = cv2.calcHist([hsv], [0, 1], None, [180, 256], [0, 180, 0, 256])
cv2.normalize(hist, hist, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX)
return hist.flatten()
```
2. 纹理特征提取
使用LBP算法提取纹理特征,计算每张图像的LBP直方图,并将其归一化。
```python
def lbp_histogram(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
radius = 1
numPoints = 8 * radius
lbp = cv2.LBP(gray, numPoints, radius, method=cv2.LBP_UNIFORM)
hist, _ = np.histogram(lbp.ravel(), bins=np.arange(0, numPoints + 3), range=(0, numPoints + 2))
hist = hist.astype("float")
hist /= (hist.sum() + 1e-7)
return hist
```
3. 形状特征提取
使用Hu矩算法提取形状特征,计算每张图像的Hu矩,并将其归一化。
```python
def hu_moments(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
moments = cv2.moments(gray)
huMoments = cv2.HuMoments(moments)
huMoments = -np.sign(huMoments) * np.log10(np.abs(huMoments) + 1e-7)
huMoments = huMoments.flatten()
return huMoments
```
4. 差异性分析
使用Kullback-Leibler散度计算两种图像的颜色、纹理、形状特征的差异性,并输出结果。
```python
def kl_divergence(p, q):
p = np.asarray(p, dtype=np.float)
q = np.asarray(q, dtype=np.float)
return np.sum(np.where(p != 0, p * np.log(p / q), 0))
def compare_images(path1, path2):
image1 = cv2.imread(path1)
image2 = cv2.imread(path2)
color_hist1 = color_histogram(image1)
color_hist2 = color_histogram(image2)
texture_hist1 = lbp_histogram(image1)
texture_hist2 = lbp_histogram(image2)
shape_feat1 = hu_moments(image1)
shape_feat2 = hu_moments(image2)
color_diff = kl_divergence(color_hist1, color_hist2)
texture_diff = kl_divergence(texture_hist1, texture_hist2)
shape_diff = kl_divergence(shape_feat1, shape_feat2)
print(f"Color difference: {color_diff:.6f}, Texture difference: {texture_diff:.6f}, Shape difference: {shape_diff:.6f}")
```
最后,使用以上方法计算两种图像的差异性,并输出结果。
```python
for i in range(1, 85):
print(f"Image {i}")
ultrasound_path = f"D:/zzz/zus2/{i}.jpg"
natural_path = f"D:/zzz/zna2/{i}.jpg"
compare_images(ultrasound_path, natural_path)
```
完整代码如下:
```python
import cv2
import numpy as np
def color_histogram(image):
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
hist = cv2.calcHist([hsv], [0, 1], None, [180, 256], [0, 180, 0, 256])
cv2.normalize(hist, hist, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX)
return hist.flatten()
def lbp_histogram(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
radius = 1
numPoints = 8 * radius
lbp = cv2.LBP(gray, numPoints, radius, method=cv2.LBP_UNIFORM)
hist, _ = np.histogram(lbp.ravel(), bins=np.arange(0, numPoints + 3), range=(0, numPoints + 2))
hist = hist.astype("float")
hist /= (hist.sum() + 1e-7)
return hist
def hu_moments(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
moments = cv2.moments(gray)
huMoments = cv2.HuMoments(moments)
huMoments = -np.sign(huMoments) * np.log10(np.abs(huMoments) + 1e-7)
huMoments = huMoments.flatten()
return huMoments
def kl_divergence(p, q):
p = np.asarray(p, dtype=np.float)
q = np.asarray(q, dtype=np.float)
return np.sum(np.where(p != 0, p * np.log(p / q), 0))
def compare_images(path1, path2):
image1 = cv2.imread(path1)
image2 = cv2.imread(path2)
color_hist1 = color_histogram(image1)
color_hist2 = color_histogram(image2)
texture_hist1 = lbp_histogram(image1)
texture_hist2 = lbp_histogram(image2)
shape_feat1 = hu_moments(image1)
shape_feat2 = hu_moments(image2)
color_diff = kl_divergence(color_hist1, color_hist2)
texture_diff = kl_divergence(texture_hist1, texture_hist2)
shape_diff = kl_divergence(shape_feat1, shape_feat2)
print(f"Color difference: {color_diff:.6f}, Texture difference: {texture_diff:.6f}, Shape difference: {shape_diff:.6f}")
for i in range(1, 85):
print(f"Image {i}")
ultrasound_path = f"D:/zzz/zus2/{i}.jpg"
natural_path = f"D:/zzz/zna2/{i}.jpg"
compare_images(ultrasound_path, natural_path)
```