opencv image rotation and flip, cv2.flip, cv2.rotate

content

flip the image

Image rotation


flip the image

        Image flipping can be achieved using cv2.filp in opencv

def flip(src, flipCode, dst=None)
  • src: input image
  • flipCode: flipCode A flag to specify how to flip the array; 0 means flip up and down, a positive number means flip left and right, and a negative number means flip both up and down.
  • dst: output image

        The code below rotates the image differently.

import cv2
import numpy as np

lp = cv2.resize(cv2.imread('../images/lp.jpg'), None, fx=0.7, fy=0.7)
# 翻转 0表示上下,正数表示左右,负数表示上下左右都翻转
new_lp1 = cv2.flip(lp, 0)
new_lp2 = cv2.flip(lp, 1)
new_lp3 = cv2.flip(lp, -1)

cv2.imshow('lp', np.hstack((lp, new_lp1, new_lp2, new_lp3)))
cv2.waitKey(0)
cv2.destroyAllWindows()


Image rotation

        Image rotation using cv2.rotate in opencv

def rotate(src, rotateCode, dst=None)
  • src: input image
  • rotateCode: flip angle, 3 options, 90 degrees, 180 degrees, 270 degrees
  • dst: output image

        The options for rotateCode are as follows

parameter describe
ROTATE_90_CLOCKWISE Rotate 90 degrees clockwise
ROTATE_180 Rotate 180 degrees
ROTATE_90_COUNTERCLOCKWISE Rotate 90 degrees counterclockwise, which is 270 degrees clockwise

        The following code uses 3 parameters

import cv2

lp = cv2.resize(cv2.imread('../images/lp.jpg'), None, fx=0.7, fy=0.7)

# 平移 3种旋转,使用cv2.ROTATE_xxx进行选择
lp1 = cv2.rotate(lp, cv2.ROTATE_90_CLOCKWISE)
lp2 = cv2.rotate(lp, cv2.ROTATE_90_COUNTERCLOCKWISE)
lp3 = cv2.rotate(lp, cv2.ROTATE_180)

cv2.imshow('lp', lp)
cv2.imshow('lp1', lp1)
cv2.imshow('lp2', lp2)
cv2.imshow('lp3', lp3)
cv2.waitKey(0)
cv2.destroyAllWindows()

 

Guess you like

Origin blog.csdn.net/m0_51545690/article/details/123959372