用python实现基于概率分布的超声图像与自然图像性质差异分析,超声图像的文件路径是D:\zzz\na5,自然图像的文件路径是D:\zzz\us5,请注意图像文件的大小不一,需要先进行归一化处理,需要判断超声图像与自然图像的像素值分布是否存在显著差异
时间: 2024-01-22 22:20:40 浏览: 26
首先,我们需要导入一些必要的库:
```python
import os
import cv2
import numpy as np
from matplotlib import pyplot as plt
```
然后,我们可以定义一个函数来读取并归一化图像:
```python
def read_and_normalize_image(file_path):
# 读取图像
img = cv2.imread(file_path, cv2.IMREAD_GRAYSCALE)
# 将像素值归一化到 [0, 1] 范围内
img = img / 255.0
return img
```
接下来,我们可以定义一个函数来计算图像的像素值分布:
```python
def calculate_pixel_distribution(img):
# 计算像素值分布
pixel_values, pixel_counts = np.unique(img, return_counts=True)
# 将像素值和像素数转换为百分比
pixel_percentages = pixel_counts / np.sum(pixel_counts)
# 返回像素值和像素百分比
return pixel_values, pixel_percentages
```
然后,我们可以使用这些函数来读取、归一化和计算图像的像素值分布:
```python
# 读取和归一化超声图像
us_images_path = 'D:/zzz/us5'
us_images = []
for file_name in os.listdir(us_images_path):
file_path = os.path.join(us_images_path, file_name)
img = read_and_normalize_image(file_path)
us_images.append(img)
# 计算超声图像的像素值分布
us_pixel_values = []
us_pixel_percentages = []
for img in us_images:
pixel_values, pixel_percentages = calculate_pixel_distribution(img)
us_pixel_values.append(pixel_values)
us_pixel_percentages.append(pixel_percentages)
# 读取和归一化自然图像
na_images_path = 'D:/zzz/na5'
na_images = []
for file_name in os.listdir(na_images_path):
file_path = os.path.join(na_images_path, file_name)
img = read_and_normalize_image(file_path)
na_images.append(img)
# 计算自然图像的像素值分布
na_pixel_values = []
na_pixel_percentages = []
for img in na_images:
pixel_values, pixel_percentages = calculate_pixel_distribution(img)
na_pixel_values.append(pixel_values)
na_pixel_percentages.append(pixel_percentages)
```
最后,我们可以使用像素值和像素百分比来绘制直方图,并比较超声图像与自然图像的像素值分布:
```python
# 绘制超声图像的像素值分布直方图
plt.figure()
for i in range(len(us_pixel_values)):
plt.plot(us_pixel_values[i], us_pixel_percentages[i])
plt.title('Pixel Value Distribution of Ultrasound Images')
plt.xlabel('Pixel Value')
plt.ylabel('Percentage')
plt.xlim([0, 1])
plt.ylim([0, 0.5])
# 绘制自然图像的像素值分布直方图
plt.figure()
for i in range(len(na_pixel_values)):
plt.plot(na_pixel_values[i], na_pixel_percentages[i])
plt.title('Pixel Value Distribution of Natural Images')
plt.xlabel('Pixel Value')
plt.ylabel('Percentage')
plt.xlim([0, 1])
plt.ylim([0, 0.5])
# 比较超声图像和自然图像的像素值分布
plt.figure()
for i in range(len(us_pixel_values)):
plt.plot(us_pixel_values[i], us_pixel_percentages[i], label='Ultrasound Image')
for i in range(len(na_pixel_values)):
plt.plot(na_pixel_values[i], na_pixel_percentages[i], label='Natural Image')
plt.legend()
plt.title('Comparison of Pixel Value Distribution')
plt.xlabel('Pixel Value')
plt.ylabel('Percentage')
plt.xlim([0, 1])
plt.ylim([0, 0.5])
plt.show()
```
这样,我们就可以比较超声图像与自然图像的像素值分布了。如果两者的像素值分布存在显著差异,我们可以看到它们在直方图上的形状和位置会有所不同。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)