用python实现基于概率分布的超声图像与自然图像性质差异分析,超声图像的文件路径是D:\zzz\na5,自然图像的文件路径是D:\zzz\us5,两种图像都有778张,请注意每张图像的长和宽不都一样可能需要调整,对比的特征包含纹理和形态,需要可视化使结果尽量明显
时间: 2024-01-24 07:17:01 浏览: 22
要实现基于概率分布的超声图像与自然图像性质差异分析,可以使用Python中的图像处理库和概率分布统计库来实现。常用的图像处理库包括OpenCV、Pillow等,常用的概率分布统计库包括NumPy和SciPy等。
首先,需要导入需要的库:
``` python
import cv2
import numpy as np
import scipy.stats as stats
import os
from matplotlib import pyplot as plt
```
然后,需要读取图像,将其调整为相同的大小,并将其转换为灰度图像:
``` python
# 读取超声图像
us_path = "D:/zzz/us5/"
us_images = []
for filename in os.listdir(us_path):
img = cv2.imread(os.path.join(us_path,filename))
if img is not None:
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_resize = cv2.resize(img_gray, (256, 256))
us_images.append(img_resize)
# 读取自然图像
na_path = "D:/zzz/na5/"
na_images = []
for filename in os.listdir(na_path):
img = cv2.imread(os.path.join(na_path,filename))
if img is not None:
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_resize = cv2.resize(img_gray, (256, 256))
na_images.append(img_resize)
```
接下来,可以计算每张图像的纹理特征和形态特征,并将其表示为概率分布。这里,我们采用灰度共生矩阵(GLCM)来计算纹理特征,并采用周长和面积比例来计算形态特征。对于每个特征,我们可以使用直方图来表示其概率分布:
``` python
# 计算纹理特征和形态特征的概率分布
us_texture_hist = []
us_shape_hist = []
for i in range(len(us_images)):
# 计算灰度共生矩阵
glcm = cv2.calcGLCM(us_images[i], [1], 0, 256, symmetric=True)
# 计算纹理特征
contrast = cv2.compareHist(cv2.normalize(glcm[:,:,0,0], None, norm_type=cv2.NORM_L1),
cv2.normalize(glcm[:,:,0,1], None, norm_type=cv2.NORM_L1),
cv2.HISTCMP_CHISQR_ALT)
homogeneity = cv2.compareHist(cv2.normalize(glcm[:,:,0,0], None, norm_type=cv2.NORM_L1),
cv2.normalize(glcm[:,:,0,1], None, norm_type=cv2.NORM_L1),
cv2.HISTCMP_INTERSECT)
us_texture_hist.append([contrast, homogeneity])
# 计算形态特征
_, contours, hierarchy = cv2.findContours(us_images[i], cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if len(contours)>0:
cnt = contours[0]
area = cv2.contourArea(cnt)
perimeter = cv2.arcLength(cnt,True)
us_shape_hist.append([perimeter/area, area/(256*256)])
na_texture_hist = []
na_shape_hist = []
for i in range(len(na_images)):
# 计算灰度共生矩阵
glcm = cv2.calcGLCM(na_images[i], [1], 0, 256, symmetric=True)
# 计算纹理特征
contrast = cv2.compareHist(cv2.normalize(glcm[:,:,0,0], None, norm_type=cv2.NORM_L1),
cv2.normalize(glcm[:,:,0,1], None, norm_type=cv2.NORM_L1),
cv2.HISTCMP_CHISQR_ALT)
homogeneity = cv2.compareHist(cv2.normalize(glcm[:,:,0,0], None, norm_type=cv2.NORM_L1),
cv2.normalize(glcm[:,:,0,1], None, norm_type=cv2.NORM_L1),
cv2.HISTCMP_INTERSECT)
na_texture_hist.append([contrast, homogeneity])
# 计算形态特征
_, contours, hierarchy = cv2.findContours(na_images[i], cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if len(contours)>0:
cnt = contours[0]
area = cv2.contourArea(cnt)
perimeter = cv2.arcLength(cnt,True)
na_shape_hist.append([perimeter/area, area/(256*256)])
# 将概率分布转换为密度函数
us_texture_pdf = stats.gaussian_kde(np.array(us_texture_hist).T)
us_shape_pdf = stats.gaussian_kde(np.array(us_shape_hist).T)
na_texture_pdf = stats.gaussian_kde(np.array(na_texture_hist).T)
na_shape_pdf = stats.gaussian_kde(np.array(na_shape_hist).T)
```
最后,可以可视化比较两种图像的纹理和形态特征的概率分布。这里,我们使用直方图来表示概率分布的密度函数:
``` python
# 可视化比较两种图像的纹理和形态特征的概率分布
fig, axs = plt.subplots(2, 2, figsize=(10, 10))
fig.suptitle('Texture and Shape Feature Analysis')
axs[0, 0].hist(np.array(us_texture_hist)[:,0], bins=20, alpha=0.5, density=True, label='US')
axs[0, 0].hist(np.array(na_texture_hist)[:,0], bins=20, alpha=0.5, density=True, label='Natural')
axs[0, 0].set_xlabel('Contrast')
axs[0, 0].set_ylabel('Density')
axs[0, 0].legend()
axs[0, 1].hist(np.array(us_texture_hist)[:,1], bins=20, alpha=0.5, density=True, label='US')
axs[0, 1].hist(np.array(na_texture_hist)[:,1], bins=20, alpha=0.5, density=True, label='Natural')
axs[0, 1].set_xlabel('Homogeneity')
axs[0, 1].set_ylabel('Density')
axs[0, 1].legend()
axs[1, 0].hist(np.array(us_shape_hist)[:,0], bins=20, alpha=0.5, density=True, label='US')
axs[1, 0].hist(np.array(na_shape_hist)[:,0], bins=20, alpha=0.5, density=True, label='Natural')
axs[1, 0].set_xlabel('Perimeter/Area')
axs[1, 0].set_ylabel('Density')
axs[1, 0].legend()
axs[1, 1].hist(np.array(us_shape_hist)[:,1], bins=20, alpha=0.5, density=True, label='US')
axs[1, 1].hist(np.array(na_shape_hist)[:,1], bins=20, alpha=0.5, density=True, label='Natural')
axs[1, 1].set_xlabel('Area/Total Area')
axs[1, 1].set_ylabel('Density')
axs[1, 1].legend()
plt.show()
```
这样就可以得到可视化的结果,可以通过比较概率分布的密度函数来判断两种图像的纹理和形态特征的差异。