python 读取两个文件夹中的图像并进行性质差异分析,一个文件路径是D:\zzz\us2,其中是超声图像,另一个文件路径是D:\zzz\na2,其中是自然图像,不用greycomatrix和greycoprops,使用opencv,两种图像的文件类型都是jpg,两种图像都有84张,图片形状不都相同,图像名称是1到84的顺序数,需要将图像归一化,需要对比两种图像的特征必须有颜色,纹理,形状,差异性分析方法需要用到概率分布,最终要实现基于概率分布的超声图像与自然图像性质差异分析,包括完整详细最新的代码
时间: 2023-09-19 10:09:03 浏览: 91
Python导入父文件夹中模块并读取当前文件夹内的资源
以下是一个实现你所需功能的代码示例。其中,我们使用了OpenCV库来读取图像,使用了numpy库来进行图像的归一化和特征提取,使用了matplotlib库来进行图像的展示和概率分布的可视化。
```python
import cv2
import numpy as np
import matplotlib.pyplot as plt
# 读取超声图像
us2_path = 'D:/zzz/us2/'
us2_images = []
for i in range(1, 85):
image_path = us2_path + str(i) + '.jpg'
image = cv2.imread(image_path)
# 将图像缩放到统一大小
image = cv2.resize(image, (256, 256))
# 将图像归一化到[0, 1]范围内
image = cv2.normalize(image.astype('float'), None, 0.0, 1.0, cv2.NORM_MINMAX)
us2_images.append(image)
# 读取自然图像
na2_path = 'D:/zzz/na2/'
na2_images = []
for i in range(1, 85):
image_path = na2_path + str(i) + '.jpg'
image = cv2.imread(image_path)
# 将图像缩放到统一大小
image = cv2.resize(image, (256, 256))
# 将图像归一化到[0, 1]范围内
image = cv2.normalize(image.astype('float'), None, 0.0, 1.0, cv2.NORM_MINMAX)
na2_images.append(image)
# 计算颜色特征
us2_color_hist = []
for image in us2_images:
# 将图像从BGR格式转换为HSV格式
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# 计算直方图
hist = cv2.calcHist([hsv_image], [0, 1], None, [180, 256], [0, 180, 0, 256])
# 归一化直方图
hist = cv2.normalize(hist, None, 0.0, 1.0, cv2.NORM_MINMAX)
us2_color_hist.append(hist.flatten())
na2_color_hist = []
for image in na2_images:
# 将图像从BGR格式转换为HSV格式
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# 计算直方图
hist = cv2.calcHist([hsv_image], [0, 1], None, [180, 256], [0, 180, 0, 256])
# 归一化直方图
hist = cv2.normalize(hist, None, 0.0, 1.0, cv2.NORM_MINMAX)
na2_color_hist.append(hist.flatten())
# 计算纹理特征
us2_texture_hist = []
for image in us2_images:
# 将图像从BGR格式转换为灰度格式
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 计算LBP特征
lbp_image = np.uint8(
np.round(cv2.filter2D(gray_image, -1, np.array([[1, 2, 4], [8, 0, 16], [32, 64, 128]]) / 256)))
hist = cv2.calcHist([lbp_image], [0], None, [256], [0, 256])
# 归一化直方图
hist = cv2.normalize(hist, None, 0.0, 1.0, cv2.NORM_MINMAX)
us2_texture_hist.append(hist.flatten())
na2_texture_hist = []
for image in na2_images:
# 将图像从BGR格式转换为灰度格式
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 计算LBP特征
lbp_image = np.uint8(
np.round(cv2.filter2D(gray_image, -1, np.array([[1, 2, 4], [8, 0, 16], [32, 64, 128]]) / 256)))
hist = cv2.calcHist([lbp_image], [0], None, [256], [0, 256])
# 归一化直方图
hist = cv2.normalize(hist, None, 0.0, 1.0, cv2.NORM_MINMAX)
na2_texture_hist.append(hist.flatten())
# 计算形状特征
us2_shape_hist = []
for image in us2_images:
# 将图像从BGR格式转换为灰度格式
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 计算轮廓
_, contours, _ = cv2.findContours(gray_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 计算轮廓面积
areas = [cv2.contourArea(c) for c in contours]
# 计算轮廓面积直方图
hist = cv2.calcHist([np.array(areas)], [0], None, [256], [0, 256])
# 归一化直方图
hist = cv2.normalize(hist, None, 0.0, 1.0, cv2.NORM_MINMAX)
us2_shape_hist.append(hist.flatten())
na2_shape_hist = []
for image in na2_images:
# 将图像从BGR格式转换为灰度格式
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 计算轮廓
_, contours, _ = cv2.findContours(gray_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 计算轮廓面积
areas = [cv2.contourArea(c) for c in contours]
# 计算轮廓面积直方图
hist = cv2.calcHist([np.array(areas)], [0], None, [256], [0, 256])
# 归一化直方图
hist = cv2.normalize(hist, None, 0.0, 1.0, cv2.NORM_MINMAX)
na2_shape_hist.append(hist.flatten())
# 计算差异性分析方法
# 计算颜色特征的概率分布
us2_color_hist = np.array(us2_color_hist)
us2_color_prob = np.sum(us2_color_hist, axis=0) / len(us2_color_hist)
na2_color_hist = np.array(na2_color_hist)
na2_color_prob = np.sum(na2_color_hist, axis=0) / len(na2_color_hist)
# 计算颜色特征的Kullback-Leibler散度
color_divergence = cv2.compareHist(us2_color_prob, na2_color_prob, cv2.HISTCMP_KL_DIV)
# 计算纹理特征的概率分布
us2_texture_hist = np.array(us2_texture_hist)
us2_texture_prob = np.sum(us2_texture_hist, axis=0) / len(us2_texture_hist)
na2_texture_hist = np.array(na2_texture_hist)
na2_texture_prob = np.sum(na2_texture_hist, axis=0) / len(na2_texture_hist)
# 计算纹理特征的Kullback-Leibler散度
texture_divergence = cv2.compareHist(us2_texture_prob, na2_texture_prob, cv2.HISTCMP_KL_DIV)
# 计算形状特征的概率分布
us2_shape_hist = np.array(us2_shape_hist)
us2_shape_prob = np.sum(us2_shape_hist, axis=0) / len(us2_shape_hist)
na2_shape_hist = np.array(na2_shape_hist)
na2_shape_prob = np.sum(na2_shape_hist, axis=0) / len(na2_shape_hist)
# 计算形状特征的Kullback-Leibler散度
shape_divergence = cv2.compareHist(us2_shape_prob, na2_shape_prob, cv2.HISTCMP_KL_DIV)
# 展示结果
print('颜色特征的差异性分析结果:', color_divergence)
print('纹理特征的差异性分析结果:', texture_divergence)
print('形状特征的差异性分析结果:', shape_divergence)
# 可视化概率分布
plt.figure(figsize=(12, 4))
plt.subplot(131)
plt.plot(us2_color_prob, color='red', label='us2')
plt.plot(na2_color_prob, color='blue', label='na2')
plt.title('Color Distribution')
plt.legend()
plt.subplot(132)
plt.plot(us2_texture_prob, color='red', label='us2')
plt.plot(na2_texture_prob, color='blue', label='na2')
plt.title('Texture Distribution')
plt.legend()
plt.subplot(133)
plt.plot(us2_shape_prob, color='red', label='us2')
plt.plot(na2_shape_prob, color='blue', label='na2')
plt.title('Shape Distribution')
plt.legend()
plt.show()
```
上述代码中,我们使用了OpenCV库中的calcHist函数来计算图像的直方图,并使用normalize函数将直方图归一化到[0, 1]范围内。对于颜色特征,我们将图像从BGR格式转换为HSV格式,并计算H和S通道的二维直方图;对于纹理特征,我们使用了LBP算法;对于形状特征,我们使用了轮廓面积直方图。在计算完各个特征的直方图后,我们将它们的概率分布计算出来,并使用compareHist函数计算它们的Kullback-Leibler散度,作为差异性分析的结果。最后,我们使用matplotlib库将各个特征的概率分布可视化出来,以便更好地理解分析结果。
阅读全文