用python实现基于概率分布的超声图像与自然图像性质差异分析这一课题, 两种图像的文件类型都是jpg,超声图像的文件路径是‘D:\zzz\us5’,自然图像的文件路径是‘D:\zzz\na4’,两种图像都有84张,图像的名称都是1到84的顺序数,两种图像的形状大小相同,需要对比两种图像的特征必须有颜色,纹理,形状,差异性分析方法也需要多种,包括完整详细代码
时间: 2023-11-11 07:04:54 浏览: 124
首先,我们需要导入所需的库:numpy、opencv-python、os、matplotlib、skimage、scipy。
```python
import numpy as np
import cv2
import os
from matplotlib import pyplot as plt
from skimage.feature import greycomatrix, greycoprops
from scipy.stats import describe
```
然后,我们需要定义一个函数,用于计算图像的纹理特征。这里我们使用灰度共生矩阵(GLCM)来计算纹理特征。
```python
def calc_texture_feature(image):
# 将图像转换为灰度图像
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 计算灰度共生矩阵
glcm = greycomatrix(gray, [1], [0, np.pi/4, np.pi/2, 3*np.pi/4], levels=256, symmetric=True, normed=True)
# 计算纹理特征
contrast = greycoprops(glcm, 'contrast').mean()
dissimilarity = greycoprops(glcm, 'dissimilarity').mean()
homogeneity = greycoprops(glcm, 'homogeneity').mean()
energy = greycoprops(glcm, 'energy').mean()
correlation = greycoprops(glcm, 'correlation').mean()
return contrast, dissimilarity, homogeneity, energy, correlation
```
接下来,我们需要定义一个函数,用于计算图像的颜色特征。这里我们使用直方图来计算颜色特征。
```python
def calc_color_feature(image):
# 将图像转换为HSV颜色空间
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# 计算直方图
hist = cv2.calcHist([hsv], [0, 1], None, [180, 256], [0, 180, 0, 256])
# 归一化直方图
cv2.normalize(hist, hist, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX)
# 计算颜色特征
mean_hue = np.mean(hsv[:, :, 0])
mean_saturation = np.mean(hsv[:, :, 1])
mean_value = np.mean(hsv[:, :, 2])
std_hue = np.std(hsv[:, :, 0])
std_saturation = np.std(hsv[:, :, 1])
std_value = np.std(hsv[:, :, 2])
return hist, mean_hue, mean_saturation, mean_value, std_hue, std_saturation, std_value
```
然后,我们需要定义一个函数,用于计算图像的形状特征。这里我们使用轮廓来计算形状特征。
```python
def calc_shape_feature(image):
# 将图像转换为灰度图像
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 二值化图像
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 查找轮廓
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 计算形状特征
area = cv2.contourArea(contours[0])
perimeter = cv2.arcLength(contours[0], True)
circularity = (4 * np.pi * area) / (perimeter ** 2)
return area, perimeter, circularity
```
接下来,我们需要定义一个函数,用于计算两张图像之间的差异性。这里我们使用欧几里得距离来计算差异性。
```python
def calc_difference(image1, image2):
# 计算颜色特征
hist1, mean_hue1, mean_saturation1, mean_value1, std_hue1, std_saturation1, std_value1 = calc_color_feature(image1)
hist2, mean_hue2, mean_saturation2, mean_value2, std_hue2, std_saturation2, std_value2 = calc_color_feature(image2)
color_distance = np.sqrt((mean_hue1 - mean_hue2) ** 2 + (mean_saturation1 - mean_saturation2) ** 2 + (mean_value1 - mean_value2) ** 2 + (std_hue1 - std_hue2) ** 2 + (std_saturation1 - std_saturation2) ** 2 + (std_value1 - std_value2) ** 2)
# 计算纹理特征
contrast1, dissimilarity1, homogeneity1, energy1, correlation1 = calc_texture_feature(image1)
contrast2, dissimilarity2, homogeneity2, energy2, correlation2 = calc_texture_feature(image2)
texture_distance = np.sqrt((contrast1 - contrast2) ** 2 + (dissimilarity1 - dissimilarity2) ** 2 + (homogeneity1 - homogeneity2) ** 2 + (energy1 - energy2) ** 2 + (correlation1 - correlation2) ** 2)
# 计算形状特征
area1, perimeter1, circularity1 = calc_shape_feature(image1)
area2, perimeter2, circularity2 = calc_shape_feature(image2)
shape_distance = np.sqrt((area1 - area2) ** 2 + (perimeter1 - perimeter2) ** 2 + (circularity1 - circularity2) ** 2)
# 计算差异性
difference = np.sqrt((color_distance ** 2) + (texture_distance ** 2) + (shape_distance ** 2))
return difference
```
最后,我们需要遍历两个文件夹中的所有图像,并计算它们之间的差异性。我们还可以选择将差异性保存到一个CSV文件中,以便进一步分析。
```python
# 设置文件夹路径和图像数量
us_path = 'D:/zzz/us5'
na_path = 'D:/zzz/na4'
num_images = 84
# 创建CSV文件
csv_file = open('difference.csv', 'w')
csv_file.write('Image1,Image2,Color distance,Texture distance,Shape distance,Difference\n')
# 遍历两个文件夹中的所有图像
for i in range(1, num_images + 1):
for j in range(i + 1, num_images + 1):
# 读取图像
us_image1 = cv2.imread(os.path.join(us_path, str(i) + '.jpg'))
us_image2 = cv2.imread(os.path.join(us_path, str(j) + '.jpg'))
na_image1 = cv2.imread(os.path.join(na_path, str(i) + '.jpg'))
na_image2 = cv2.imread(os.path.join(na_path, str(j) + '.jpg'))
# 计算差异性
us_difference = calc_difference(us_image1, us_image2)
na_difference = calc_difference(na_image1, na_image2)
# 将差异性保存到CSV文件中
csv_file.write(str(i) + '_' + str(j) + ',US,' + str(us_difference) + '\n')
csv_file.write(str(i) + '_' + str(j) + ',NA,' + str(na_difference) + '\n')
# 关闭CSV文件
csv_file.close()
```
完整代码:
```python
import numpy as np
import cv2
import os
from matplotlib import pyplot as plt
from skimage.feature import greycomatrix, greycoprops
from scipy.stats import describe
def calc_texture_feature(image):
# 将图像转换为灰度图像
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 计算灰度共生矩阵
glcm = greycomatrix(gray, [1], [0, np.pi/4, np.pi/2, 3*np.pi/4], levels=256, symmetric=True, normed=True)
# 计算纹理特征
contrast = greycoprops(glcm, 'contrast').mean()
dissimilarity = greycoprops(glcm, 'dissimilarity').mean()
homogeneity = greycoprops(glcm, 'homogeneity').mean()
energy = greycoprops(glcm, 'energy').mean()
correlation = greycoprops(glcm, 'correlation').mean()
return contrast, dissimilarity, homogeneity, energy, correlation
def calc_color_feature(image):
# 将图像转换为HSV颜色空间
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# 计算直方图
hist = cv2.calcHist([hsv], [0, 1], None, [180, 256], [0, 180, 0, 256])
# 归一化直方图
cv2.normalize(hist, hist, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX)
# 计算颜色特征
mean_hue = np.mean(hsv[:, :, 0])
mean_saturation = np.mean(hsv[:, :, 1])
mean_value = np.mean(hsv[:, :, 2])
std_hue = np.std(hsv[:, :, 0])
std_saturation = np.std(hsv[:, :, 1])
std_value = np.std(hsv[:, :, 2])
return hist, mean_hue, mean_saturation, mean_value, std_hue, std_saturation, std_value
def calc_shape_feature(image):
# 将图像转换为灰度图像
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 二值化图像
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 查找轮廓
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 计算形状特征
area = cv2.contourArea(contours[0])
perimeter = cv2.arcLength(contours[0], True)
circularity = (4 * np.pi * area) / (perimeter ** 2)
return area, perimeter, circularity
def calc_difference(image1, image2):
# 计算颜色特征
hist1, mean_hue1, mean_saturation1, mean_value1, std_hue1, std_saturation1, std_value1 = calc_color_feature(image1)
hist2, mean_hue2, mean_saturation2, mean_value2, std_hue2, std_saturation2, std_value2 = calc_color_feature(image2)
color_distance = np.sqrt((mean_hue1 - mean_hue2) ** 2 + (mean_saturation1 - mean_saturation2) ** 2 + (mean_value1 - mean_value2) ** 2 + (std_hue1 - std_hue2) ** 2 + (std_saturation1 - std_saturation2) ** 2 + (std_value1 - std_value2) ** 2)
# 计算纹理特征
contrast1, dissimilarity1, homogeneity1, energy1, correlation1 = calc_texture_feature(image1)
contrast2, dissimilarity2, homogeneity2, energy2, correlation2 = calc_texture_feature(image2)
texture_distance = np.sqrt((contrast1 - contrast2) ** 2 + (dissimilarity1 - dissimilarity2) ** 2 + (homogeneity1 - homogeneity2) ** 2 + (energy1 - energy2) ** 2 + (correlation1 - correlation2) ** 2)
# 计算形状特征
area1, perimeter1, circularity1 = calc_shape_feature(image1)
area2, perimeter2, circularity2 = calc_shape_feature(image2)
shape_distance = np.sqrt((area1 - area2) ** 2 + (perimeter1 - perimeter2) ** 2 + (circularity1 - circularity2) ** 2)
# 计算差异性
difference = np.sqrt((color_distance ** 2) + (texture_distance ** 2) + (shape_distance ** 2))
return difference
# 设置文件夹路径和图像数量
us_path = 'D:/zzz/us5'
na_path = 'D:/zzz/na4'
num_images = 84
# 创建CSV文件
csv_file = open('difference.csv', 'w')
csv_file.write('Image1,Image2,Color distance,Texture distance,Shape distance,Difference\n')
# 遍历两个文件夹中的所有图像
for i in range(1, num_images + 1):
for j in range(i + 1, num_images + 1):
# 读取图像
us_image1 = cv2.imread(os.path.join(us_path, str(i) + '.jpg'))
us_image2 = cv2.imread(os.path.join(us_path, str(j) + '.jpg'))
na_image1 = cv2.imread(os.path.join(na_path, str(i) + '.jpg'))
na_image2 = cv2.imread(os.path.join(na_path, str(j) + '.jpg'))
# 计算差异性
us_difference = calc_difference(us_image1, us_image2)
na_difference = calc_difference(na_image1, na_image2)
# 将差异性保存到CSV文件中
csv_file.write(str(i) + '_' + str(j) + ',US,' + str(us_difference) + '\n')
csv_file.write(str(i) + '_' + str(j) + ',NA,' + str(na_difference) + '\n')
# 关闭CSV文件
csv_file.close()
```
阅读全文