数据增强算法代码python
时间: 2023-09-08 22:13:50 浏览: 105
python进行数据增强
以下是一些常见的图像数据增强算法的Python代码示例:
1. 色彩调整:
```python
import cv2
import numpy as np
def adjust_brightness(image, value):
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
v = np.clip(v * value, 0, 255).astype(np.uint8)
hsv = cv2.merge((h, s, v))
image = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
return image
def adjust_contrast(image, value):
alpha = float(value) / 127.0
adjusted = cv2.convertScaleAbs(image, alpha=alpha, beta=0)
return adjusted
```
2. 锐化增强:
```python
import cv2
import numpy as np
def sharpen_image(image):
kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
sharpened = cv2.filter2D(image, -1, kernel)
return sharpened
```
3. 噪声去除:
```python
import cv2
import numpy as np
def denoise_image(image):
denoised = cv2.fastNlMeansDenoisingColored(image, None, 10, 10, 7, 21)
return denoised
```
4. 图像平滑:
```python
import cv2
import numpy as np
def smooth_image(image):
smoothed = cv2.GaussianBlur(image, (5, 5), 0)
return smoothed
```
这些代码示例演示了如何使用OpenCV库中的函数来实现常见的图像增强算法。你可以根据自己的需求进行调整和扩展。注意,在运行代码之前,确保安装了OpenCV库。
阅读全文