warpAffine实例
时间: 2024-06-15 14:07:44 浏览: 17
warpAffine是OpenCV库中的一个函数,用于对图像进行仿射变换。它可以通过定义一个2x3的变换矩阵来实现平移、旋转、缩放和剪切等操作。下面是一个warpAffine的实例:
```python
import cv2
import numpy as np
# 读取图像
image = cv2.imread('image.jpg')
# 定义变换矩阵
M = np.float32([[1, 0, 100], [0, 1, 50]])
# 进行仿射变换
result = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
# 显示原始图像和变换后的图像
cv2.imshow('Original Image', image)
cv2.imshow('Transformed Image', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
在这个例子中,我们首先使用`cv2.imread`函数读取一张图像,然后定义一个2x3的变换矩阵M,其中`M[0, 2]`表示水平方向的平移量,`M[1, 2]`表示垂直方向的平移量。接下来,我们使用`cv2.warpAffine`函数对图像进行仿射变换,将变换后的结果保存在`result`中。最后,使用`cv2.imshow`函数显示原始图像和变换后的图像。
相关问题
Cv2.GetRotationMatrix2D 实例
下面是使用cv2.GetRotationMatrix2D旋转图像的示例:
``` python
import cv2
# 加载图像
image = cv2.imread("image.jpg")
# 获取图像的中心点和旋转角度
(height, width) = image.shape[:2]
center = (width / 2, height / 2)
angle = 45
# 旋转图像
matrix = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated_image = cv2.warpAffine(image, matrix, (width, height))
# 显示原始图像和旋转后的图像
cv2.imshow("Original Image", image)
cv2.imshow("Rotated Image", rotated_image)
cv2.waitKey(0)
```
在此示例中,我们首先加载一张图像。然后,使用cv2.GetRotationMatrix2D函数获取图像的中心点和旋转角度,其中旋转角度设置为45度。接下来,我们将使用cv2.warpAffine函数来旋转图像。最后,我们将显示原始图像和旋转后的图像。
这是使用cv2.GetRotationMatrix2D旋转图像时的一个基本示例。
实例分割的数据增强代码
以下是一个示例数据增强代码,其中使用了常见的图像增强技术,如随机旋转、缩放、翻转、裁剪等。
```python
import random
import cv2
def random_crop(image, mask, size=(256, 256)):
h, w = image.shape[:2]
new_h, new_w = size
top = random.randint(0, h - new_h)
left = random.randint(0, w - new_w)
image = image[top: top + new_h, left: left + new_w]
mask = mask[top: top + new_h, left: left + new_w]
return image, mask
def random_rotate(image, mask, angle_range=(-30, 30)):
angle = random.uniform(angle_range[0], angle_range[1])
h, w = image.shape[:2]
center = (w / 2, h / 2)
matrix = cv2.getRotationMatrix2D(center, angle, 1.0)
image = cv2.warpAffine(image, matrix, (w, h), flags=cv2.INTER_LINEAR)
mask = cv2.warpAffine(mask, matrix, (w, h), flags=cv2.INTER_NEAREST)
return image, mask
def random_scale(image, mask, scale_range=(0.8, 1.2)):
scale = random.uniform(scale_range[0], scale_range[1])
h, w = image.shape[:2]
new_h, new_w = int(h * scale), int(w * scale)
image = cv2.resize(image, (new_w, new_h), interpolation=cv2.INTER_LINEAR)
mask = cv2.resize(mask, (new_w, new_h), interpolation=cv2.INTER_NEAREST)
return image, mask
def random_flip(image, mask, flip_prob=0.5):
if random.random() < flip_prob:
image = cv2.flip(image, 1)
mask = cv2.flip(mask, 1)
return image, mask
def apply_augmentations(image, mask):
image, mask = random_crop(image, mask)
image, mask = random_rotate(image, mask)
image, mask = random_scale(image, mask)
image, mask = random_flip(image, mask)
return image, mask
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rp](https://img-home.csdnimg.cn/images/20210720083646.png)