用python实现基于概率分布的超声图像与自然图像性质差异分析,不用greycomatrix和greycoprops,使用opencv,两种图像的文件类型都是jpg,超声图像的文件路径是D:\zzz\us5,自然图像的文件路径是D:\zzz\na4,两种图像都有84张,图片形状相同,图像名称是1到84的顺序数,需要对比两种图像的特征必须有颜色,纹理,形状,差异性分析方法也需要多种,包括完整详细最新的代码
时间: 2023-09-23 14:03:41 浏览: 73
以下是一种实现方法,分别使用了颜色直方图、Gabor滤波器和形状匹配进行特征提取和差异性分析:
```python
import cv2
import numpy as np
from matplotlib import pyplot as plt
# 计算颜色直方图
def calc_hist(image):
hist = cv2.calcHist([image], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256])
hist = cv2.normalize(hist, hist).flatten()
return hist
# 计算Gabor滤波器响应
def calc_gabor(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
filters = []
ksize = 31
for theta in np.arange(0, np.pi, np.pi/4):
kern = cv2.getGaborKernel((ksize, ksize), 4.0, theta, 10.0, 0.5, 0, ktype=cv2.CV_32F)
kern /= 1.5*kern.sum()
filters.append(kern)
feats = []
for kern in filters:
filtered = cv2.filter2D(gray, cv2.CV_8UC3, kern)
feats.append(np.mean(filtered))
feats = np.array(feats)
return feats
# 计算形状匹配得分
def calc_shape(image1, image2):
gray1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
sift = cv2.SIFT_create()
kp1, des1 = sift.detectAndCompute(gray1, None)
kp2, des2 = sift.detectAndCompute(gray2, None)
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1, des2, k=2)
good = []
for m, n in matches:
if m.distance < 0.75*n.distance:
good.append([m])
score = len(good) / min(len(kp1), len(kp2))
return score
# 读取超声图像和自然图像
us_images = []
na_images = []
for i in range(1, 85):
us_image = cv2.imread(f'D:/zzz/us5/{i}.jpg')
na_image = cv2.imread(f'D:/zzz/na4/{i}.jpg')
us_images.append(us_image)
na_images.append(na_image)
# 计算颜色直方图和Gabor滤波器响应
us_feats = []
na_feats = []
for i in range(84):
us_hist = calc_hist(us_images[i])
us_gabor = calc_gabor(us_images[i])
us_feats.append(np.concatenate([us_hist, us_gabor]))
na_hist = calc_hist(na_images[i])
na_gabor = calc_gabor(na_images[i])
na_feats.append(np.concatenate([na_hist, na_gabor]))
us_feats = np.array(us_feats)
na_feats = np.array(na_feats)
# 计算形状匹配得分
shape_scores = []
for i in range(84):
shape_scores.append(calc_shape(us_images[i], na_images[i]))
# 绘制颜色直方图和Gabor滤波器响应的分布图
plt.figure(figsize=(20, 10))
for i in range(24):
plt.subplot(4, 6, i+1)
plt.hist(us_feats[:, i], bins=50, alpha=0.5, label='Ultrasound')
plt.hist(na_feats[:, i], bins=50, alpha=0.5, label='Natural')
plt.title(f'Hist {i+1}')
plt.legend()
# 绘制形状匹配得分的分布图
plt.figure(figsize=(10, 5))
plt.hist(shape_scores, bins=20)
plt.title('Shape Score')
# 计算各特征之间的相关性
corr = np.corrcoef(np.transpose(np.concatenate([us_feats, na_feats])))
plt.figure(figsize=(10, 10))
plt.imshow(corr, cmap='coolwarm')
plt.colorbar()
plt.title('Correlation Matrix')
plt.xticks(np.arange(48), np.arange(1, 49))
plt.yticks(np.arange(48), np.arange(1, 49))
# 计算各特征的差异性指标
us_mean = np.mean(us_feats, axis=0)
na_mean = np.mean(na_feats, axis=0)
us_cov = np.cov(np.transpose(us_feats))
na_cov = np.cov(np.transpose(na_feats))
diff_means = np.abs(us_mean - na_mean)
diff_covs = np.abs(us_cov - na_cov)
diff_feats = np.concatenate([diff_means, np.diag(diff_covs)])
diff_feats /= np.max(diff_feats)
# 打印各特征的差异性指标
feat_names = [f'Hist {i+1}' for i in range(24)] + [f'Gabor {i+1}' for i in range(24)]
for i, diff_feat in enumerate(diff_feats):
print(f'{feat_names[i]}: {diff_feat:.3f}')
# 计算总体差异性指标
diff_total = np.mean(diff_feats) * np.mean(shape_scores)
print(f'Total Diff: {diff_total:.3f}')
```
需要注意的是,这种方法只是一种简单的实现,实际应用时可能需要根据实际情况进行改进和优化。
阅读全文