OpenCV-18图像的翻转和旋转

一、图像的翻转

使用API---cv.flip(src, flipCode)

flipCode = 0表示上下翻转

flipCode > 0表示左右翻转

flipCode < 0上下 + 左右翻转

或者使用np的翻转src[: : -1,: : -1]实现上下翻转。

示例代码如下:

import cv2
import numpy as np

dog = cv2.imread("dog.png")

# 图片的翻转
# new_dog = cv2.flip(dog, 0)
new_dog1 = cv2.flip(dog, 1)
new_dog2 = cv2.flip(dog, -1)

# 使用np同样可以实现上下翻转。
new_dog = dog[::-1, ::-1]

cv2.imshow("dog", dog)
cv2.imshow("new_dog", new_dog)
cv2.imshow("new_dog1", new_dog1)
cv2.imshow("new_dog2", new_dog2)

cv2.waitKey(0)
cv2.destroyAllWindows()

输出结果如下:

二、图像的旋转

使用API ---cv2.rotate(img, rotateCode)

ROTATE_90_CLOCKWISE    90度顺时针

ROTATE_180       180度顺时针

ROTATE_90_COUNTERCLOCKWISE   90度逆时针

new_dog = cv2.rotate(dog, cv2.ROTATE_90_CLOCKWISE)
new_dog1 = cv2.rotate(dog, cv2.ROTATE_180)
new_dog2 = cv2.rotate(dog, cv2.ROTATE_90_COUNTERCLOCKWISE)

输出结果如下:

只提供了这三个角度设置,无法旋转其他角度。

综合演示代码如下所示:

import cv2
import numpy as np

dog = cv2.imread("dog.png")

# 图片的翻转
# new_dog = cv2.flip(dog, 0)
# new_dog1 = cv2.flip(dog, 1)
# new_dog2 = cv2.flip(dog, -1)

# 使用np同样可以实现上下翻转。
# new_dog = dog[::-1, ::-1]

# 图像的旋转
new_dog = cv2.rotate(dog, cv2.ROTATE_90_CLOCKWISE)
new_dog1 = cv2.rotate(dog, cv2.ROTATE_180)
new_dog2 = cv2.rotate(dog, cv2.ROTATE_90_COUNTERCLOCKWISE)

cv2.imshow("dog", dog)
cv2.imshow("new_dog", new_dog)
cv2.imshow("new_dog1", new_dog1)
cv2.imshow("new_dog2", new_dog2)

cv2.waitKey(0)
cv2.destroyAllWindows()

相关推荐

  1. OpenCV图像翻转旋转

    2024-01-09 10:00:01       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-09 10:00:01       91 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-09 10:00:01       97 阅读
  3. 在Django里面运行非项目文件

    2024-01-09 10:00:01       78 阅读
  4. Python语言-面向对象

    2024-01-09 10:00:01       88 阅读

热门阅读

  1. 算法训练营Day40(动态规划)

    2024-01-09 10:00:01       62 阅读
  2. 在 PyCharm 中运用 GitHub Copilot 的详细指南

    2024-01-09 10:00:01       56 阅读
  3. 笙默考试管理系统-MyExamTest----codemirror(63)

    2024-01-09 10:00:01       51 阅读
  4. 机器学习 -决策树的案例

    2024-01-09 10:00:01       65 阅读
  5. how2heap-2.23-15-house_of_einherjar

    2024-01-09 10:00:01       67 阅读
  6. 记一次线上报错 GList AddChildAt NullReferenceException

    2024-01-09 10:00:01       44 阅读
  7. 知识笔记(七十四)———链式语句中field用法

    2024-01-09 10:00:01       61 阅读