设原图像高度为 f H f_H fH,宽度为 f W f_W fW。
设原始图像的任意点 P 0 ( x 0 , y 0 ) P_0(x_0, y_0) P0(x0,y0),沿水平( x x x 方向)镜像后到新的位置 P ( x , y ) P(x,y) P(x,y),水平镜像不改变 y y y 坐标。其变换式为
{ x = f W − x 0 y = y 0 \left\{ \begin{matrix} x = f_W - x_0 \\ y = y_0 \end{matrix} \right. { x=fW−x0y=y0
矩阵表达式为:
[ x y 1 ] = [ − 1 0 f W 0 1 0 0 0 1 ] [ x 0 y 0 1 ] \left[\begin{matrix} x\\ y \\ 1 \end{matrix}\right] =\left[\begin{matrix} -1 & 0 & f_W\\ 0 & 1 & 0\\ 0 & 0 & 1 \end{matrix}\right] \left[\begin{matrix} x_0\\ y_0 \\ 1 \end{matrix}\right] ⎣⎡xy1⎦⎤=⎣⎡−100010fW01⎦⎤⎣⎡x0y01⎦⎤
设原始图像的任意点 P 0 ( x 0 , y 0 ) P_0(x_0, y_0) P0(x0,y0),沿垂直( y y y 方向)镜像后到新的位置 P ( x , y ) P(x,y) P(x,y),垂直镜像不改变 x x x 坐标。其变换式为
{ x = x 0 y = f H − y 0 \left\{ \begin{matrix} x = x_0 \\ y = f_H - y_0 \end{matrix} \right. { x=x0y=fH−y0
矩阵表达式为:
[ x y 1 ] = [ 1 0 0 0 − 1 f H 0 0 1 ] [ x 0 y 0 1 ] \left[\begin{matrix} x\\ y \\ 1 \end{matrix}\right] =\left[\begin{matrix} 1 & 0 & 0\\ 0 & -1 & f_H\\ 0 & 0 & 1 \end{matrix}\right] \left[\begin{matrix} x_0\\ y_0 \\ 1 \end{matrix}\right] ⎣⎡xy1⎦⎤=⎣⎡1000−100fH1⎦⎤⎣⎡x0y01⎦⎤
设原始图像的任意点 P 0 ( x 0 , y 0 ) P_0(x_0, y_0) P0(x0,y0),沿对角镜像后到新的位置 P ( x , y ) P(x,y) P(x,y)。其变换式为
{ x = f W − x 0 y = f H − y 0 \left\{ \begin{matrix} x = f_W - x_0 \\ y = f_H - y_0 \end{matrix} \right. { x=fW−x0y=fH−y0
矩阵表达式为:
[ x y 1 ] = [ − 1 0 f W 0 − 1 f H 0 0 1 ] [ x 0 y 0 1 ] \left[\begin{matrix} x\\ y \\ 1 \end{matrix}\right] =\left[\begin{matrix} -1 & 0 & f_W\\ 0 & -1 & f_H\\ 0 & 0 & 1 \end{matrix}\right] \left[\begin{matrix} x_0\\ y_0 \\ 1 \end{matrix}\right] ⎣⎡xy1⎦⎤=⎣⎡−1000−10fWfH1⎦⎤⎣⎡x0y01⎦⎤
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
img = cv.imread('pic/rabbit500x333.jpg')
# 镜像1
mirrorM = np.array([
[-1, 0, 333],
[0, 1, 0]
], dtype=np.float32)
img_mirr = cv.warpAffine(img, mirrorM, dsize=img.shape[:2][::-1])
show(img_mirr)
# 镜像2
img_mirh = cv.flip(img, 1)
img_mirv = cv.flip(img, 0)
img_mirb = cv.flip(img, -1)
show(np.hstack([img, img_mirh, img_mirv, img_mirb]))
说明: