帮我写出 关于1.读取图像“15_noise.png”,转化为灰度图,并显示图像。2.去除图像“15_noise.png”中的椒盐噪声,并将图像旋转30度显示,3.用双线性插值法将图像“15_noise.png”放大1.2倍后,对图像进行梯度锐化的具体jupyter代码
时间: 2024-10-14 22:11:45 浏览: 66
在Python中,我们可以使用`PIL`库(Pillow)处理图像,`scipy`库用于降噪,以及`skimage`库来进行图像操作。以下是一段示例Jupyter Notebook代码:
```python
# 导入所需的库
from PIL import Image
import numpy as np
import scipy.signal
from skimage.io import imread, imsave
from skimage.exposure import rescale_intensity
from skimage.transform import rotate, resize, downscale_local_mean
# 1. 读取图像并转换为灰度图
img = Image.open('15_noise.png')
gray_img = img.convert('L') # 转换为灰度
gray_img.show() # 显示图像
# 2. 去除椒盐噪声并旋转
def remove_salt_pepper_noise(image, threshold=0.1):
mean = image.mean()
mask = (np.abs(image - mean) < threshold * mean).astype(np.uint8)
cleaned_image = image.copy()
cleaned_image[mask] = mean
return cleaned_image
noisy_img = imread('15_noise.png')
cleaned_img = remove_salt_pepper_noise(noisy_img)
rotated_img = rotate(cleaned_img, 30)
rotated_img.show()
# 3. 双线性插值放大并进行梯度锐化
def gradient_sharpening(image, sigma=1):
blurred = gaussian_filter(image, sigma)
grad_x = sobel_x(blurred)
grad_y = sobel_y(blurred)
sharpened = image + 0.15 * (grad_x ** 2 + grad_y ** 2) / (blurred.std() + 1e-7) * (image - blurred)
return sharpened
amplified_img = resize(gray_img, (int(gray_img.size[0] * 1.2), int(gray_img.size[1] * 1.2)), anti_aliasing=True) # 使用双线性插值放大
sharp_img = gradient_sharpening(amplified_img)
# 保存处理后的图像
imsave('sharpened_15_noise.png', sharp_img)
阅读全文