用python实现基于概率分布的超声图像与自然图像性质差异分析这一课题,不用greycomatrix和greycoprops,使用opencv,两种图像的文件类型都是jpg,超声图像的文件路径是‘D:\zzz\us2’,自然图像的文件路径是‘D:\zzz\na2’,两种图像都有84张,图片形状都不同,图像名称是随机的,需要对比两种图像的特征必须有颜色,纹理,形状,差异性分析方法也需要多种,包括完整详细代码
时间: 2023-10-09 11:06:06 浏览: 128
利用python处理图片资料
以下是基于概率分布的超声图像与自然图像性质差异分析的Python代码,使用了OpenCV库:
```python
import cv2
import numpy as np
import os
# 文件路径
us_path = 'D:\\zzz\\us2\\'
na_path = 'D:\\zzz\\na2\\'
# 读取图像
us_images = []
na_images = []
for filename in os.listdir(us_path):
img = cv2.imread(us_path + filename)
us_images.append(img)
for filename in os.listdir(na_path):
img = cv2.imread(na_path + filename)
na_images.append(img)
# 颜色特征分析
us_color_hist = []
na_color_hist = []
for img in us_images:
hist = cv2.calcHist([img], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256])
hist = cv2.normalize(hist, hist).flatten()
us_color_hist.append(hist)
for img in na_images:
hist = cv2.calcHist([img], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256])
hist = cv2.normalize(hist, hist).flatten()
na_color_hist.append(hist)
# 纹理特征分析
us_texture_hist = []
na_texture_hist = []
win_size = 21
for img in us_images:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
texture = cv2.calcHist([cv2.Laplacian(gray, cv2.CV_64F)], [0], None, [256], [-127, 128])
texture = cv2.normalize(texture, texture).flatten()
us_texture_hist.append(texture)
for img in na_images:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
texture = cv2.calcHist([cv2.Laplacian(gray, cv2.CV_64F)], [0], None, [256], [-127, 128])
texture = cv2.normalize(texture, texture).flatten()
na_texture_hist.append(texture)
# 形状特征分析
us_shape_hist = []
na_shape_hist = []
for img in us_images:
contours, _ = cv2.findContours(cv2.Canny(img, 100, 200), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
hist = np.zeros((1, len(contours)))
for i in range(len(contours)):
hist[0, i] = cv2.contourArea(contours[i])
hist = cv2.normalize(hist, hist).flatten()
us_shape_hist.append(hist)
for img in na_images:
contours, _ = cv2.findContours(cv2.Canny(img, 100, 200), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
hist = np.zeros((1, len(contours)))
for i in range(len(contours)):
hist[0, i] = cv2.contourArea(contours[i])
hist = cv2.normalize(hist, hist).flatten()
na_shape_hist.append(hist)
# 特征差异性分析
color_diff = cv2.compareHist(us_color_hist[0], na_color_hist[0], cv2.HISTCMP_CHISQR_ALT)
texture_diff = cv2.compareHist(us_texture_hist[0], na_texture_hist[0], cv2.HISTCMP_CHISQR_ALT)
shape_diff = cv2.compareHist(us_shape_hist[0], na_shape_hist[0], cv2.HISTCMP_CHISQR_ALT)
print('颜色特征差异性分析:', color_diff)
print('纹理特征差异性分析:', texture_diff)
print('形状特征差异性分析:', shape_diff)
```
以上代码中,我们首先使用`cv2.imread()`函数读取文件夹中的图像,然后使用`cv2.calcHist()`计算图像的颜色和纹理直方图,以及形状特征的轮廓面积,使用`cv2.normalize()`对直方图进行归一化处理,最后使用`cv2.compareHist()`计算不同图像特征之间的差异性。
阅读全文