用python实现基于概率分布的超声图像与自然图像性质差异分析,超声图像的文件路径是D:\zzz\na5,自然图像的文件路径是D:\zzz\us5,两种图像都有778张,请注意每张图像的长和宽不都一样可能需要调整,对比的特征包含纹理和形态,需要可视化使结果尽量明显
时间: 2024-01-24 22:17:06 浏览: 65
首先,我们需要安装必要的库:numpy、scikit-image、matplotlib 和 opencv-python。
然后,我们可以按照以下步骤进行超声图像和自然图像的性质差异分析:
1. 读取超声图像和自然图像,并将它们调整为统一的大小。
```python
import os
import cv2
import numpy as np
from skimage import io, transform
us_path = 'D:/zzz/us5/'
na_path = 'D:/zzz/na5/'
us_images = []
na_images = []
for filename in os.listdir(us_path):
img = io.imread(os.path.join(us_path, filename))
img = transform.resize(img, (256, 256))
us_images.append(img)
for filename in os.listdir(na_path):
img = io.imread(os.path.join(na_path, filename))
img = transform.resize(img, (256, 256))
na_images.append(img)
```
2. 计算每张图像的纹理特征。
```python
from skimage.feature import greycomatrix, greycoprops
def get_texture_features(image):
glcm = greycomatrix(image, distances=[5], angles=[0], levels=256, symmetric=True, normed=True)
contrast = greycoprops(glcm, 'contrast')[0][0]
homogeneity = greycoprops(glcm, 'homogeneity')[0][0]
energy = greycoprops(glcm, 'energy')[0][0]
correlation = greycoprops(glcm, 'correlation')[0][0]
return [contrast, homogeneity, energy, correlation]
us_texture_features = [get_texture_features(img) for img in us_images]
na_texture_features = [get_texture_features(img) for img in na_images]
```
3. 计算每张图像的形态特征。
```python
from skimage.measure import moments, moments_central, moments_normalized, moments_hu
def get_shape_features(image):
moments_val = moments(image)
moments_cent_val = moments_central(image, moments_val[0, 1], moments_val[1, 0])
moments_norm_val = moments_normalized(moments_cent_val)
moments_hu_val = moments_hu(moments_norm_val)
return moments_hu_val.tolist()
us_shape_features = [get_shape_features(img) for img in us_images]
na_shape_features = [get_shape_features(img) for img in na_images]
```
4. 将纹理特征和形态特征组合起来,形成特征向量。
```python
us_features = np.hstack((us_texture_features, us_shape_features))
na_features = np.hstack((na_texture_features, na_shape_features))
```
5. 通过 t 检验检查两组特征向量是否具有显著差异。
```python
from scipy.stats import ttest_ind
def check_significant_difference(f1, f2):
t, p = ttest_ind(f1, f2, equal_var=False)
return p
p = check_significant_difference(us_features, na_features)
if p < 0.05:
print('There is a significant difference between ultrasound images and natural images.')
else:
print('There is no significant difference between ultrasound images and natural images.')
```
6. 可视化结果。
```python
import matplotlib.pyplot as plt
plt.scatter(us_features[:, 0], us_features[:, 1], color='red', label='Ultrasound')
plt.scatter(na_features[:, 0], na_features[:, 1], color='green', label='Natural')
plt.legend()
plt.xlabel('Contrast')
plt.ylabel('Homogeneity')
plt.show()
```
这段代码将纹理特征的对比结果可视化。您可以将其扩展到其他特征并进行可视化,以使结果更加明显。
阅读全文