python实现图像添加正弦噪声
时间: 2024-01-10 16:02:55 浏览: 144
Python给图像添加噪声具体操作
可以使用Python的NumPy和OpenCV库来添加正弦噪声。具体的步骤如下:
1. 导入需要的库
```python
import cv2
import numpy as np
```
2. 读取图片并转换为灰度图像
```python
img = cv2.imread('image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
```
3. 生成正弦噪声
```python
height, width = gray.shape
freq = 10 # 正弦波频率
amplitude = 50 # 正弦波振幅
noise = amplitude * np.sin(2 * np.pi * freq * np.arange(width) / width)
```
4. 将正弦噪声加入图像中
```python
for i in range(height):
gray[i, :] += noise.astype(np.uint8)
```
5. 显示加入噪声后的图像
```python
cv2.imshow('image', gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
完整代码:
```python
import cv2
import numpy as np
img = cv2.imread('image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
height, width = gray.shape
freq = 10 # 正弦波频率
amplitude = 50 # 正弦波振幅
noise = amplitude * np.sin(2 * np.pi * freq * np.arange(width) / width)
for i in range(height):
gray[i, :] += noise.astype(np.uint8)
cv2.imshow('image', gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
阅读全文