用python实现基于概率分布的超声图像与自然图像性质差异分析这一课题,使用opencv, 不能用greycomatrix, greycoprops,两种图像的文件类型都是jpg,超声图像的文件路径是‘D:\zzz\us5’,自然图像的文件路径是‘D:\zzz\na4’,两种图像都有84张,图像的名称都是1到84的顺序数,两种图像的形状大小相同,需要对比两种图像的特征必须有颜色,纹理,形态学特征,差异性分析方法也需要多种,包括完整详细代码
时间: 2023-11-16 12:07:52 浏览: 47
利用OpenCV和Python实现查找图片差异
首,我们需要导入必要的库:
```python
import cv2
import numpy as np
import os
from matplotlib import pyplot as plt
```
然后,我们可以定义一些常量和函数来帮助我们进行图像处理和特征提取:
```python
# 图像路径和文件名前缀
us_path = 'D:/zzz/us5/'
na_path = 'D:/zzz/na4/'
prefix = '1'
# 图像大小
img_size = (256, 256)
# 颜色特征提取函数
def color_feature(img):
# 将图像转换为HSV颜色空间
hsv = cv2.cvtColor(img, 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)
return hist.flatten()
# 纹理特征提取函数
def texture_feature(img):
# 将图像转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 计算LBP特征
radius = 3
n_points = 8 * radius
lbp = cv2.spatial_histogram(gray, [n_points, radius], [0, 256], True)
# 归一化特征
cv2.normalize(lbp, lbp, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX)
return lbp.flatten()
# 形态学特征提取函数
def morphology_feature(img):
# 将图像转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 计算轮廓
contours, hierarchy = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 计算面积和周长
area = cv2.contourArea(contours[0])
perimeter = cv2.arcLength(contours[0], True)
return np.array([area, perimeter])
# 差异性分析函数
def diff_analysis(f1, f2):
# 计算欧氏距离
dist = np.linalg.norm(f1 - f2)
# 计算余弦相似度
cos_sim = np.dot(f1, f2) / (np.linalg.norm(f1) * np.linalg.norm(f2))
return dist, cos_sim
```
接下来,我们可以读取图像并提取其特征:
```python
# 存储特征向量的列表
us_color = []
us_texture = []
us_morphology = []
na_color = []
na_texture = []
na_morphology = []
# 读取超声图像并提取特征
for i in range(1, 85):
img_path = os.path.join(us_path, prefix + str(i) + '.jpg')
img = cv2.imread(img_path)
img = cv2.resize(img, img_size)
us_color.append(color_feature(img))
us_texture.append(texture_feature(img))
us_morphology.append(morphology_feature(img))
# 读取自然图像并提取特征
for i in range(1, 85):
img_path = os.path.join(na_path, prefix + str(i) + '.jpg')
img = cv2.imread(img_path)
img = cv2.resize(img, img_size)
na_color.append(color_feature(img))
na_texture.append(texture_feature(img))
na_morphology.append(morphology_feature(img))
```
最后,我们可以使用我们定义的差异性分析函数来比较两种图像的特征:
```python
# 计算颜色特征的差异性
us_color_mean = np.mean(us_color, axis=0)
na_color_mean = np.mean(na_color, axis=0)
color_dist, color_cos_sim = diff_analysis(us_color_mean, na_color_mean)
# 计算纹理特征的差异性
us_texture_mean = np.mean(us_texture, axis=0)
na_texture_mean = np.mean(na_texture, axis=0)
texture_dist, texture_cos_sim = diff_analysis(us_texture_mean, na_texture_mean)
# 计算形态学特征的差异性
us_morphology_mean = np.mean(us_morphology, axis=0)
na_morphology_mean = np.mean(na_morphology, axis=0)
morphology_dist, morphology_cos_sim = diff_analysis(us_morphology_mean, na_morphology_mean)
# 输出结果
print('颜色特征的欧氏距离为:', color_dist)
print('颜色特征的余弦相似度为:', color_cos_sim)
print('纹理特征的欧氏距离为:', texture_dist)
print('纹理特征的余弦相似度为:', texture_cos_sim)
print('形态学特征的欧氏距离为:', morphology_dist)
print('形态学特征的余弦相似度为:', morphology_cos_sim)
```
完整代码如下:
阅读全文