求能用python实现基于概率分布的超声图像与自然图像性质差异分析这一课题的完整详细代码,其中超声图像所在的文件夹路径是'D:/zzz/us5',自然图像所在的文件夹路径是'D:/zzz/na4',两种图像的类型都是jpg,两种图像都有84张,两种图像的形状都相同,分别对比的特征需要包含颜色,形态和纹理,最后利用K-S检验来比较两种图像的概率密度函数
时间: 2023-12-06 17:45:06 浏览: 30
由于这个问题涉及到多个方面的知识,所以需要分步骤来解决。
1. 加载图像
首先,我们需要使用Python中的OpenCV库来加载图像。以下是加载图像的代码:
``` python
import cv2
import os
us_path = 'D:/zzz/us5' # 超声图像文件夹路径
na_path = 'D:/zzz/na4' # 自然图像文件夹路径
us_images = [] # 存储超声图像
na_images = [] # 存储自然图像
# 加载超声图像
for filename in os.listdir(us_path):
if filename.endswith('.jpg'):
img = cv2.imread(os.path.join(us_path, filename))
us_images.append(img)
# 加载自然图像
for filename in os.listdir(na_path):
if filename.endswith('.jpg'):
img = cv2.imread(os.path.join(na_path, filename))
na_images.append(img)
```
2. 提取特征
接下来,我们需要对图像进行特征提取。我们将使用OpenCV和scikit-image库来提取颜色,形态和纹理特征。
``` python
import cv2
from skimage.feature import greycomatrix, greycoprops
from skimage.color import rgb2gray
import numpy as np
# 定义一个函数来提取颜色、形态和纹理特征
def extract_features(image):
# 提取颜色特征
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
hist_h = cv2.calcHist([h], [0], None, [180], [0, 180])
hist_s = cv2.calcHist([s], [0], None, [256], [0, 256])
hist_v = cv2.calcHist([v], [0], None, [256], [0, 256])
color_features = np.concatenate((hist_h, hist_s, hist_v))
# 提取形态特征
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
area = 0
for contour in contours:
area += cv2.contourArea(contour)
shape_features = np.array([len(contours), area])
# 提取纹理特征
gray = rgb2gray(image)
glcm = greycomatrix((gray * 255).astype(np.uint8), distances=[1], angles=[0], levels=256, symmetric=True, normed=True)
texture_features = np.array([greycoprops(glcm, 'contrast')[0, 0], greycoprops(glcm, 'homogeneity')[0, 0]])
# 返回三种特征的合并
return np.concatenate((color_features, shape_features, texture_features))
```
3. 计算概率密度函数
接下来,我们将使用scipy库来计算两种图像的概率密度函数,并使用K-S检验来比较它们。
``` python
from scipy.stats import ks_2samp
us_features = np.array([extract_features(img) for img in us_images])
na_features = np.array([extract_features(img) for img in na_images])
# 计算概率密度函数
us_pdf = []
na_pdf = []
for i in range(us_features.shape[1]):
us_pdf.append(np.histogram(us_features[:, i], bins=50, density=True)[0])
na_pdf.append(np.histogram(na_features[:, i], bins=50, density=True)[0])
# 比较概率密度函数
p_values = []
for i in range(len(us_pdf)):
p_value = ks_2samp(us_pdf[i], na_pdf[i])[1]
p_values.append(p_value)
# 输出p值
print(p_values)
```
完整代码如下:
``` python
import cv2
import os
from skimage.feature import greycomatrix, greycoprops
from skimage.color import rgb2gray
import numpy as np
from scipy.stats import ks_2samp
us_path = 'D:/zzz/us5' # 超声图像文件夹路径
na_path = 'D:/zzz/na4' # 自然图像文件夹路径
us_images = [] # 存储超声图像
na_images = [] # 存储自然图像
# 加载超声图像
for filename in os.listdir(us_path):
if filename.endswith('.jpg'):
img = cv2.imread(os.path.join(us_path, filename))
us_images.append(img)
# 加载自然图像
for filename in os.listdir(na_path):
if filename.endswith('.jpg'):
img = cv2.imread(os.path.join(na_path, filename))
na_images.append(img)
# 定义一个函数来提取颜色、形态和纹理特征
def extract_features(image):
# 提取颜色特征
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
hist_h = cv2.calcHist([h], [0], None, [180], [0, 180])
hist_s = cv2.calcHist([s], [0], None, [256], [0, 256])
hist_v = cv2.calcHist([v], [0], None, [256], [0, 256])
color_features = np.concatenate((hist_h, hist_s, hist_v))
# 提取形态特征
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
area = 0
for contour in contours:
area += cv2.contourArea(contour)
shape_features = np.array([len(contours), area])
# 提取纹理特征
gray = rgb2gray(image)
glcm = greycomatrix((gray * 255).astype(np.uint8), distances=[1], angles=[0], levels=256, symmetric=True, normed=True)
texture_features = np.array([greycoprops(glcm, 'contrast')[0, 0], greycoprops(glcm, 'homogeneity')[0, 0]])
# 返回三种特征的合并
return np.concatenate((color_features, shape_features, texture_features))
us_features = np.array([extract_features(img) for img in us_images])
na_features = np.array([extract_features(img) for img in na_images])
# 计算概率密度函数
us_pdf = []
na_pdf = []
for i in range(us_features.shape[1]):
us_pdf.append(np.histogram(us_features[:, i], bins=50, density=True)[0])
na_pdf.append(np.histogram(na_features[:, i], bins=50, density=True)[0])
# 比较概率密度函数
p_values = []
for i in range(len(us_pdf)):
p_value = ks_2samp(us_pdf[i], na_pdf[i])[1]
p_values.append(p_value)
# 输出p值
print(p_values)
```
上述代码将输出特征提取和比较概率密度函数得到的p值。如果p值小于0.05,则可以拒绝零假设,即两种图像的概率密度函数不相同。