图像镜像变换公式为:
水平镜像:{x∗=M−1−xy∗=y水平镜像: \left\{
\begin{matrix}
x^* & = & M - 1 - x \\
y^* & = & y
\end{matrix}
\right.
水平镜像:{x∗y∗==M−1−xy
垂直镜像:{x∗=xy∗=M−1−y垂直镜像: \left\{
\begin{matrix}
x^* & = & x \\
y^* & = & M - 1 -y
\end{matrix}
\right.
垂直镜像:{x∗y∗==xM−1−y
对角镜像:{x∗=M−1−xy∗=M−1−y对角镜像: \left\{
\begin{matrix}
x^* & = & M - 1 -x \\
y^* & = & M - 1 -y
\end{matrix}
\right.
对角镜像:{x∗y∗==M−1−xM−1−y
用矩阵表示为:
水平镜像:{x∗y∗1}={−10M−1010001}{xy1}
水平镜像: \left\{
\begin{matrix}
x^* \\
y^* \\
1
\end{matrix}
\right\}=
\left\{
\begin{matrix}
-1 & 0 & M- 1 \\
0 & 1 & 0 \\
0 & 0 & 1
\end{matrix}
\right\}
\left\{
\begin{matrix}
x \\
y \\
1
\end{matrix}
\right\}
水平镜像:⎩⎨⎧x∗y∗1⎭⎬⎫=⎩⎨⎧−100010M−101⎭⎬⎫⎩⎨⎧xy1⎭⎬⎫
垂直镜像:{x∗y∗1}={1000−1N−1001}{xy1} 垂直镜像: \left\{ \begin{matrix} x^* \\ y^* \\ 1 \end{matrix} \right\}= \left\{ \begin{matrix} 1 & 0 & 0 \\ 0 & -1 & N - 1 \\ 0 & 0 & 1 \end{matrix} \right\} \left\{ \begin{matrix} x \\ y \\ 1 \end{matrix} \right\} 垂直镜像:⎩⎨⎧x∗y∗1⎭⎬⎫=⎩⎨⎧1000−100N−11⎭⎬⎫⎩⎨⎧xy1⎭⎬⎫
对角镜像:{x∗y∗1}={−10M−10−1N−1001}{xy1} 对角镜像: \left\{ \begin{matrix} x^* \\ y^* \\ 1 \end{matrix} \right\}= \left\{ \begin{matrix} -1 & 0 & M- 1 \\ 0 & -1 & N-1 \\ 0 & 0 & 1 \end{matrix} \right\} \left\{ \begin{matrix} x \\ y \\ 1 \end{matrix} \right\} 对角镜像:⎩⎨⎧x∗y∗1⎭⎬⎫=⎩⎨⎧−1000−10M−1N−11⎭⎬⎫⎩⎨⎧xy1⎭⎬⎫
0. 待镜像处理的原图像
1. 不使用MATLAB自带函数
clc,clear,close all;
Image = imread('flower.jpg');
[h, w, c] = size(Image);
deltax= 20;deltay = 40;
T1 = maketform('affine', [-1 0 0;0 1 0;w - 1 0 1]);
NewImage1 = imtransform(Image, T1, 'XData', [1 size(Image, 2)], 'YData', [1 size(Image, 1)], 'FillValue', 255);
T2 = maketform('affine', [1 0 0;0 -1 0;0 h - 1 1]);
NewImage2 = imtransform(Image, T2, 'XData', [1 size(Image, 2)], 'YData', [1 size(Image, 1)], 'FillValue', 255);
T3 = maketform('affine', [-1 0 0;0 -1 0;w - 1 h - 1 1]);
NewImage3 = imtransform(Image, T3, 'XData', [1 size(Image, 2)], 'YData', [1 size(Image, 1)], 'FillValue', 255);
subplot(221), imshow(Image), title('原图');
subplot(222), imshow(NewImage1), title('水平镜像');
subplot(223), imshow(NewImage2), title('垂直镜像');
subplot(224), imshow(NewImage3), title('对角镜像');
2. 使用MATLAB自带函数
% 沿维度 dim 反转 A 中元素的顺序。
B = flip(A,dim)
flip(A,1) 将反转每一列中的元素,flip(A,2) 将反转每一行中的元素
Image = imread('flower.jpg');
subplot(221), imshow(Image), title('原图');
Image1 = flip(Image, 2);subplot(222), imshow(Image1), title('水平镜像');
Image2 = flip(Image, 1);subplot(223), imshow(Image2), title('垂直镜像');
Image3 = flip(Image1, 1);subplot(224), imshow(Image3), title('对角镜像');