用python实现基于概率分布的超声图像与自然图像性质差异分析,不用greycomatrix和greycoprops,使用opencv,两种图像的文件类型都是jpg,超声图像的文件路径是D:\zzz\us5,自然图像的文件路径是D:\zzz\na4,两种图像都有84张,图片形状相同,图像名称是1到84的顺序数,需要对比两种图像的特征必须有颜色,纹理,形状,需要用到概率分布,差异性检验分析方法需要使用多种,包括完整详细最新的代码
时间: 2023-09-23 08:03:41 浏览: 118
用python实现对比两张图片的不同
以下是一个基于概率分布的超声图像与自然图像性质差异分析的Python代码示例,使用OpenCV库和一些统计学工具(如Kolmogorov-Smirnov检验和t检验)。
```python
import cv2
import numpy as np
from scipy.stats import ks_2samp, ttest_ind
def color_distribution(img):
"""
计算彩色图像的颜色分布,返回三个颜色通道的直方图
"""
b_hist = cv2.calcHist([img], [0], None, [256], [0, 256])
g_hist = cv2.calcHist([img], [1], None, [256], [0, 256])
r_hist = cv2.calcHist([img], [2], None, [256], [0, 256])
return b_hist, g_hist, r_hist
def texture_distribution(img):
"""
计算灰度图像的纹理分布,返回灰度直方图和灰度共生矩阵的统计值
"""
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray_hist = cv2.calcHist([gray], [0], None, [256], [0, 256])
# 计算灰度共生矩阵,使用4个方向,距离为1
glcm = cv2.calcGLCM(gray, [1], [0, np.pi/4, np.pi/2, 3*np.pi/4], symmetric=True, normed=True)
# 计算灰度共生矩阵的统计值
contrast = cv2.compareHist(gray, cv2.normalize(glcm[:, :, 0, 0], None), cv2.HISTCMP_CORREL)
dissimilarity = cv2.compareHist(gray, cv2.normalize(glcm[:, :, 0, 1], None), cv2.HISTCMP_CORREL)
homogeneity = cv2.compareHist(gray, cv2.normalize(glcm[:, :, 0, 2], None), cv2.HISTCMP_CORREL)
energy = cv2.compareHist(gray, cv2.normalize(glcm[:, :, 0, 3], None), cv2.HISTCMP_CORREL)
correlation = cv2.compareHist(gray, cv2.normalize(glcm[:, :, 0, 4], None), cv2.HISTCMP_CORREL)
return gray_hist, contrast, dissimilarity, homogeneity, energy, correlation
def shape_distribution(img):
"""
计算图像形状的分布,返回图像的面积和周长
"""
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
# 计算轮廓
contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 计算面积和周长
area = cv2.contourArea(contours[0])
perimeter = cv2.arcLength(contours[0], True)
return area, perimeter
# 超声图像文件路径
us_path = 'D:/zzz/us5/'
# 自然图像文件路径
na_path = 'D:/zzz/na4/'
# 初始化两种图像的特征向量列表
us_color_hist = []
us_gray_hist = []
us_contrast = []
us_dissimilarity = []
us_homogeneity = []
us_energy = []
us_correlation = []
us_area = []
us_perimeter = []
na_color_hist = []
na_gray_hist = []
na_contrast = []
na_dissimilarity = []
na_homogeneity = []
na_energy = []
na_correlation = []
na_area = []
na_perimeter = []
# 读取超声图像
for i in range(1, 85):
img = cv2.imread(us_path + str(i) + '.jpg')
b_hist, g_hist, r_hist = color_distribution(img)
gray_hist, contrast, dissimilarity, homogeneity, energy, correlation = texture_distribution(img)
area, perimeter = shape_distribution(img)
us_color_hist.append([b_hist, g_hist, r_hist])
us_gray_hist.append(gray_hist)
us_contrast.append(contrast)
us_dissimilarity.append(dissimilarity)
us_homogeneity.append(homogeneity)
us_energy.append(energy)
us_correlation.append(correlation)
us_area.append(area)
us_perimeter.append(perimeter)
# 读取自然图像
for i in range(1, 85):
img = cv2.imread(na_path + str(i) + '.jpg')
b_hist, g_hist, r_hist = color_distribution(img)
gray_hist, contrast, dissimilarity, homogeneity, energy, correlation = texture_distribution(img)
area, perimeter = shape_distribution(img)
na_color_hist.append([b_hist, g_hist, r_hist])
na_gray_hist.append(gray_hist)
na_contrast.append(contrast)
na_dissimilarity.append(dissimilarity)
na_homogeneity.append(homogeneity)
na_energy.append(energy)
na_correlation.append(correlation)
na_area.append(area)
na_perimeter.append(perimeter)
# 使用Kolmogorov-Smirnov检验比较颜色直方图和灰度直方图的分布差异
p_value, _ = ks_2samp(us_color_hist, na_color_hist)
print("颜色直方图分布差异检验结果:", p_value)
p_value, _ = ks_2samp(us_gray_hist, na_gray_hist)
print("灰度直方图分布差异检验结果:", p_value)
# 使用t检验比较纹理统计值、面积和周长的分布差异
p_value, _ = ttest_ind(us_contrast, na_contrast)
print("对比度分布差异检验结果:", p_value)
p_value, _ = ttest_ind(us_dissimilarity, na_dissimilarity)
print("不相似度分布差异检验结果:", p_value)
p_value, _ = ttest_ind(us_homogeneity, na_homogeneity)
print("均匀性分布差异检验结果:", p_value)
p_value, _ = ttest_ind(us_energy, na_energy)
print("能量分布差异检验结果:", p_value)
p_value, _ = ttest_ind(us_correlation, na_correlation)
print("相关性分布差异检验结果:", p_value)
p_value, _ = ttest_ind(us_area, na_area)
print("面积分布差异检验结果:", p_value)
p_value, _ = ttest_ind(us_perimeter, na_perimeter)
print("周长分布差异检验结果:", p_value)
```
这个代码会计算每张图像的颜色分布、纹理分布和形状分布,然后使用Kolmogorov-Smirnov检验(对于颜色和灰度直方图)和t检验(对于纹理统计值、面积和周长)比较超声图像和自然图像之间的分布差异。你需要将代码中的超声图像路径和自然图像路径修改为你的实际路径。
阅读全文