OpenCV 06 (basic transformation of images)

1. Basic transformation of images

1.1 Zoom in and out of images

- resize(src, dsize, dst, fx, fy, interpolation)

  - src: the image to be scaled
  - dsize: the size of the image after scaling, both tuple and list representation are available.
  - dst: optional parameter, the output image after scaling
  - fx, fy: the scaling ratio of the x-axis and y-axis , That is, the scaling ratio of width and height.
  - interpolation: interpolation algorithm, mainly including the following:
    - INTER_NEAREST, neighbor interpolation, fast but poor effect.
    - INTER_LINEAR, bilinear interpolation, uses 4 points in the original image for interpolation . Default.
    - INTER_CUBIC, cubic interpolation, 16 points in the original image.
    - INTER_AREA, area interpolation, the best effect and the longest calculation time.

 import cv2
  import numpy as np
  
  #导入图片
  dog = cv2.imread('./dog.jpeg')
  
  # x,y放大一倍
  new_dog = cv2.resize(dog,dsize=(800, 800), interpolation=cv2.INTER_NEAREST)
  cv2.imshow('dog', new_dog)
  cv2.waitKey(0)
  cv2.destroyAllWindows()

1.2 Image flipping

- flip(src, flipCode)
  - flipCode =0 means flip up and down
  - flipCode >0 means flip left and right
  - flipCode <0 up and down + left and right

# 翻转
import cv2
import numpy as np

#导入图片
dog = cv2.imread('./dog.jpeg')

new_dog = cv2.flip(dog, flipCode=-1)
cv2.imshow('dog', new_dog)
cv2.waitKey(0)
cv2.destroyAllWindows()

1.3 Image rotation

- rotate(img, rotateCode)
  - ROTATE_90_CLOCKWISE 90 degrees clockwise
  - ROTATE_180 180 degrees
  - ROTATE_90_COUNTERCLOCKWISE 90 degrees counterclockwise

# 旋转
import cv2
import numpy as np

#导入图片
dog = cv2.imread('./dog.jpeg')

new_dog = cv2.rotate(dog, rotateCode=cv2.cv2.ROTATE_90_COUNTERCLOCKWISE)
cv2.imshow('dog', new_dog)
cv2.waitKey(0)
cv2.destroyAllWindows()

1.4 Image translation of affine transformation

- Affine transformation is the general term for image rotation, scaling, and translation. The specific method is to calculate the sum of the original image coordinates through a matrix, obtain the new coordinates, and complete the transformation. So the key is this matrix. 

- warpAffine(src, M, dsize, flags, mode, value)

- M: transformation matrix

- dsize: output image size

- flag: consistent with the interpolation algorithm in resize

- mode: boundary extrapolation flag

- value: padding boundary value

- translation matrix

 # 仿射变换之平移
  import cv2
  import numpy as np
  
  #导入图片
  dog = cv2.imread('./dog.jpeg')
  
  h, w, ch = dog.shape
  M = np.float32([[1, 0, 100], [0, 1, 0]])
  # 注意opencv中是先宽度, 再高度
  new = cv2.warpAffine(dog, M, (w, h))
  
  cv2.imshow('new', new)
  cv2.waitKey(0)
  cv2.destroyAllWindows()

1.5 Obtaining transformation matrix for affine transformation

The difficulty of affine transformation is to calculate the transformation matrix. OpenCV provides an API for calculating the transformation matrix.

- getRotationMatrix2D(center, angle, scale)
  - center center point, which point in the picture is used as the center point for rotation.
  - angle angle: angle of rotation, rotate counterclockwise.
  - scale scaling ratio: how you want to make the picture of scaling.

# 仿射变换之平移
import cv2
import numpy as np

#导入图片
dog = cv2.imread('./dog.jpeg')

h, w, ch = dog.shape
# M = np.float32([[1, 0, 100], [0, 1, 0]])

# 注意旋转的角度为逆时针.
# M = cv2.getRotationMatrix2D((100, 100), 15, 1.0)
# 以图像中心点旋转
M = cv2.getRotationMatrix2D((w/2, h/2), 15, 1.0)
# 注意opencv中是先宽度, 再高度
new = cv2.warpAffine(dog, M, (w, h))

cv2.imshow('new', new)
cv2.waitKey(0)
cv2.destroyAllWindows()

- getAffineTransform(src[], dst[]) The transformed position can be determined through three points, which is equivalent to solving equations. Three points correspond to three equations, and the offset parameters and rotation angle can be solved.

  - The three points of the original target in src
  - The three points in dst corresponding to the transformation

 # 通过三个点来确定M
  # 仿射变换之平移
  import cv2
  import numpy as np
  
  #导入图片
  dog = cv2.imread('./dog.jpeg')
  
  h, w, ch = dog.shape
  
  # 一般是横向和纵向的点, 所以一定会有2个点横坐标相同, 2个点纵坐标相同
  src = np.float32([[200, 100], [300, 100], [200, 300]])
  dst = np.float32([[100, 150], [360, 200], [280, 120]])
  M = cv2.getAffineTransform(src, dst)
  # 注意opencv中是先宽度, 再高度
  new = cv2.warpAffine(dog, M, (w, h))
  
  cv2.imshow('new', new)
  cv2.waitKey(0)
  cv2.destroyAllWindows()

1.6 Perspective transformation

Perspective transformation is to transform one coordinate system into another coordinate system. To put it simply, you can turn an "oblique" picture into an "upright" one.

- warpPerspective(img, M, dsize,....)

- For perspective transformation, M is a 3 * 3 matrix.

- getPerspectiveTransform(src, dst) gets the transformation matrix of perspective transformation, which requires 4 points, that is, the 4 corners of the picture. 

# 透视变换
  import cv2
  import numpy as np
  
  #导入图片
  img = cv2.imread('./123.png')
  print(img.shape)
  
  src = np.float32([[100, 1100], [2100, 1100], [0, 4000], [2500, 3900]])
  dst = np.float32([[0, 0], [2300, 0], [0, 3000], [2300, 3000]])
  M = cv2.getPerspectiveTransform(src, dst)
  
  new = cv2.warpPerspective(img, M, (2300, 3000))
  cv2.namedWindow('img', cv2.WINDOW_NORMAL)
  cv2.resizeWindow('img', 640, 480)
  
  cv2.namedWindow('new', cv2.WINDOW_NORMAL)
  cv2.resizeWindow('new', 640, 480)
  
  cv2.imshow('img', img)
  cv2.imshow('new', new)
  
  
  cv2.waitKey(0)
  cv2.destroyAllWindows()

Guess you like

Origin blog.csdn.net/peng_258/article/details/132768135