如何用python实现基于概率分布的超声图像与自然图像性质差异分析这一课题,两种图像的文件类型都是jpg,超声图像的文件路径是‘D:\zzz\zus2’,自然图像的文件路径是‘D:\zzz\zna2’,两种图像都有84张,需要对比两种图像的特征必须有颜色,纹理,形状,差异性分析方法也需要多种,包括完整详细复杂代码,不使用greycomatrix, greycoprops
时间: 2023-11-27 20:51:18 浏览: 68
用python实现对比两张图片的不同
5星 · 资源好评率100%
该问题需要使用一些图像处理和机器学习的知识来解决,以下是实现该问题的步骤:
1. 导入必要的库
```python
import os
import numpy as np
import cv2
from skimage.feature import hog
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from scipy.stats import skew, kurtosis
```
2. 定义计算特征的函数
```python
def get_color_feature(img):
# 将图像转换为HSV色彩空间
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# 分离通道
h, s, v = cv2.split(hsv)
# 计算颜色特征
mean_h = np.mean(h)
mean_s = np.mean(s)
mean_v = np.mean(v)
std_h = np.std(h)
std_s = np.std(s)
std_v = np.std(v)
skew_h = skew(h.reshape(-1))
skew_s = skew(s.reshape(-1))
skew_v = skew(v.reshape(-1))
kurtosis_h = kurtosis(h.reshape(-1))
kurtosis_s = kurtosis(s.reshape(-1))
kurtosis_v = kurtosis(v.reshape(-1))
return [mean_h, mean_s, mean_v, std_h, std_s, std_v, skew_h, skew_s, skew_v, kurtosis_h, kurtosis_s, kurtosis_v]
def get_texture_feature(img):
# 将图像转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 计算LBP特征
lbp = np.zeros_like(gray)
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, _ = np.histogram(lbp, bins=np.arange(0, 256))
hist = hist.astype("float")
# 计算LBPH特征
lbph = cv2.createLBPHFaceRecognizer()
lbph.train([gray], np.array([1]))
lbph_feature = lbph.getHistogram()[0]
# 计算HOG特征
hog_feature = hog(gray, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2), visualize=False)
return np.concatenate((hist, lbph_feature, hog_feature))
def get_shape_feature(img):
# 提取轮廓
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 100, 200)
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# 计算形状特征
area = cv2.contourArea(contours[0])
perimeter = cv2.arcLength(contours[0], True)
hull = cv2.convexHull(contours[0])
hull_area = cv2.contourArea(hull)
solidity = float(area) / hull_area
return [area, perimeter, solidity]
def get_all_features(img_path):
# 读取图像
img = cv2.imread(img_path)
# 计算颜色特征
color_feature = get_color_feature(img)
# 计算纹理特征
texture_feature = get_texture_feature(img)
# 计算形状特征
shape_feature = get_shape_feature(img)
# 拼接特征向量
feature = np.concatenate((color_feature, texture_feature, shape_feature))
return feature
```
3. 读取图像并计算特征
```python
# 超声图像路径
zus_path = "D:/zzz/zus2"
# 自然图像路径
zna_path = "D:/zzz/zna2"
# 特征矩阵
X = []
# 图像标签
y = []
# 读取超声图像
for filename in os.listdir(zus_path):
if filename.endswith(".jpg"):
img_path = os.path.join(zus_path, filename)
feature = get_all_features(img_path)
X.append(feature)
y.append(0) # 0表示超声图像
# 读取自然图像
for filename in os.listdir(zna_path):
if filename.endswith(".jpg"):
img_path = os.path.join(zna_path, filename)
feature = get_all_features(img_path)
X.append(feature)
y.append(1) # 1表示自然图像
# 转换为numpy数组
X = np.array(X)
y = np.array(y)
```
4. 特征标准化和降维
```python
# 标准化特征矩阵
scaler = StandardScaler()
X_std = scaler.fit_transform(X)
# PCA降维
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_std)
```
5. 计算类别间距离和类内距离
```python
# 计算类别中心
centers = []
for i in [0, 1]:
centers.append(np.mean(X_pca[y==i], axis=0))
# 计算类别间距离
between_dist = np.linalg.norm(centers[1] - centers[0])
# 计算类内距离
within_dist = 0
for i in [0, 1]:
within_dist += np.sum(np.square(X_pca[y==i] - centers[i]))
```
6. 计算类别重叠度
```python
# 计算类别1在类别0中的重叠率
overlap_10 = np.sum((X_pca[y==1] >= centers[0]) & (X_pca[y==1] <= centers[1])) / np.sum(y==1)
# 计算类别0在类别1中的重叠率
overlap_01 = np.sum((X_pca[y==0] >= centers[1]) & (X_pca[y==0] <= centers[0])) / np.sum(y==0)
# 计算类别重叠度
overlap = (overlap_10 + overlap_01) / 2
```
7. 输出结果
```python
print("类别间距离:", between_dist)
print("类别内距离:", within_dist)
print("类别重叠度:", overlap)
```
完整的代码如下:
阅读全文