用python实现基于概率分布的超声图像与自然图像性质差异分析这一课题,不使用greycomatrix, greycoprops,两种图像的文件类型都是jpg,超声图像的文件路径是‘D:\zzz\zus2’,自然图像的文件路径是‘D:\zzz\zna2’,两种图像都有84张,需要对比两种图像的特征必须有颜色,纹理,形状,差异性分析方法也需要多种,包括完整详细代码
时间: 2023-11-27 07:52:14 浏览: 154
这个任务比较复杂,需要用到多种图像处理和机器学习的技术。以下是一个可能的实现方案,供参考:
1. 导入所需的库和模块
```python
import os
import cv2
import numpy as np
from skimage.feature import greycomatrix, greycoprops
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans
from sklearn.svm import SVC
from sklearn.metrics import classification_report
import matplotlib.pyplot as plt
```
2. 定义读取图像的函数
```python
def read_images(path):
images = []
for filename in os.listdir(path):
img = cv2.imread(os.path.join(path, filename))
images.append(img)
return images
```
3. 定义计算颜色特征的函数
```python
def compute_color_features(images):
features = []
for img in images:
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
hist, _ = np.histogram(hsv[:, :, 0], bins=180, range=[0, 180])
features.append(hist)
features = np.array(features)
pca = PCA(n_components=3)
features = pca.fit_transform(features)
return features
```
4. 定义计算纹理特征的函数
```python
def compute_texture_features(images):
features = []
for img in images:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
glcm = greycomatrix(gray, [5], [0], 256, symmetric=True, normed=True)
contrast = greycoprops(glcm, 'contrast')[0, 0]
dissimilarity = greycoprops(glcm, 'dissimilarity')[0, 0]
homogeneity = greycoprops(glcm, 'homogeneity')[0, 0]
energy = greycoprops(glcm, 'energy')[0, 0]
correlation = greycoprops(glcm, 'correlation')[0, 0]
features.append([contrast, dissimilarity, homogeneity, energy, correlation])
features = np.array(features)
return features
```
5. 定义计算形状特征的函数
```python
def compute_shape_features(images):
features = []
for img in images:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
canny = cv2.Canny(gray, 100, 200)
contours, _ = cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
area = cv2.contourArea(contours[0])
perimeter = cv2.arcLength(contours[0], True)
circularity = 4 * np.pi * area / (perimeter ** 2)
features.append([area, perimeter, circularity])
features = np.array(features)
return features
```
6. 加载图像并提取特征
```python
us_images = read_images('D:/zzz/zus2') # 超声图像路径
na_images = read_images('D:/zzz/zna2') # 自然图像路径
us_color_features = compute_color_features(us_images)
na_color_features = compute_color_features(na_images)
us_texture_features = compute_texture_features(us_images)
na_texture_features = compute_texture_features(na_images)
us_shape_features = compute_shape_features(us_images)
na_shape_features = compute_shape_features(na_images)
```
7. 比较两种图像的颜色特征
```python
kmeans = KMeans(n_clusters=2)
X = np.vstack((us_color_features, na_color_features))
y = np.array([0] * len(us_color_features) + [1] * len(na_color_features))
y_pred = kmeans.fit_predict(X)
y_pred[y_pred == 0] = 2
y_pred[y_pred == 1] = 0
y_pred[y_pred == 2] = 1
print(classification_report(y, y_pred))
```
8. 比较两种图像的纹理特征
```python
svc = SVC(kernel='linear')
X = np.vstack((us_texture_features, na_texture_features))
y = np.array([0] * len(us_texture_features) + [1] * len(na_texture_features))
y_pred = svc.fit(X, y).predict(X)
print(classification_report(y, y_pred))
```
9. 比较两种图像的形状特征
```python
kmeans = KMeans(n_clusters=2)
X = np.vstack((us_shape_features, na_shape_features))
y = np.array([0] * len(us_shape_features) + [1] * len(na_shape_features))
y_pred = kmeans.fit_predict(X)
y_pred[y_pred == 0] = 2
y_pred[y_pred == 1] = 0
y_pred[y_pred == 2] = 1
print(classification_report(y, y_pred))
```
10. 可视化结果
```python
fig, ax = plt.subplots(1, 3, figsize=(15, 5))
ax[0].scatter(us_color_features[:, 0], us_color_features[:, 1], color='r', label='Ultrasound')
ax[0].scatter(na_color_features[:, 0], na_color_features[:, 1], color='b', label='Natural')
ax[0].set_xlabel('PCA1')
ax[0].set_ylabel('PCA2')
ax[0].legend()
ax[1].scatter(us_texture_features[:, 0], us_texture_features[:, 1], color='r', label='Ultrasound')
ax[1].scatter(na_texture_features[:, 0], na_texture_features[:, 1], color='b', label='Natural')
ax[1].set_xlabel('Contrast')
ax[1].set_ylabel('Dissimilarity')
ax[1].legend()
ax[2].scatter(us_shape_features[:, 0], us_shape_features[:, 1], color='r', label='Ultrasound')
ax[2].scatter(na_shape_features[:, 0], na_shape_features[:, 1], color='b', label='Natural')
ax[2].set_xlabel('Area')
ax[2].set_ylabel('Perimeter')
ax[2].legend()
plt.show()
```
这份代码实现了三种特征的提取和多种分类算法的比较,但是可能还有很多改进的空间,比如可以尝试其他的特征提取算法和机器学习模型,或者使用深度学习的方法。
阅读全文