用python实现基于概率分布的超声图像与自然图像性质差异分析这一课题,使用opencv, 两种图像的文件类型都是jpg,超声图像的文件路径是‘D:\zzz\us5’,自然图像的文件路径是‘D:\zzz\na4’,两种图像都有84张,图像的名称都是1到84的顺序数,两种图像的形状大小相同,需要对比两种图像的特征必须有颜色,纹理和形态学特征,差异性检验方法也需要多种,请提供完整详细的代码
时间: 2023-12-03 08:44:11 浏览: 22
以下是我编写的基于概率分布的超声图像与自然图像性质差异分析的代码,包括颜色、纹理和形态学特征的对比和多种差异性检验方法:
```python
import cv2
import numpy as np
from scipy.stats import kstest, entropy, wasserstein_distance
from skimage.feature import greycomatrix, greycoprops
# 定义函数计算图像的特征
def calculate_feature(img):
# 颜色特征
b, g, r = cv2.split(img)
r_mean, g_mean, b_mean = np.mean(r), np.mean(g), np.mean(b)
r_std, g_std, b_std = np.std(r), np.std(g), np.std(b)
# 纹理特征
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
glcm = greycomatrix(gray, [1], [0], symmetric=True, normed=True)
contrast = greycoprops(glcm, 'contrast')[0, 0]
correlation = greycoprops(glcm, 'correlation')[0, 0]
energy = greycoprops(glcm, 'energy')[0, 0]
homogeneity = greycoprops(glcm, 'homogeneity')[0, 0]
# 形态学特征
canny = cv2.Canny(gray, 100, 200)
contours, _ = cv2.findContours(canny, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contour_area = 0
for contour in contours:
contour_area += cv2.contourArea(contour)
return [r_mean, g_mean, b_mean, r_std, g_std, b_std, contrast, correlation, energy, homogeneity, contour_area]
# 读取图像并计算特征
us_images = []
na_images = []
for i in range(1, 85):
us_image = cv2.imread("D:\\zzz\\us5\\{}.jpg".format(i))
na_image = cv2.imread("D:\\zzz\\na4\\{}.jpg".format(i))
us_images.append(us_image)
na_images.append(na_image)
us_features = np.array([calculate_feature(img) for img in us_images])
na_features = np.array([calculate_feature(img) for img in na_images])
# 颜色特征差异性检验
for i in range(3):
_, p_value = kstest(us_features[:, i], na_features[:, i])
print("颜色特征{}的KS检验p值为:{}".format(i+1, p_value))
for i in range(3, 6):
_, p_value = kstest(us_features[:, i], na_features[:, i])
print("颜色特征{}的KS检验p值为:{}".format(i-2, p_value))
# 纹理特征差异性检验
for i, feature_name in enumerate(['对比度', '相关性', '能量', '同质性']):
_, p_value = kstest(us_features[:, i+6], na_features[:, i+6])
print("纹理特征{}的KS检验p值为:{}".format(feature_name, p_value))
# 形态学特征差异性检验
_, p_value = kstest(us_features[:, -1], na_features[:, -1])
print("形态学特征的KS检验p值为:{}".format(p_value))
# 计算两种图像特征之间的距离
us_color = us_features[:, :3]
na_color = na_features[:, :3]
us_gray = us_features[:, 6:10]
na_gray = na_features[:, 6:10]
us_contour = us_features[:, -1]
na_contour = na_features[:, -1]
color_distance = wasserstein_distance(us_color, na_color)
gray_distance = wasserstein_distance(us_gray, na_gray)
contour_distance = wasserstein_distance(us_contour, na_contour)
print("颜色特征距离为:{}".format(color_distance))
print("灰度共生矩阵特征距离为:{}".format(gray_distance))
print("形态学特征距离为:{}".format(contour_distance))
```
在以上代码中,我们首先定义了一个`calculate_feature()`函数,用于计算图像的颜色、纹理和形态学特征。其中,颜色特征包括三个通道的均值和标准差,纹理特征使用灰度共生矩阵计算四个属性(对比度、相关性、能量、同质性),形态学特征使用Canny算子计算轮廓,并计算轮廓面积作为特征。
然后,我们读取两种图像的84张图片,并分别计算它们的特征。接着,我们使用KS检验方法来检验两种图像的特征是否有显著差异,其中颜色特征使用三个通道的均值和标准差分别计算,纹理特征使用灰度共生矩阵计算的四个属性分别计算,形态学特征使用Canny算子计算轮廓面积计算。
最后,我们使用Wasserstein距离计算两种图像特征之间的距离,分别计算颜色特征、灰度共生矩阵特征和形态学特征的距离,并输出结果。