求能用python实现基于概率分布的超声图像与自然图像性质差异分析这一课题的完整详细代码,其中超声图像所在的文件夹路径是'D:/zzz/us5',自然图像所在的文件夹路径是'D:/zzz/na4',两种图像的类型都是jpg,两种图像都有84张,两种图像的形状都相同,分别对比的特征需要包含颜色,形态和纹理,利用K-S检验来比较两种图像的概率密度函数,最后用可视化展示结果
时间: 2023-12-06 13:45:17 浏览: 95
好的,首先需要安装必要的Python库,包括numpy, pandas, matplotlib, opencv-python和scipy。可以使用以下命令进行安装:
```
pip install numpy pandas matplotlib opencv-python scipy
```
接下来,可以使用以下代码实现上述任务:
```python
import os
import cv2
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.stats import ks_2samp
# 定义两个文件夹路径
us_path = 'D:/zzz/us5'
na_path = 'D:/zzz/na4'
# 读取图像并将其转换为HSV颜色空间
def read_image(path):
img = cv2.imread(path)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
return hsv
# 计算图像的颜色特征
def get_color_feature(img):
hue, sat, val = cv2.split(img)
hue_hist = cv2.calcHist([hue], [0], None, [180], [0, 180])
sat_hist = cv2.calcHist([sat], [0], None, [256], [0, 256])
val_hist = cv2.calcHist([val], [0], None, [256], [0, 256])
return np.concatenate((hue_hist.flatten(), sat_hist.flatten(), val_hist.flatten()))
# 计算图像的形态特征
def get_shape_feature(img):
edges = cv2.Canny(img, 100, 200)
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
area = sum([cv2.contourArea(cnt) for cnt in contours])
perimeter = sum([cv2.arcLength(cnt, True) for cnt in contours])
compactness = 4 * np.pi * area / perimeter ** 2
return np.array([area, perimeter, compactness])
# 计算图像的纹理特征
def get_texture_feature(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
hist = cv2.calcHist([gray], [0], None, [256], [0, 256])
return hist.flatten()
# 定义两个空列表用于存储图像特征
us_features = []
na_features = []
# 读取超声图像并提取特征
for filename in os.listdir(us_path):
if filename.endswith('.jpg'):
path = os.path.join(us_path, filename)
img = read_image(path)
color_feature = get_color_feature(img)
shape_feature = get_shape_feature(img)
texture_feature = get_texture_feature(img)
feature = np.concatenate((color_feature, shape_feature, texture_feature))
us_features.append(feature)
# 读取自然图像并提取特征
for filename in os.listdir(na_path):
if filename.endswith('.jpg'):
path = os.path.join(na_path, filename)
img = read_image(path)
color_feature = get_color_feature(img)
shape_feature = get_shape_feature(img)
texture_feature = get_texture_feature(img)
feature = np.concatenate((color_feature, shape_feature, texture_feature))
na_features.append(feature)
# 将特征列表转换为NumPy数组
us_features = np.array(us_features)
na_features = np.array(na_features)
# 计算K-S检验并输出结果
print('Color feature:')
print(ks_2samp(us_features[:, :536].flatten(), na_features[:, :536].flatten()))
print('Shape feature:')
print(ks_2samp(us_features[:, 536:539].flatten(), na_features[:, 536:539].flatten()))
print('Texture feature:')
print(ks_2samp(us_features[:, 539:].flatten(), na_features[:, 539:].flatten()))
# 可视化结果
plt.figure(figsize=(12, 4))
plt.subplot(131)
plt.hist(us_features[:, :536].flatten(), bins=50, alpha=0.5, label='US')
plt.hist(na_features[:, :536].flatten(), bins=50, alpha=0.5, label='NA')
plt.title('Color feature')
plt.legend()
plt.subplot(132)
plt.hist(us_features[:, 536:539].flatten(), bins=50, alpha=0.5, label='US')
plt.hist(na_features[:, 536:539].flatten(), bins=50, alpha=0.5, label='NA')
plt.title('Shape feature')
plt.legend()
plt.subplot(133)
plt.hist(us_features[:, 539:].flatten(), bins=50, alpha=0.5, label='US')
plt.hist(na_features[:, 539:].flatten(), bins=50, alpha=0.5, label='NA')
plt.title('Texture feature')
plt.legend()
plt.show()
```
其中`read_image`函数用于读取图像并将其转换为HSV颜色空间,`get_color_feature`函数用于计算图像的颜色特征,`get_shape_feature`函数用于计算图像的形态特征,`get_texture_feature`函数用于计算图像的纹理特征。这些函数的具体实现可以根据具体情况进行修改。最后,将三种特征的K-S检验结果和可视化结果输出。
阅读全文