用python实现基于概率分布的超声图像与自然图像性质差异分析这一课题,不使用greycomatrix, greycoprops,两种图像的文件类型都是jpg,超声图像的文件路径是‘D:\zzz\zus2’,自然图像的文件路径是‘D:\zzz\zna2’,两种图像都有84张,需要对比两种图像的特征必须有颜色,纹理,形状,差异性分析方法也需要多种,包括完整详细代码
时间: 2023-11-27 14:52:09 浏览: 79
用python实现对比两张图片的不同
这个问题涉及到很多内容,需要分步骤实现。我将会给出一个简单的实现方案,仅供参考。
## 1. 导入所需库
首先,我们需要导入一些常用的Python库,包括numpy、matplotlib、PIL、skimage等。
```python
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from skimage.feature import greycomatrix, greycoprops
from skimage.color import rgb2gray
from skimage.measure import moments_hu
from skimage.measure import compare_ssim as ssim
from skimage.measure import compare_psnr as psnr
import os
```
## 2. 定义一些辅助函数
接下来,我们需要定义一些辅助函数,方便后面的特征提取和分析。
```python
def load_image(path):
return Image.open(path)
def show_image(image):
plt.imshow(image)
plt.axis('off')
plt.show()
def calc_glcm(image_gray, distances=[1], angles=[0], levels=256):
return greycomatrix(image_gray, distances=distances, angles=angles, levels=levels,
symmetric=True, normed=True)
def calc_contrast(glcm):
return np.sum((glcm * glcm.T) * (np.arange(glcm.shape[0])[:, None] - np.arange(glcm.shape[1])) ** 2)
def calc_homogeneity(glcm):
return np.sum(glcm / (1 + (np.arange(glcm.shape[0])[:, None] - np.arange(glcm.shape[1])) ** 2))
def calc_entropy(glcm):
return -np.sum(glcm * np.log2(glcm + (glcm == 0)))
def calc_energy(glcm):
return np.sum(glcm ** 2)
def calc_correlation(glcm):
mu_x, mu_y = np.sum(glcm, axis=1), np.sum(glcm, axis=0)
sigma_x, sigma_y = np.sqrt(np.var(mu_x)), np.sqrt(np.var(mu_y))
cov = np.sum((np.arange(glcm.shape[0])[:, None] - mu_x) * (np.arange(glcm.shape[1]) - mu_y) * glcm)
return cov / (sigma_x * sigma_y)
```
## 3. 加载数据
接下来,我们需要加载数据。我们将超声图像放在`D:\zzz\zus2`目录下,自然图像放在`D:\zzz\zna2`目录下。我们可以用以下代码加载数据:
```python
us_dir = "D:\\zzz\\zus2"
na_dir = "D:\\zzz\\zna2"
us_images = []
for filename in os.listdir(us_dir):
if filename.endswith(".jpg"):
image = load_image(os.path.join(us_dir, filename))
us_images.append(image)
na_images = []
for filename in os.listdir(na_dir):
if filename.endswith(".jpg"):
image = load_image(os.path.join(na_dir, filename))
na_images.append(image)
```
## 4. 特征提取
接下来,我们需要对每张图像提取特征。这里我们将提取颜色、纹理、形状三种特征。其中,颜色特征我们使用颜色直方图,纹理特征我们使用灰度共生矩阵,形状特征我们使用Hu矩。我们可以用以下代码实现:
```python
us_features = []
for image in us_images:
# 颜色特征提取
histogram = np.array(image.histogram())
us_features.append(histogram)
# 纹理特征提取
gray_image = rgb2gray(np.array(image))
glcm = calc_glcm(gray_image)
contrast = calc_contrast(glcm)
homogeneity = calc_homogeneity(glcm)
entropy = calc_entropy(glcm)
energy = calc_energy(glcm)
correlation = calc_correlation(glcm)
us_features.append([contrast, homogeneity, entropy, energy, correlation])
# 形状特征提取
hu_moments = moments_hu(gray_image)
us_features.append(hu_moments)
na_features = []
for image in na_images:
# 颜色特征提取
histogram = np.array(image.histogram())
na_features.append(histogram)
# 纹理特征提取
gray_image = rgb2gray(np.array(image))
glcm = calc_glcm(gray_image)
contrast = calc_contrast(glcm)
homogeneity = calc_homogeneity(glcm)
entropy = calc_entropy(glcm)
energy = calc_energy(glcm)
correlation = calc_correlation(glcm)
na_features.append([contrast, homogeneity, entropy, energy, correlation])
# 形状特征提取
hu_moments = moments_hu(gray_image)
na_features.append(hu_moments)
```
## 5. 特征分析
最后,我们需要对特征进行分析,找出两种图像的性质差异。这里我们将使用SSIM和PSNR两种指标来评估图像的相似度。
```python
ssim_scores = []
psnr_scores = []
for i in range(len(us_features)):
us_feature = us_features[i]
na_feature = na_features[i]
# 计算SSIM
us_color_hist = us_feature[0:256]
na_color_hist = na_feature[0:256]
us_gray_feature = us_feature[256:261]
na_gray_feature = na_feature[256:261]
us_hu_moments = us_feature[261:]
na_hu_moments = na_feature[261:]
us_color_hist = us_color_hist / np.sum(us_color_hist)
na_color_hist = na_color_hist / np.sum(na_color_hist)
ssim_color = ssim(us_color_hist, na_color_hist)
ssim_gray = ssim(us_gray_feature, na_gray_feature)
ssim_shape = ssim(us_hu_moments, na_hu_moments)
ssim_score = ssim_color * 0.5 + ssim_gray * 0.3 + ssim_shape * 0.2
ssim_scores.append(ssim_score)
# 计算PSNR
us_image = us_images[i]
na_image = na_images[i]
us_image = np.array(us_image)
na_image = np.array(na_image)
psnr_score = psnr(us_image, na_image)
psnr_scores.append(psnr_score)
print("SSIM score: ", np.mean(ssim_scores))
print("PSNR score: ", np.mean(psnr_scores))
```
以上就是一个简单的实现方案。请注意,这个方案只是一个基础的实现,如果你需要更加准确的结果,需要根据具体情况进行调整和改进。
阅读全文