用python实现基于概率分布的超声图像与自然图像性质差异分析这一课题,使用opencv,两种图像的文件类型都是jpg,超声图像的文件路径是‘D:/zzz/us2’,自然图像的文件路径是‘D:/zzz/na2’,两种图像都有84张,图像名称是随机的,需要对比两种图像的特征必须有颜色,纹理,形状,差异性分析方法也需要多种,包括完整详细代码
时间: 2023-09-18 14:10:17 浏览: 84
首先,我们需要安装必要的库:opencv-python、numpy、scikit-image、matplotlib
```python
!pip install opencv-python numpy scikit-image matplotlib
```
然后,我们可以编写代码进行超声图像和自然图像的性质差异分析。具体实现方法如下:
```python
import cv2
import numpy as np
from skimage.feature import greycomatrix, greycoprops
from matplotlib import pyplot as plt
# 定义函数提取图像颜色、纹理、形状等特征
def get_image_features(image):
# 提取颜色特征
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
hist_h = cv2.calcHist([h], [0], None, [180], [0, 180])
hist_s = cv2.calcHist([s], [0], None, [256], [0, 256])
hist_v = cv2.calcHist([v], [0], None, [256], [0, 256])
color_feature = np.concatenate((hist_h, hist_s, hist_v)).ravel()
# 提取纹理特征
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
glcm = greycomatrix(gray, [1], [0, np.pi/4, np.pi/2, 3*np.pi/4], levels=256, symmetric=True, normed=True)
contrast = greycoprops(glcm, 'contrast').ravel()
dissimilarity = greycoprops(glcm, 'dissimilarity').ravel()
homogeneity = greycoprops(glcm, 'homogeneity').ravel()
energy = greycoprops(glcm, 'energy').ravel()
correlation = greycoprops(glcm, 'correlation').ravel()
texture_feature = np.concatenate((contrast, dissimilarity, homogeneity, energy, correlation))
# 提取形状特征
canny = cv2.Canny(gray, 50, 150)
contours, hierarchy = cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnt = max(contours, key=cv2.contourArea)
area = cv2.contourArea(cnt)
perimeter = cv2.arcLength(cnt, True)
x, y, w, h = cv2.boundingRect(cnt)
aspect_ratio = float(w)/h
rectangularity = w*h/area
circularity = (4*np.pi*area)/(perimeter*perimeter)
shape_feature = np.array([aspect_ratio, rectangularity, circularity])
return np.concatenate((color_feature, texture_feature, shape_feature))
# 读取超声图像和自然图像,提取特征并进行对比
us_path = 'D:/zzz/us2/'
na_path = 'D:/zzz/na2/'
us_features = []
na_features = []
for i in range(84):
us_name = us_path + str(i+1) + '.jpg'
na_name = na_path + str(i+1) + '.jpg'
us_image = cv2.imread(us_name)
na_image = cv2.imread(na_name)
us_feature = get_image_features(us_image)
na_feature = get_image_features(na_image)
us_features.append(us_feature)
na_features.append(na_feature)
us_features = np.array(us_features)
na_features = np.array(na_features)
# 对比颜色特征
plt.subplot(2, 2, 1)
plt.plot(us_features[:, :180].mean(axis=0), label='ultrasound')
plt.plot(na_features[:, :180].mean(axis=0), label='natural')
plt.title('Hue Histogram')
plt.legend()
plt.subplot(2, 2, 2)
plt.plot(us_features[:, 180:436].mean(axis=0), label='ultrasound')
plt.plot(na_features[:, 180:436].mean(axis=0), label='natural')
plt.title('Saturation Histogram')
plt.legend()
plt.subplot(2, 2, 3)
plt.plot(us_features[:, 436:].mean(axis=0), label='ultrasound')
plt.plot(na_features[:, 436:].mean(axis=0), label='natural')
plt.title('Value Histogram')
plt.legend()
plt.show()
# 对比纹理特征
plt.subplot(2, 3, 1)
plt.plot(us_features[:, 512:768].mean(axis=0), label='ultrasound')
plt.plot(na_features[:, 512:768].mean(axis=0), label='natural')
plt.title('Contrast')
plt.legend()
plt.subplot(2, 3, 2)
plt.plot(us_features[:, 768:1024].mean(axis=0), label='ultrasound')
plt.plot(na_features[:, 768:1024].mean(axis=0), label='natural')
plt.title('Dissimilarity')
plt.legend()
plt.subplot(2, 3, 3)
plt.plot(us_features[:, 1024:1280].mean(axis=0), label='ultrasound')
plt.plot(na_features[:, 1024:1280].mean(axis=0), label='natural')
plt.title('Homogeneity')
plt.legend()
plt.subplot(2, 3, 4)
plt.plot(us_features[:, 1280:1536].mean(axis=0), label='ultrasound')
plt.plot(na_features[:, 1280:1536].mean(axis=0), label='natural')
plt.title('Energy')
plt.legend()
plt.subplot(2, 3, 5)
plt.plot(us_features[:, 1536:].mean(axis=0), label='ultrasound')
plt.plot(na_features[:, 1536:].mean(axis=0), label='natural')
plt.title('Correlation')
plt.legend()
plt.show()
# 对比形状特征
plt.subplot(2, 2, 1)
plt.bar(['ultrasound', 'natural'], [us_features[:, -3].mean(), na_features[:, -3].mean()])
plt.title('Aspect Ratio')
plt.subplot(2, 2, 2)
plt.bar(['ultrasound', 'natural'], [us_features[:, -2].mean(), na_features[:, -2].mean()])
plt.title('Rectangularity')
plt.subplot(2, 2, 3)
plt.bar(['ultrasound', 'natural'], [us_features[:, -1].mean(), na_features[:, -1].mean()])
plt.title('Circularity')
plt.show()
```
这段代码将分别对超声图像和自然图像进行特征提取,并对比颜色、纹理、形状等特征的差异性。我们可以通过观察图像来分析两种图像的性质差异。
阅读全文