用python实现基于概率分布的超声图像与自然图像性质差异分析这一课题,不用greycomatrix和greycoprops,使用opencv,不用两种图像的文件类型都是jpg,超声图像的文件路径是‘D:/zzz/us2’,自然图像的文件路径是‘D:/zzz/na2’,两种图像都有84张,图片形状都不同,图像名称是随机的,需要对比两种图像的特征必须有颜色,纹理,形状,差异性分析方法也需要多种,包括完整详细代码
时间: 2023-10-06 08:11:38 浏览: 95
首先,我们需要使用OpenCV库读取所有的图像。然后,可以使用ORB算法来提取每个图像的特征点和描述符。接下来,对于每个图像,我们可以使用k-means聚类算法来对描述符进行聚类,以获得代表其颜色和纹理的视觉单词。
有了这些视觉单词,我们可以计算每个图像的颜色和纹理直方图,并使用卡方检验来比较超声图像和自然图像之间的差异。最后,我们可以使用形状描述符(如Hu矩)来计算每个图像的形状特征,并使用欧几里得距离来比较超声图像和自然图像之间的形状差异。
以下是完整的代码:
```python
import cv2
import os
import numpy as np
from scipy.spatial.distance import euclidean
from sklearn.cluster import KMeans
from sklearn.metrics import pairwise_distances_argmin_min
from scipy.stats import chi2_contingency
from math import sqrt
# 读取所有图像并提取ORB特征
def extract_features(image_paths):
# 初始化ORB检测器
orb = cv2.ORB_create()
# 提取每个图像的ORB描述符
descriptors = []
for image_path in image_paths:
image = cv2.imread(image_path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, descriptor = orb.detectAndCompute(gray, None)
if descriptor is not None:
descriptors.append(descriptor)
# 将所有描述符堆叠在一起
descriptors = np.vstack(descriptors)
# 使用k-means聚类算法获得视觉单词
kmeans = KMeans(n_clusters=50)
kmeans.fit(descriptors)
visual_words = kmeans.cluster_centers_
# 计算每个图像的视觉单词直方图
histograms = []
for image_path in image_paths:
image = cv2.imread(image_path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, descriptor = orb.detectAndCompute(gray, None)
if descriptor is not None:
words, _ = pairwise_distances_argmin_min(kmeans.cluster_centers_, descriptor)
histogram, _ = np.histogram(words, bins=len(visual_words))
histograms.append(histogram)
return np.array(histograms)
# 计算颜色直方图
def color_histogram(image):
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
hist = cv2.calcHist([hsv], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256])
hist = cv2.normalize(hist, hist).flatten()
return hist
# 计算纹理直方图
def texture_histogram(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
lbp = np.zeros(gray.shape, dtype=np.uint8)
for i in range(1, gray.shape[0]-1):
for j in range(1, gray.shape[1]-1):
center = gray[i, j]
code = 0
code |= (gray[i-1, j-1] >= center) << 7
code |= (gray[i-1, j] >= center) << 6
code |= (gray[i-1, j+1] >= center) << 5
code |= (gray[i, j+1] >= center) << 4
code |= (gray[i+1, j+1] >= center) << 3
code |= (gray[i+1, j] >= center) << 2
code |= (gray[i+1, j-1] >= center) << 1
code |= (gray[i, j-1] >= center) << 0
lbp[i, j] = code
hist = cv2.calcHist([lbp], [0], None, [256], [0, 256])
hist = cv2.normalize(hist, hist).flatten()
return hist
# 计算Hu矩
def hu_moments(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
moments = cv2.moments(gray)
hu_moments = cv2.HuMoments(moments)
hu_moments = -np.sign(hu_moments) * np.log10(np.abs(hu_moments))
return hu_moments.flatten()
# 计算欧几里得距离
def euclidean_distance(x, y):
return euclidean(x, y)
# 计算卡方检验
def chi_squared_distance(x, y):
chi2, _ = chi2_contingency([x, y])
return sqrt(chi2)
# 加载所有图像
us_image_paths = [os.path.join('D:/zzz/us2', filename) for filename in os.listdir('D:/zzz/us2')]
na_image_paths = [os.path.join('D:/zzz/na2', filename) for filename in os.listdir('D:/zzz/na2')]
# 提取特征
us_features = extract_features(us_image_paths)
na_features = extract_features(na_image_paths)
# 计算颜色直方图和纹理直方图
us_color_histograms = []
us_texture_histograms = []
for us_image_path in us_image_paths:
us_image = cv2.imread(us_image_path)
us_color_histogram = color_histogram(us_image)
us_texture_histogram = texture_histogram(us_image)
us_color_histograms.append(us_color_histogram)
us_texture_histograms.append(us_texture_histogram)
na_color_histograms = []
na_texture_histograms = []
for na_image_path in na_image_paths:
na_image = cv2.imread(na_image_path)
na_color_histogram = color_histogram(na_image)
na_texture_histogram = texture_histogram(na_image)
na_color_histograms.append(na_color_histogram)
na_texture_histograms.append(na_texture_histogram)
# 计算形状特征
us_hu_moments = []
for us_image_path in us_image_paths:
us_image = cv2.imread(us_image_path)
us_hu_moment = hu_moments(us_image)
us_hu_moments.append(us_hu_moment)
na_hu_moments = []
for na_image_path in na_image_paths:
na_image = cv2.imread(na_image_path)
na_hu_moment = hu_moments(na_image)
na_hu_moments.append(na_hu_moment)
# 计算所有图像之间的差异
color_histogram_distances = np.zeros((len(us_image_paths), len(na_image_paths)))
texture_histogram_distances = np.zeros((len(us_image_paths), len(na_image_paths)))
hu_moment_distances = np.zeros((len(us_image_paths), len(na_image_paths)))
for i in range(len(us_image_paths)):
for j in range(len(na_image_paths)):
color_histogram_distances[i, j] = chi_squared_distance(us_color_histograms[i], na_color_histograms[j])
texture_histogram_distances[i, j] = chi_squared_distance(us_texture_histograms[i], na_texture_histograms[j])
hu_moment_distances[i, j] = euclidean_distance(us_hu_moments[i], na_hu_moments[j])
# 打印结果
print('Color histogram differences:')
print(color_histogram_distances)
print('Texture histogram differences:')
print(texture_histogram_distances)
print('Hu moment differences:')
print(hu_moment_distances)
```
这个代码将输出颜色直方图,纹理直方图和形状差异的矩阵。您可以使用这些矩阵来比较超声图像和自然图像之间的差异。
阅读全文