用python实现基于概率分布的超声图像与自然图像性质差异分析,不用greycomatrix和greycoprops,使用opencv,两种图像的文件类型都是jpg,超声图像的文件路径是‘D:\zzz\us2’,自然图像的文件路径是‘D:\zzz\na2’,两种图像都有84张,图片形状都不同,图像名称是随机的,需要对比两种图像的特征必须有颜色,纹理,形状,差异性分析方法也需要多种,包括完整详细代码
时间: 2023-10-09 10:06:06 浏览: 79
以下是一种实现方式:
```python
import cv2
import numpy as np
import os
import matplotlib.pyplot as plt
# 定义函数:计算颜色直方图
def calc_color_hist(image):
hist = np.zeros((256, 3))
for i in range(3):
channel_hist = cv2.calcHist([image], [i], None, [256], [0,256])
hist[:, i] = channel_hist[:, 0]
return hist
# 定义函数:计算纹理特征(LBP)
def calc_texture_lbp(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
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 = cv2.calcHist([lbp], [0], None, [256], [0,256])
return hist
# 定义函数:计算形状特征(Hu矩)
def calc_shape_hu(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
moments = cv2.moments(gray)
hu_moments = cv2.HuMoments(moments)
return hu_moments
# 定义函数:对比两张图片的颜色、纹理、形状特征差异
def compare_images(image1, image2):
# 计算颜色直方图差异
hist1 = calc_color_hist(image1)
hist2 = calc_color_hist(image2)
color_diff = cv2.compareHist(hist1, hist2, cv2.HISTCMP_CHISQR)
# 计算纹理特征差异
texture1 = calc_texture_lbp(image1)
texture2 = calc_texture_lbp(image2)
texture_diff = cv2.compareHist(texture1, texture2, cv2.HISTCMP_CHISQR)
# 计算形状特征差异
shape1 = calc_shape_hu(image1)
shape2 = calc_shape_hu(image2)
shape_diff = np.sum(np.abs(np.log(shape1) - np.log(shape2)))
return color_diff, texture_diff, shape_diff
# 读取文件夹中的所有图片
us_dir = 'D:\\zzz\\us2'
na_dir = 'D:\\zzz\\na2'
us_images = []
na_images = []
for filename in os.listdir(us_dir):
image = cv2.imread(os.path.join(us_dir, filename))
us_images.append(image)
for filename in os.listdir(na_dir):
image = cv2.imread(os.path.join(na_dir, filename))
na_images.append(image)
# 计算每张超声图像与每张自然图像的特征差异
color_diffs = np.zeros((len(us_images), len(na_images)))
texture_diffs = np.zeros((len(us_images), len(na_images)))
shape_diffs = np.zeros((len(us_images), len(na_images)))
for i in range(len(us_images)):
for j in range(len(na_images)):
color_diff, texture_diff, shape_diff = compare_images(us_images[i], na_images[j])
color_diffs[i, j] = color_diff
texture_diffs[i, j] = texture_diff
shape_diffs[i, j] = shape_diff
# 找到每张超声图像最相似的自然图像,输出结果
for i in range(len(us_images)):
index = np.argmin(color_diffs[i,:] + texture_diffs[i,:] + shape_diffs[i,:])
print("超声图像{}与自然图像{}最相似".format(i+1, index+1))
# 可视化结果:将每张超声图像与最相似的自然图像放在一起展示
for i in range(len(us_images)):
index = np.argmin(color_diffs[i,:] + texture_diffs[i,:] + shape_diffs[i,:])
plt.subplot(1, 2, 1)
plt.imshow(cv2.cvtColor(us_images[i], cv2.COLOR_BGR2RGB))
plt.title("超声图像{}".format(i+1))
plt.xticks([])
plt.yticks([])
plt.subplot(1, 2, 2)
plt.imshow(cv2.cvtColor(na_images[index], cv2.COLOR_BGR2RGB))
plt.title("自然图像{}".format(index+1))
plt.xticks([])
plt.yticks([])
plt.show()
```
该代码中实现了三种特征差异的计算方法:颜色直方图差异、LBP纹理特征差异、Hu矩形状特征差异。其中颜色直方图和LBP纹理特征使用OpenCV中的函数计算,而Hu矩需要自己实现。在对比两张图片时,将三种特征差异分别计算,并求和作为最终的差异值。通过比较每张超声图像与每张自然图像的差异值,找到最相似的自然图像,输出结果并可视化展示。
阅读全文