用python实现基于概率分布的超声图像与自然图像性质差异分析,不用greycomatrix和greycoprops,使用opencv,两种图像的文件类型都是jpg,超声图像的文件路径是D:\zzz\us2,自然图像的文件路径是D:\zzz\na2,两种图像都有84张,图片形状都不同,图像名称是随机的,需要将图像归一化,需要对比两种图像的特征必须有颜色,纹理,形状,差异性分析方法也需要多种,包括完整详细最新的代码
时间: 2023-10-03 20:02:18 浏览: 114
基于Python的图像分类
5星 · 资源好评率100%
首,我们需要导入必要的库,包括OpenCV、numpy和matplotlib:
```python
import cv2
import numpy as np
import matplotlib.pyplot as plt
```
接下来,定义一个函数 `normalize_image`,用于将图像归一化到0到1之间的范围内:
```python
def normalize_image(image):
return cv2.normalize(image.astype('float'), None, 0.0, 1.0, cv2.NORM_MINMAX)
```
然后,定义一个函数 `get_image_features`,用于提取图像的特征。在这个函数中,我们将使用OpenCV的SIFT算法提取图像的颜色和纹理特征,使用轮廓检测提取图像的形状特征。此外,我们还将使用灰度共生矩阵(GLCM)计算图像的纹理特征。
```python
def get_image_features(image):
# 提取颜色和纹理特征
sift = cv2.SIFT_create()
kp, des = sift.detectAndCompute(image, None)
color_feature = np.mean(des, axis=0)
# 提取形状特征
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
area = cv2.contourArea(contours[0])
perimeter = cv2.arcLength(contours[0], True)
shape_feature = np.array([area, perimeter])
# 计算灰度共生矩阵
glcm = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
glcm = (glcm * 255).astype(np.uint8)
glcm = cv2.resize(glcm, (64, 64))
glcm = normalize_image(glcm)
glcm = (glcm * 255).astype(np.uint8)
glcm = cv2.cvtColor(glcm, cv2.COLOR_GRAY2BGR)
glcm = cv2.cvtColor(glcm, cv2.COLOR_BGR2GRAY)
glcm = glcm.astype(np.uint8)
glcm = cv2.cvtColor(glcm, cv2.COLOR_GRAY2BGR)
glcm = cv2.cvtColor(glcm, cv2.COLOR_BGR2GRAY)
glcm = cv2.GaussianBlur(glcm, (3, 3), 0)
glcm = cv2.normalize(glcm.astype('float'), None, 0.0, 255.0, cv2.NORM_MINMAX).astype(np.uint8)
glcm = cv2.cvtColor(glcm, cv2.COLOR_GRAY2BGR)
glcm = cv2.cvtColor(glcm, cv2.COLOR_BGR2GRAY)
glcm = glcm.astype(np.uint8)
glcm_feature = np.zeros(4)
for i in range(4):
glcm_matrix = cv2.calcGLCM(glcm, [5], [i], symmetric=False, normed=True)
glcm_feature[i] = cv2.compareHist(glcm_matrix, glcm_matrix, cv2.HISTCMP_CORREL)
# 返回特征向量
return np.concatenate((color_feature, shape_feature, glcm_feature))
```
在主函数中,我们将遍历两个文件夹中的所有图像,提取它们的特征,并计算它们之间的差异。这里我们使用了欧氏距离和余弦相似度两种度量方法。
```python
if __name__ == '__main__':
# 遍历超声图像文件夹
us_folder = 'D:/zzz/us2/'
us_features = []
for i in range(1, 85):
filename = us_folder + str(i) + '.jpg'
image = cv2.imread(filename)
image = cv2.resize(image, (256, 256))
feature = get_image_features(image)
us_features.append(feature)
# 遍历自然图像文件夹
na_folder = 'D:/zzz/na2/'
na_features = []
for i in range(1, 85):
filename = na_folder + str(i) + '.jpg'
image = cv2.imread(filename)
image = cv2.resize(image, (256, 256))
feature = get_image_features(image)
na_features.append(feature)
# 计算差异性分析
us_features = np.array(us_features)
na_features = np.array(na_features)
color_diff = np.linalg.norm(us_features[:, :128] - na_features[:, :128], axis=1)
shape_diff = np.linalg.norm(us_features[:, 128:130] - na_features[:, 128:130], axis=1)
glcm_diff = np.linalg.norm(us_features[:, 130:] - na_features[:, 130:], axis=1)
total_diff = color_diff + shape_diff + glcm_diff
cos_sim = np.dot(us_features, na_features.T) / (np.linalg.norm(us_features, axis=1)[:, None] * np.linalg.norm(na_features, axis=1)[None, :])
cos_sim = np.mean(cos_sim, axis=0)
# 输出结果
print('Color feature difference:')
print('Mean:', np.mean(color_diff))
print('Std:', np.std(color_diff))
print('Max:', np.max(color_diff))
print('Min:', np.min(color_diff))
print('Shape feature difference:')
print('Mean:', np.mean(shape_diff))
print('Std:', np.std(shape_diff))
print('Max:', np.max(shape_diff))
print('Min:', np.min(shape_diff))
print('GLCM feature difference:')
print('Mean:', np.mean(glcm_diff))
print('Std:', np.std(glcm_diff))
print('Max:', np.max(glcm_diff))
print('Min:', np.min(glcm_diff))
print('Total feature difference:')
print('Mean:', np.mean(total_diff))
print('Std:', np.std(total_diff))
print('Max:', np.max(total_diff))
print('Min:', np.min(total_diff))
print('Cosine similarity:')
print('Mean:', np.mean(cos_sim))
print('Std:', np.std(cos_sim))
print('Max:', np.max(cos_sim))
print('Min:', np.min(cos_sim))
```
完整代码如下:
阅读全文