用python实现基于概率分布的超声图像与自然图像性质差异分析这一课题,使用opencv, 两种图像的文件类型都是jpg,超声图像的文件路径是‘D:\zzz\us5’,自然图像的文件路径是‘D:\zzz\na4’,两种图像都有84张,图像的名称都是1到84的顺序数,两种图像的形状大小相同,需要对比两种图像的特征必须有颜色,纹理,形态学特征,差异性分析方法也需要多种,包括完整详细代码
时间: 2023-11-16 22:06:13 浏览: 107
首先,我们需要安装必要的库:
```python
!pip install opencv-python
!pip install scikit-image
!pip install numpy
!pip install matplotlib
```
接下来,我们可以编写代码来比较两种图像的不同特征。
```python
import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage.feature import greycomatrix, greycoprops
# 读取超声图像
us_images = []
for i in range(1, 85):
img_path = f"D:/zzz/us5/{i}.jpg"
img = cv2.imread(img_path)
us_images.append(img)
# 读取自然图像
na_images = []
for i in range(1, 85):
img_path = f"D:/zzz/na4/{i}.jpg"
img = cv2.imread(img_path)
na_images.append(img)
# 计算颜色差异
us_mean_color = []
na_mean_color = []
for i in range(84):
us_mean_color.append(np.mean(us_images[i], axis=(0, 1)))
na_mean_color.append(np.mean(na_images[i], axis=(0, 1)))
us_mean_color = np.array(us_mean_color)
na_mean_color = np.array(na_mean_color)
color_diff = np.abs(us_mean_color - na_mean_color)
plt.figure()
plt.bar(range(len(color_diff)), color_diff[:, 0], label='B', color='b')
plt.bar(range(len(color_diff)), color_diff[:, 1], label='G', color='g')
plt.bar(range(len(color_diff)), color_diff[:, 2], label='R', color='r')
plt.xlabel('Image Index')
plt.ylabel('Color Difference')
plt.title('Color Difference between US and Natural Images')
plt.legend()
plt.show()
# 计算纹理特征差异
us_texture = []
na_texture = []
for i in range(84):
us_gray = cv2.cvtColor(us_images[i], cv2.COLOR_BGR2GRAY)
na_gray = cv2.cvtColor(na_images[i], cv2.COLOR_BGR2GRAY)
us_glcm = greycomatrix(us_gray, [5], [0], 256, symmetric=True, normed=True)
na_glcm = greycomatrix(na_gray, [5], [0], 256, symmetric=True, normed=True)
us_texture.append(greycoprops(us_glcm, 'dissimilarity')[0, 0])
na_texture.append(greycoprops(na_glcm, 'dissimilarity')[0, 0])
us_texture = np.array(us_texture)
na_texture = np.array(na_texture)
texture_diff = np.abs(us_texture - na_texture)
plt.figure()
plt.plot(range(len(texture_diff)), texture_diff)
plt.xlabel('Image Index')
plt.ylabel('Texture Difference')
plt.title('Texture Difference between US and Natural Images')
plt.show()
# 计算形态学特征差异
us_gray = cv2.cvtColor(us_images[0], cv2.COLOR_BGR2GRAY)
na_gray = cv2.cvtColor(na_images[0], cv2.COLOR_BGR2GRAY)
us_blur = cv2.GaussianBlur(us_gray, (5, 5), 0)
na_blur = cv2.GaussianBlur(na_gray, (5, 5), 0)
us_canny = cv2.Canny(us_blur, 100, 200)
na_canny = cv2.Canny(na_blur, 100, 200)
us_contours, _ = cv2.findContours(us_canny, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
na_contours, _ = cv2.findContours(na_canny, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
us_area = []
na_area = []
for contour in us_contours:
us_area.append(cv2.contourArea(contour))
for contour in na_contours:
na_area.append(cv2.contourArea(contour))
us_area = np.array(us_area)
na_area = np.array(na_area)
area_diff = np.abs(us_area - na_area)
plt.figure()
plt.plot(range(len(area_diff)), area_diff)
plt.xlabel('Image Index')
plt.ylabel('Area Difference')
plt.title('Area Difference between US and Natural Images')
plt.show()
```
代码中,我们首先分别读取了超声图像和自然图像,然后计算了颜色、纹理和形态学特征之间的差异。在计算颜色差异时,我们计算了每个图像的平均颜色,并计算了超声图像和自然图像之间的颜色差异。在计算纹理特征差异时,我们使用灰度共生矩阵(GLCM)计算了每个图像的纹理特征,并计算了超声图像和自然图像之间的纹理特征差异。在计算形态学特征差异时,我们首先将图像转换为灰度图像,然后使用高斯模糊和Canny边缘检测算法得到图像的轮廓,并计算了每个图像的轮廓面积,并计算了超声图像和自然图像之间的轮廓面积差异。
最后,我们可以将结果可视化,以便更好地理解两种图像之间的差异。
阅读全文