求能用python实现基于概率分布的超声图像与自然图像性质差异分析这一课题的完整详细代码,其中超声图像所在的文件夹路径是'D:/zzz/us5',自然图像所在的文件夹路径是'D:/zzz/na4',两种图像的类型都是jpg,两种图像都有84张,两种图像的形状都相同,分别对比的特征需要包含颜色,形态和纹理,最后利用K-S检验来比较两种图像的概率密度函数
时间: 2023-12-06 19:44:46 浏览: 36
为了实现基于概率分布的超声图像与自然图像性质差异分析,我们需要完成以下几个步骤:
1. 读取超声图像和自然图像,并将它们转换为灰度图像。
2. 提取超声图像和自然图像的颜色、形态和纹理特征。
3. 计算超声图像和自然图像的概率密度函数。
4. 使用K-S检验来比较两种图像的概率密度函数。
下面是完整的Python代码:
```python
import cv2
import numpy as np
import os
from scipy.stats import kstest
# 读取图像并转换为灰度图像
def read_and_convert_image(path):
img = cv2.imread(path)
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
return gray_img
# 提取颜色特征
def extract_color_feature(img):
hist = cv2.calcHist([img], [0], None, [256], [0, 256])
hist = cv2.normalize(hist, hist).flatten()
return hist
# 提取形态特征
def extract_shape_feature(img):
_, thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
contours, _ = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnt = max(contours, key=cv2.contourArea)
area = cv2.contourArea(cnt)
perimeter = cv2.arcLength(cnt, True)
circularity = (4 * np.pi * area) / (perimeter * perimeter)
return np.array([area, perimeter, circularity])
# 提取纹理特征
def extract_texture_feature(img):
glcm = cv2.calcGLCM(img, [1], None, 256, [0, 256])
contrast = cv2.compareHist(cv2.normalize(cv2.calcHist([glcm[:, :, 0, 0]], [0], None, [256], [0, 256]), None), np.arange(256), cv2.HISTCMP_CHISQR_ALT)
energy = cv2.compareHist(cv2.normalize(cv2.calcHist([glcm[:, :, 0, 1]], [0], None, [256], [0, 256]), None), np.arange(256), cv2.HISTCMP_CHISQR_ALT)
homogeneity = cv2.compareHist(cv2.normalize(cv2.calcHist([glcm[:, :, 0, 2]], [0], None, [256], [0, 256]), None), np.arange(256), cv2.HISTCMP_CHISQR_ALT)
return np.array([contrast, energy, homogeneity])
# 计算概率密度函数
def calculate_pdf(feature_list):
mean = np.mean(feature_list, axis=0)
cov = np.cov(feature_list.T)
return lambda x: np.exp(-0.5 * (x - mean) @ np.linalg.inv(cov) @ (x - mean).T) / np.sqrt(np.linalg.det(cov) * (2 * np.pi) ** len(mean))
# 读取超声图像
us_path = 'D:/zzz/us5'
us_images = []
for filename in os.listdir(us_path):
if filename.endswith('.jpg'):
img = read_and_convert_image(os.path.join(us_path, filename))
us_images.append(img)
# 读取自然图像
na_path = 'D:/zzz/na4'
na_images = []
for filename in os.listdir(na_path):
if filename.endswith('.jpg'):
img = read_and_convert_image(os.path.join(na_path, filename))
na_images.append(img)
# 提取特征
us_color_features = np.array([extract_color_feature(img) for img in us_images])
us_shape_features = np.array([extract_shape_feature(img) for img in us_images])
us_texture_features = np.array([extract_texture_feature(img) for img in us_images])
na_color_features = np.array([extract_color_feature(img) for img in na_images])
na_shape_features = np.array([extract_shape_feature(img) for img in na_images])
na_texture_features = np.array([extract_texture_feature(img) for img in na_images])
# 计算概率密度函数
us_color_pdf = calculate_pdf(us_color_features)
us_shape_pdf = calculate_pdf(us_shape_features)
us_texture_pdf = calculate_pdf(us_texture_features)
na_color_pdf = calculate_pdf(na_color_features)
na_shape_pdf = calculate_pdf(na_shape_features)
na_texture_pdf = calculate_pdf(na_texture_features)
# 使用K-S检验比较概率密度函数
print('颜色特征K-S检验结果:', kstest(us_color_pdf(na_color_features), na_color_pdf(us_color_features)))
print('形态特征K-S检验结果:', kstest(us_shape_pdf(na_shape_features), na_shape_pdf(us_shape_features)))
print('纹理特征K-S检验结果:', kstest(us_texture_pdf(na_texture_features), na_texture_pdf(us_texture_features)))
```
在这段代码中,我们首先读取了超声图像和自然图像,并将它们转换为灰度图像。然后,我们分别提取了超声图像和自然图像的颜色、形态和纹理特征。接着,我们使用这些特征计算了每种图像的概率密度函数,并使用K-S检验比较了它们。最后,我们打印了比较结果。
需要注意的是,这段代码中提取特征的方法并不是唯一的,你可以根据自己的需求进行修改。同样,计算概率密度函数的方法也可以根据实际情况进行修改。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)