遥感图像数据扩充常用代码
时间: 2023-10-23 09:14:37 浏览: 35
遥感图像数据扩充是通过对原始数据进行一系列变换,从而生成更多的训练数据,以提高深度学习模型的泛化能力和识别准确度。常用的数据扩充方法包括镜像翻转、随机裁剪、旋转、缩放等。以下是一些常用的代码实现,供参考:
1. 镜像翻转
```python
import cv2
import numpy as np
def flip(image, axis=1):
"""
镜像翻转
:param image: 输入的图像
:param axis: 翻转轴,0表示垂直翻转,1表示水平翻转
:return: 翻转后的图像
"""
return cv2.flip(image, axis)
```
2. 随机裁剪
```python
import random
import numpy as np
def random_crop(image, crop_size):
"""
随机裁剪
:param image: 输入的图像
:param crop_size: 裁剪尺寸
:return: 裁剪后的图像
"""
h, w, c = image.shape
th, tw = crop_size
if w == tw and h == th:
return image
x1 = random.randint(0, w - tw)
y1 = random.randint(0, h - th)
return image[y1:y1+th, x1:x1+tw, :]
```
3. 旋转
```python
import cv2
import numpy as np
def rotate(image, angle):
"""
旋转
:param image: 输入的图像
:param angle: 旋转角度
:return: 旋转后的图像
"""
h, w, c = image.shape
M = cv2.getRotationMatrix2D((w/2, h/2), angle, 1)
return cv2.warpAffine(image, M, (w, h))
```
4. 缩放
```python
import cv2
import numpy as np
def resize(image, size):
"""
缩放
:param image: 输入的图像
:param size: 缩放尺寸
:return: 缩放后的图像
"""
return cv2.resize(image, size)
```
以上是一些常用的数据扩充方法的代码实现,可以根据需要进行组合和调整。