用python实现基于概率分布的超声图像与自然图像性质差异分析这一课题,使用opencv, 两种图像的文件类型都是jpg,超声图像的文件路径是‘D:\zzz\us5’,自然图像的文件路径是‘D:\zzz\na4’,两种图像都有84张,图像的名称都是1到84的顺序数,两种图像的形状大小相同,需要对比两种图像的特征必须有颜色,纹理,形状,差异性分析方法也需要多种,包括完整详细代码
时间: 2023-11-16 16:04:45 浏览: 64
以下是基于概率分布的超声图像与自然图像性质差异分析的Python代码,使用了OpenCV库:
```python
import cv2
import numpy as np
import matplotlib.pyplot as plt
# 均值和标准差
def mean_std(img):
mean = np.mean(img)
std = np.std(img)
return mean, std
# 颜色直方图
def color_histogram(img):
hist = cv2.calcHist([img], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256])
return hist.flatten()
# 灰度共生矩阵
def glcm(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
glcm = cv2.calcGLCM(gray, [5], 0, 256)
return glcm.flatten()
# 轮廓特征
def contour_features(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, 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_images = [cv2.imread(us_path + str(i) + ".jpg") for i in range(1, 85)]
na_images = [cv2.imread(na_path + str(i) + ".jpg") for i in range(1, 85)]
# 计算均值和标准差
us_means, us_stds = [], []
na_means, na_stds = [], []
for i in range(84):
us_mean, us_std = mean_std(us_images[i])
us_means.append(us_mean)
us_stds.append(us_std)
na_mean, na_std = mean_std(na_images[i])
na_means.append(na_mean)
na_stds.append(na_std)
# 计算颜色直方图
us_hists, na_hists = [], []
for i in range(84):
us_hist = color_histogram(us_images[i])
us_hists.append(us_hist)
na_hist = color_histogram(na_images[i])
na_hists.append(na_hist)
# 计算灰度共生矩阵
us_glcm, na_glcm = [], []
for i in range(84):
us_glcm.append(glcm(us_images[i]))
na_glcm.append(glcm(na_images[i]))
# 计算轮廓特征
us_contour_area, us_contour_perimeter = [], []
na_contour_area, na_contour_perimeter = [], []
for i in range(84):
us_area, us_perimeter = contour_features(us_images[i])
us_contour_area.append(us_area)
us_contour_perimeter.append(us_perimeter)
na_area, na_perimeter = contour_features(na_images[i])
na_contour_area.append(na_area)
na_contour_perimeter.append(na_perimeter)
# 绘制均值和标准差直方图
plt.hist(us_means, alpha=0.5, label='Ultrasound')
plt.hist(na_means, alpha=0.5, label='Natural')
plt.legend(loc='upper right')
plt.xlabel('Means')
plt.ylabel('Frequency')
plt.title('Means Distribution')
plt.show()
plt.hist(us_stds, alpha=0.5, label='Ultrasound')
plt.hist(na_stds, alpha=0.5, label='Natural')
plt.legend(loc='upper right')
plt.xlabel('Stds')
plt.ylabel('Frequency')
plt.title('Stds Distribution')
plt.show()
# 绘制颜色直方图
us_hist = us_hists[0]
na_hist = na_hists[0]
for i in range(1, 84):
us_hist = np.vstack((us_hist, us_hists[i]))
na_hist = np.vstack((na_hist, na_hists[i]))
plt.figure(figsize=(10, 5))
plt.subplot(121)
plt.imshow(us_hist.T, interpolation='nearest', cmap='gray')
plt.title('Ultrasound Color Histogram')
plt.xlabel('Bins')
plt.ylabel('Channels')
plt.subplot(122)
plt.imshow(na_hist.T, interpolation='nearest', cmap='gray')
plt.title('Natural Color Histogram')
plt.xlabel('Bins')
plt.ylabel('Channels')
plt.show()
# 绘制灰度共生矩阵
us_glcms = np.array(us_glcm)
na_glcms = np.array(na_glcm)
us_glcms = us_glcms.reshape((84, 256, 256))
na_glcms = na_glcms.reshape((84, 256, 256))
plt.figure(figsize=(10, 5))
plt.subplot(121)
plt.imshow(us_glcms[0], cmap='gray')
plt.title('Ultrasound Gray-Level Co-Occurrence Matrix')
plt.xlabel('X')
plt.ylabel('Y')
plt.subplot(122)
plt.imshow(na_glcms[0], cmap='gray')
plt.title('Natural Gray-Level Co-Occurrence Matrix')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
# 绘制轮廓特征
plt.figure(figsize=(10, 5))
plt.subplot(121)
plt.scatter(us_contour_area, us_contour_perimeter)
plt.title('Ultrasound Contour Features')
plt.xlabel('Area')
plt.ylabel('Perimeter')
plt.subplot(122)
plt.scatter(na_contour_area, na_contour_perimeter)
plt.title('Natural Contour Features')
plt.xlabel('Area')
plt.ylabel('Perimeter')
plt.show()
```
这个代码会分别计算超声图像和自然图像的颜色直方图、灰度共生矩阵、轮廓特征等信息,并绘制相关的统计图表,以便进行两种图像性质差异的分析。
阅读全文