用python实现基于概率分布的超声图像与自然图像性质差异分析这一课题,使用opencv,两种图像的文件类型都是jpg,超声图像的文件路径是‘D:\zzz\zus2’,自然图像的文件路径是‘D:\zzz\zna2’,两种图像都有84张,需要对比两种图像的特征必须有颜色,纹理,形状,差异性分析方法也需要多种,包括完整详细代码
时间: 2023-12-03 19:43:41 浏览: 153
图像检索.rar_B7W4_actuallyj5p_learning python_图像检索_图像检索 python
以下是一个基于概率分布的超声图像与自然图像性质差异分析的 Python 代码,使用了 OpenCV 库:
```python
import cv2
import numpy as np
import os
# 定义文件路径
zus_path = 'D:/zzz/zus2'
zna_path = 'D:/zzz/zna2'
# 定义函数获取图像的颜色直方图、纹理特征和形状特征
def get_features(image):
# 颜色直方图
b_hist = cv2.calcHist([image], [0], None, [256], [0, 256])
g_hist = cv2.calcHist([image], [1], None, [256], [0, 256])
r_hist = cv2.calcHist([image], [2], None, [256], [0, 256])
color_feature = np.concatenate((b_hist, g_hist, r_hist), axis=0)
# 纹理特征
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
texture_feature = cv2.calcHist([cv2.Canny(gray, 100, 200)], [0], None, [256], [0, 256])
# 形状特征
contours, _ = cv2.findContours(cv2.Canny(gray, 100, 200), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if len(contours) > 0:
contour = max(contours, key=cv2.contourArea)
_, _, w, h = cv2.boundingRect(contour)
aspect_ratio = float(w) / h
area = cv2.contourArea(contour)
extent = float(area) / (w * h)
hull = cv2.convexHull(contour)
hull_area = cv2.contourArea(hull)
solidity = float(area) / hull_area
shape_feature = np.array([aspect_ratio, extent, solidity])
else:
shape_feature = np.zeros((1, 3))
return color_feature, texture_feature, shape_feature
# 定义函数获取图像的差异性
def get_difference(image1, image2):
# 颜色差异
color_feature1, _, _ = get_features(image1)
color_feature2, _, _ = get_features(image2)
color_diff = cv2.compareHist(color_feature1, color_feature2, cv2.HISTCMP_CHISQR_ALT)
# 纹理差异
_, texture_feature1, _ = get_features(image1)
_, texture_feature2, _ = get_features(image2)
texture_diff = cv2.compareHist(texture_feature1, texture_feature2, cv2.HISTCMP_CHISQR_ALT)
# 形状差异
_, _, shape_feature1 = get_features(image1)
_, _, shape_feature2 = get_features(image2)
shape_diff = np.sqrt(np.sum(np.square(shape_feature1 - shape_feature2)))
return color_diff, texture_diff, shape_diff
# 加载超声图像和自然图像
zus_images = []
for file in os.listdir(zus_path):
image = cv2.imread(os.path.join(zus_path, file))
zus_images.append(image)
zna_images = []
for file in os.listdir(zna_path):
image = cv2.imread(os.path.join(zna_path, file))
zna_images.append(image)
# 计算每对图像的差异性
results = np.zeros((84, 84, 3))
for i in range(84):
for j in range(84):
color_diff, texture_diff, shape_diff = get_difference(zus_images[i], zna_images[j])
results[i, j, 0] = color_diff
results[i, j, 1] = texture_diff
results[i, j, 2] = shape_diff
# 输出结果
print('颜色差异:\n', np.mean(results[:, :, 0], axis=1))
print('纹理差异:\n', np.mean(results[:, :, 1], axis=1))
print('形状差异:\n', np.mean(results[:, :, 2], axis=1))
```
该代码首先定义了两个文件路径,分别为超声图像和自然图像的文件路径。然后定义了两个函数 `get_features` 和 `get_difference`,分别用于获取图像的特征和差异性。其中 `get_features` 函数获取了图像的颜色直方图、纹理特征和形状特征,使用了 OpenCV 库计算。`get_difference` 函数计算了两幅图像的颜色、纹理和形状的差异性,使用了 OpenCV 库计算直方图差异和欧氏距离。
接下来加载了超声图像和自然图像,将每对图像的差异性计算出来,并输出结果。结果使用了 numpy 库计算每一列的平均值,分别表示颜色差异、纹理差异和形状差异。
需要注意的是,在该代码中只使用了 84 张超声图像和自然图像,如果需要对更多的图像进行差异性分析,可以修改代码中的文件路径和循环次数。
阅读全文