用python实现基于概率分布的超声图像与自然图像性质差异分析这一课题,不用greycomatrix和greycoprops,使用opencv,不用两种图像的文件类型都是jpg,超声图像的文件路径是‘D:/zzz/us2’,自然图像的文件路径是‘D:/zzz/na2’,两种图像都有84张,图片形状都不同,图像名称是随机的,需要对比两种图像的特征必须有颜色,纹理,形状,差异性分析方法也需要多种,包括完整详细代码
时间: 2023-09-19 21:09:59 浏览: 35
实现基于概率分布的超声图像与自然图像性质差异分析可以采用以下步骤:
1. 导入必要的库
```
import cv2
import numpy as np
import os
from scipy.stats import multivariate_normal
```
2. 读取图像并转换为HSV颜色空间
```
def read_images(path):
images = []
for file in os.listdir(path):
if file.endswith(".jpg"):
img = cv2.imread(os.path.join(path, file))
img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
images.append(img)
return images
us_images = read_images("D:/zzz/us2")
na_images = read_images("D:/zzz/na2")
```
3. 计算颜色分布
```
def calculate_color_distribution(images):
hue_hist = np.zeros(180)
sat_hist = np.zeros(256)
val_hist = np.zeros(256)
for img in images:
h, s, v = cv2.split(img)
hue_hist += np.histogram(h, bins=180, range=(0, 180))[0]
sat_hist += np.histogram(s, bins=256, range=(0, 256))[0]
val_hist += np.histogram(v, bins=256, range=(0, 256))[0]
hue_hist /= np.sum(hue_hist)
sat_hist /= np.sum(sat_hist)
val_hist /= np.sum(val_hist)
return hue_hist, sat_hist, val_hist
us_hue_hist, us_sat_hist, us_val_hist = calculate_color_distribution(us_images)
na_hue_hist, na_sat_hist, na_val_hist = calculate_color_distribution(na_images)
```
4. 计算纹理分布
```
def calculate_texture_distribution(images):
texture_hist = np.zeros(256)
for img in images:
gray = cv2.cvtColor(img, cv2.COLOR_HSV2GRAY)
texture = cv2.Laplacian(gray, cv2.CV_64F).var()
texture_hist += np.histogram(texture, bins=256, range=(0, 256))[0]
texture_hist /= np.sum(texture_hist)
return texture_hist
us_texture_hist = calculate_texture_distribution(us_images)
na_texture_hist = calculate_texture_distribution(na_images)
```
5. 计算形状分布
```
def calculate_shape_distribution(images):
shape_hist = np.zeros(256)
for img in images:
contours, _ = cv2.findContours(cv2.Canny(img, 100, 200), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
area = cv2.contourArea(contours[0])
shape_hist += np.histogram(area, bins=256, range=(0, 256))[0]
shape_hist /= np.sum(shape_hist)
return shape_hist
us_shape_hist = calculate_shape_distribution(us_images)
na_shape_hist = calculate_shape_distribution(na_images)
```
6. 计算差异性
```
def calculate_difference(hist1, hist2):
return np.sum(np.abs(hist1 - hist2))
color_diff = calculate_difference(us_hue_hist, na_hue_hist) + \
calculate_difference(us_sat_hist, na_sat_hist) + \
calculate_difference(us_val_hist, na_val_hist)
texture_diff = calculate_difference(us_texture_hist, na_texture_hist)
shape_diff = calculate_difference(us_shape_hist, na_shape_hist)
total_diff = color_diff + texture_diff + shape_diff
print("Color difference: ", color_diff)
print("Texture difference: ", texture_diff)
print("Shape difference: ", shape_diff)
print("Total difference: ", total_diff)
```
完整代码如下:
```
import cv2
import numpy as np
import os
from scipy.stats import multivariate_normal
def read_images(path):
images = []
for file in os.listdir(path):
if file.endswith(".jpg"):
img = cv2.imread(os.path.join(path, file))
img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
images.append(img)
return images
def calculate_color_distribution(images):
hue_hist = np.zeros(180)
sat_hist = np.zeros(256)
val_hist = np.zeros(256)
for img in images:
h, s, v = cv2.split(img)
hue_hist += np.histogram(h, bins=180, range=(0, 180))[0]
sat_hist += np.histogram(s, bins=256, range=(0, 256))[0]
val_hist += np.histogram(v, bins=256, range=(0, 256))[0]
hue_hist /= np.sum(hue_hist)
sat_hist /= np.sum(sat_hist)
val_hist /= np.sum(val_hist)
return hue_hist, sat_hist, val_hist
def calculate_texture_distribution(images):
texture_hist = np.zeros(256)
for img in images:
gray = cv2.cvtColor(img, cv2.COLOR_HSV2GRAY)
texture = cv2.Laplacian(gray, cv2.CV_64F).var()
texture_hist += np.histogram(texture, bins=256, range=(0, 256))[0]
texture_hist /= np.sum(texture_hist)
return texture_hist
def calculate_shape_distribution(images):
shape_hist = np.zeros(256)
for img in images:
contours, _ = cv2.findContours(cv2.Canny(img, 100, 200), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
area = cv2.contourArea(contours[0])
shape_hist += np.histogram(area, bins=256, range=(0, 256))[0]
shape_hist /= np.sum(shape_hist)
return shape_hist
def calculate_difference(hist1, hist2):
return np.sum(np.abs(hist1 - hist2))
us_images = read_images("D:/zzz/us2")
na_images = read_images("D:/zzz/na2")
us_hue_hist, us_sat_hist, us_val_hist = calculate_color_distribution(us_images)
na_hue_hist, na_sat_hist, na_val_hist = calculate_color_distribution(na_images)
us_texture_hist = calculate_texture_distribution(us_images)
na_texture_hist = calculate_texture_distribution(na_images)
us_shape_hist = calculate_shape_distribution(us_images)
na_shape_hist = calculate_shape_distribution(na_images)
color_diff = calculate_difference(us_hue_hist, na_hue_hist) + \
calculate_difference(us_sat_hist, na_sat_hist) + \
calculate_difference(us_val_hist, na_val_hist)
texture_diff = calculate_difference(us_texture_hist, na_texture_hist)
shape_diff = calculate_difference(us_shape_hist, na_shape_hist)
total_diff = color_diff + texture_diff + shape_diff
print("Color difference: ", color_diff)
print("Texture difference: ", texture_diff)
print("Shape difference: ", shape_diff)
print("Total difference: ", total_diff)
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)