用python实现基于概率分布的超声图像与自然图像性质差异分析这一课题,使用opencv, 两种图像的文件类型都是jpg,超声图像的文件路径是‘D:\zzz\us5’,自然图像的文件路径是‘D:\zzz\na4’,两种图像都有84张,图像的名称都是1到84的顺序数,两种图像的形状大小相同,需要对比两种图像的特征必须有颜色,纹理,形态学特征,差异性分析方法也需要多种,包括完整详细代码
时间: 2023-11-16 16:05:32 浏览: 120
以下是基于概率分布的超声图像与自然图像性质差异分析的Python代码,使用了OpenCV库:
```python
import cv2
import numpy as np
import os
us_path = 'D:/zzz/us5/'
na_path = 'D:/zzz/na4/'
# 读取图像
def read_img(path):
img_list = []
for i in range(1, 85):
img = cv2.imread(path + str(i) + '.jpg')
img_list.append(img)
return img_list
# 提取颜色特征
def color_feature(img_list):
hsv_list = []
for img in img_list:
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
hsv_list.append(hsv)
hue_hist = []
sat_hist = []
val_hist = []
for hsv in hsv_list:
hue_hist.append(cv2.calcHist([hsv], [0], None, [180], [0, 180]))
sat_hist.append(cv2.calcHist([hsv], [1], None, [256], [0, 256]))
val_hist.append(cv2.calcHist([hsv], [2], None, [256], [0, 256]))
hue_hist = np.array(hue_hist)
sat_hist = np.array(sat_hist)
val_hist = np.array(val_hist)
return hue_hist, sat_hist, val_hist
# 提取纹理特征
def texture_feature(img_list):
gray_list = []
for img in img_list:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray_list.append(gray)
glcm_list = []
for gray in gray_list:
glcm = cv2.calcGLCM(gray, [5], 0, 256, symmetric=True)
glcm_list.append(glcm)
contrast = []
dissimilarity = []
homogeneity = []
energy = []
correlation = []
asm = []
for glcm in glcm_list:
contrast.append(cv2.compareHist(glcm[0], np.zeros_like(glcm[0]), cv2.HISTCMP_CHISQR_ALT))
dissimilarity.append(cv2.compareHist(glcm[1], np.zeros_like(glcm[1]), cv2.HISTCMP_CHISQR_ALT))
homogeneity.append(cv2.compareHist(glcm[2], np.zeros_like(glcm[2]), cv2.HISTCMP_CHISQR_ALT))
energy.append(cv2.compareHist(glcm[3], np.zeros_like(glcm[3]), cv2.HISTCMP_CHISQR_ALT))
correlation.append(cv2.compareHist(glcm[4], np.zeros_like(glcm[4]), cv2.HISTCMP_CHISQR_ALT))
asm.append(cv2.compareHist(glcm[5], np.zeros_like(glcm[5]), cv2.HISTCMP_CHISQR_ALT))
contrast = np.array(contrast)
dissimilarity = np.array(dissimilarity)
homogeneity = np.array(homogeneity)
energy = np.array(energy)
correlation = np.array(correlation)
asm = np.array(asm)
return contrast, dissimilarity, homogeneity, energy, correlation, asm
# 提取形态学特征
def morphology_feature(img_list):
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
opening_list = []
closing_list = []
gradient_list = []
tophat_list = []
blackhat_list = []
for img in img_list:
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
gradient = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel)
tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)
blackhat = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel)
opening_list.append(opening)
closing_list.append(closing)
gradient_list.append(gradient)
tophat_list.append(tophat)
blackhat_list.append(blackhat)
opening = np.mean(np.array(opening_list), axis=0)
closing = np.mean(np.array(closing_list), axis=0)
gradient = np.mean(np.array(gradient_list), axis=0)
tophat = np.mean(np.array(tophat_list), axis=0)
blackhat = np.mean(np.array(blackhat_list), axis=0)
return opening, closing, gradient, tophat, blackhat
# 计算两种图像的特征差异
def feature_diff(us_feature, na_feature):
diff = []
for i in range(len(us_feature)):
us_f = us_feature[i]
na_f = na_feature[i]
if len(us_f.shape) == 1:
us_f = us_f.reshape(-1, 1)
na_f = na_f.reshape(-1, 1)
us_p = us_f / np.sum(us_f)
na_p = na_f / np.sum(na_f)
kl = np.sum(us_p * np.log2(us_p / na_p))
diff.append(kl)
return np.array(diff)
# 提取超声图像和自然图像的特征
us_img_list = read_img(us_path)
na_img_list = read_img(na_path)
hue_hist_us, sat_hist_us, val_hist_us = color_feature(us_img_list)
hue_hist_na, sat_hist_na, val_hist_na = color_feature(na_img_list)
contrast_us, dissimilarity_us, homogeneity_us, energy_us, correlation_us, asm_us = texture_feature(us_img_list)
contrast_na, dissimilarity_na, homogeneity_na, energy_na, correlation_na, asm_na = texture_feature(na_img_list)
opening_us, closing_us, gradient_us, tophat_us, blackhat_us = morphology_feature(us_img_list)
opening_na, closing_na, gradient_na, tophat_na, blackhat_na = morphology_feature(na_img_list)
# 计算两种图像的特征差异
hue_hist_diff = feature_diff(hue_hist_us, hue_hist_na)
sat_hist_diff = feature_diff(sat_hist_us, sat_hist_na)
val_hist_diff = feature_diff(val_hist_us, val_hist_na)
contrast_diff = feature_diff(contrast_us, contrast_na)
dissimilarity_diff = feature_diff(dissimilarity_us, dissimilarity_na)
homogeneity_diff = feature_diff(homogeneity_us, homogeneity_na)
energy_diff = feature_diff(energy_us, energy_na)
correlation_diff = feature_diff(correlation_us, correlation_na)
asm_diff = feature_diff(asm_us, asm_na)
opening_diff = feature_diff(opening_us, opening_na)
closing_diff = feature_diff(closing_us, closing_na)
gradient_diff = feature_diff(gradient_us, gradient_na)
tophat_diff = feature_diff(tophat_us, tophat_na)
blackhat_diff = feature_diff(blackhat_us, blackhat_na)
# 输出特征差异
print('Hue hist diff:', hue_hist_diff)
print('Sat hist diff:', sat_hist_diff)
print('Val hist diff:', val_hist_diff)
print('Contrast diff:', contrast_diff)
print('Dissimilarity diff:', dissimilarity_diff)
print('Homogeneity diff:', homogeneity_diff)
print('Energy diff:', energy_diff)
print('Correlation diff:', correlation_diff)
print('ASM diff:', asm_diff)
print('Opening diff:', opening_diff)
print('Closing diff:', closing_diff)
print('Gradient diff:', gradient_diff)
print('Tophat diff:', tophat_diff)
print('Blackhat diff:', blackhat_diff)
```
该代码会输出每种特征的差异性分析结果。需要注意的是,由于KL散度的值在不同特征中具有不同的量纲,因此输出的结果可能不具有可比性。
阅读全文