用python实现基于概率分布的超声图像与自然图像性质差异分析这一课题,使用opencv,两种图像的文件类型都是jpg,超声图像的文件路径是‘D:\zzz\zus2\’,自然图像的文件路径是‘D:\zzz\zna2\’,两种图像都有84张,需要对比两种图像的特征必须有颜色,纹理,形状,差异性分析方法也需要多种,包括完整详细代码
时间: 2023-11-27 11:53:24 浏览: 54
利用OpenCV和Python实现查找图片差异
首先需要安装Opencv库,可以使用以下指令安装:
```python
!pip install opencv-python
```
然后开始实现特征提取和差异性分析方法。
1. 颜色特征提取
使用HSV颜色空间提取颜色特征,计算每张图像的颜色直方图,并将其归一化。
```python
import cv2
import numpy as np
def color_histogram(image):
hsv = cv2.cvtColor(image, 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()
```
2. 纹理特征提取
使用LBP算法提取纹理特征,计算每张图像的LBP直方图,并将其归一化。
```python
def lbp_histogram(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
radius = 1
numPoints = 8 * radius
lbp = cv2.LBP(gray, numPoints, radius, method=cv2.LBP_UNIFORM)
hist, _ = np.histogram(lbp.ravel(), bins=np.arange(0, numPoints + 3), range=(0, numPoints + 2))
hist = hist.astype("float")
hist /= (hist.sum() + 1e-7)
return hist
```
3. 形状特征提取
使用Hu矩算法提取形状特征,计算每张图像的Hu矩,并将其归一化。
```python
def hu_moments(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
moments = cv2.moments(gray)
huMoments = cv2.HuMoments(moments)
huMoments = -np.sign(huMoments) * np.log10(np.abs(huMoments) + 1e-7)
huMoments = huMoments.flatten()
return huMoments
```
4. 差异性分析
使用Kullback-Leibler散度计算两种图像的颜色、纹理、形状特征的差异性,并输出结果。
```python
def kl_divergence(p, q):
p = np.asarray(p, dtype=np.float)
q = np.asarray(q, dtype=np.float)
return np.sum(np.where(p != 0, p * np.log(p / q), 0))
def compare_images(path1, path2):
image1 = cv2.imread(path1)
image2 = cv2.imread(path2)
color_hist1 = color_histogram(image1)
color_hist2 = color_histogram(image2)
texture_hist1 = lbp_histogram(image1)
texture_hist2 = lbp_histogram(image2)
shape_feat1 = hu_moments(image1)
shape_feat2 = hu_moments(image2)
color_diff = kl_divergence(color_hist1, color_hist2)
texture_diff = kl_divergence(texture_hist1, texture_hist2)
shape_diff = kl_divergence(shape_feat1, shape_feat2)
print(f"Color difference: {color_diff:.6f}, Texture difference: {texture_diff:.6f}, Shape difference: {shape_diff:.6f}")
```
最后,使用以上方法计算两种图像的差异性,并输出结果。
```python
for i in range(1, 85):
print(f"Image {i}")
ultrasound_path = f"D:/zzz/zus2/{i}.jpg"
natural_path = f"D:/zzz/zna2/{i}.jpg"
compare_images(ultrasound_path, natural_path)
```
完整代码如下:
```python
import cv2
import numpy as np
def color_histogram(image):
hsv = cv2.cvtColor(image, 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 lbp_histogram(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
radius = 1
numPoints = 8 * radius
lbp = cv2.LBP(gray, numPoints, radius, method=cv2.LBP_UNIFORM)
hist, _ = np.histogram(lbp.ravel(), bins=np.arange(0, numPoints + 3), range=(0, numPoints + 2))
hist = hist.astype("float")
hist /= (hist.sum() + 1e-7)
return hist
def hu_moments(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
moments = cv2.moments(gray)
huMoments = cv2.HuMoments(moments)
huMoments = -np.sign(huMoments) * np.log10(np.abs(huMoments) + 1e-7)
huMoments = huMoments.flatten()
return huMoments
def kl_divergence(p, q):
p = np.asarray(p, dtype=np.float)
q = np.asarray(q, dtype=np.float)
return np.sum(np.where(p != 0, p * np.log(p / q), 0))
def compare_images(path1, path2):
image1 = cv2.imread(path1)
image2 = cv2.imread(path2)
color_hist1 = color_histogram(image1)
color_hist2 = color_histogram(image2)
texture_hist1 = lbp_histogram(image1)
texture_hist2 = lbp_histogram(image2)
shape_feat1 = hu_moments(image1)
shape_feat2 = hu_moments(image2)
color_diff = kl_divergence(color_hist1, color_hist2)
texture_diff = kl_divergence(texture_hist1, texture_hist2)
shape_diff = kl_divergence(shape_feat1, shape_feat2)
print(f"Color difference: {color_diff:.6f}, Texture difference: {texture_diff:.6f}, Shape difference: {shape_diff:.6f}")
for i in range(1, 85):
print(f"Image {i}")
ultrasound_path = f"D:/zzz/zus2/{i}.jpg"
natural_path = f"D:/zzz/zna2/{i}.jpg"
compare_images(ultrasound_path, natural_path)
```
阅读全文