首先,我们需要安装Python的图像处理库PIL和NumPy。可以使用以下命令安装:
```bash
pip install pillow numpy
```
接下来,我们将编写一个Python脚本来读取MRI图像,添加椒盐噪声,然后使用均值滤波和中值滤波进行处理。
```python
from PIL import Image
import numpy as np
# 读取MRI图像
def read_image(file_path):
img = Image.open(file_path)
return img
# 添加椒盐噪声
def add_salt_pepper_noise(img, noise_ratio):
img_array = np.array(img)
height, width = img_array.shape
num_noise_pixels = int(height * width * noise_ratio)
for _ in range(num_noise_pixels):
x = np.random.randint(0, width)
y = np.random.randint(0, height)
if np.random.random() < 0.5:
img_array[y, x] = 0
else:
img_array[y, x] = 255
return Image.fromarray(img_array)
# 均值滤波
def mean_filter(img, kernel_size):
img_array = np.array(img)
height, width = img_array.shape
pad = kernel_size // 2
output = np.zeros_like(img_array)
for y in range(pad, height - pad):
for x in range(pad, width - pad):
window = img_array[y - pad:y + pad + 1, x - pad:x + pad + 1]
output[y, x] = np.mean(window)
return Image.fromarray(output)
# 中值滤波
def median_filter(img, kernel_size):
img_array = np.array(img)
height, width = img_array.shape
pad = kernel_size // 2
output = np.zeros_like(img_array)
for y in range(pad, height - pad):
for x in range(pad, width - pad):
window = img_array[y - pad:y + pad + 1, x - pad:x + pad + 1]
output[y, x] = np.median(window)
return Image.fromarray(output)
# 主函数
def main():
input_file = 'mri.tif'
output_file_mean = 'mri_mean_filtered.tif'
output_file_median = 'mri_median_filtered.tif'
noise_ratio = 0.05
kernel_size = 3
img = read_image(input_file)
img_with_noise = add_salt_pepper_noise(img, noise_ratio)
img_mean_filtered = mean_filter(img_with_noise, kernel_size)
img_median_filtered = median_filter(img_with_noise, kernel_size)
img_mean_filtered.save(output_file_mean)
img_median_filtered.save(output_file_median)
if __name__ == '__main__':
main()
```
将上述代码保存为`mri_filtering.py`,然后在命令行中运行`python mri_filtering.py`。这将生成两个处理后的图像文件:`mri_mean_filtered.tif`和`mri_median_filtered.tif`。
```bash
pip install pillow numpy
```
接下来,我们将编写一个Python脚本来读取MRI图像,添加椒盐噪声,然后使用均值滤波和中值滤波进行处理。
```python
from PIL import Image
import numpy as np
# 读取MRI图像
def read_image(file_path):
img = Image.open(file_path)
return img
# 添加椒盐噪声
def add_salt_pepper_noise(img, noise_ratio):
img_array = np.array(img)
height, width = img_array.shape
num_noise_pixels = int(height * width * noise_ratio)
for _ in range(num_noise_pixels):
x = np.random.randint(0, width)
y = np.random.randint(0, height)
if np.random.random() < 0.5:
img_array[y, x] = 0
else:
img_array[y, x] = 255
return Image.fromarray(img_array)
# 均值滤波
def mean_filter(img, kernel_size):
img_array = np.array(img)
height, width = img_array.shape
pad = kernel_size // 2
output = np.zeros_like(img_array)
for y in range(pad, height - pad):
for x in range(pad, width - pad):
window = img_array[y - pad:y + pad + 1, x - pad:x + pad + 1]
output[y, x] = np.mean(window)
return Image.fromarray(output)
# 中值滤波
def median_filter(img, kernel_size):
img_array = np.array(img)
height, width = img_array.shape
pad = kernel_size // 2
output = np.zeros_like(img_array)
for y in range(pad, height - pad):
for x in range(pad, width - pad):
window = img_array[y - pad:y + pad + 1, x - pad:x + pad + 1]
output[y, x] = np.median(window)
return Image.fromarray(output)
# 主函数
def main():
input_file = 'mri.tif'
output_file_mean = 'mri_mean_filtered.tif'
output_file_median = 'mri_median_filtered.tif'
noise_ratio = 0.05
kernel_size = 3
img = read_image(input_file)
img_with_noise = add_salt_pepper_noise(img, noise_ratio)
img_mean_filtered = mean_filter(img_with_noise, kernel_size)
img_median_filtered = median_filter(img_with_noise, kernel_size)
img_mean_filtered.save(output_file_mean)
img_median_filtered.save(output_file_median)
if __name__ == '__main__':
main()
```
将上述代码保存为`mri_filtering.py`,然后在命令行中运行`python mri_filtering.py`。这将生成两个处理后的图像文件:`mri_mean_filtered.tif`和`mri_median_filtered.tif`。