一、圖像的翻轉
使用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)
輸出結果如下:
只提供了這三個角度設置,無法旋轉其他角度。文章來源:http://www.zghlxwxcb.cn/news/detail-781153.html
綜合演示代碼如下所示:文章來源地址http://www.zghlxwxcb.cn/news/detail-781153.html
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()
到了這里,關于OpenCV-18圖像的翻轉和旋轉的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!