图像的镜像变化不改变图像的形状。图像的镜像变换分为三种:水平镜像、垂直镜像、对角镜像
设图像的大小为M×N,则
水平镜像可按公式
I = i
J = N - j + 1
垂直镜像可按公式
I = M - i + 1
J = j
对角镜像可按公式
I = M - i + 1
J = N - j + 1
值得注意的是在OpenCV中坐标是从[0,0]开始的
所以,式中的 +1 在编程时需要改为 -1
具体程序如下:
1
import
cv
2
3 image = cv.LoadImage( ' lena.jpg ' , 1 )
4 size = (image.width,image.height)
5 iUD = cv.CreateImage(size,image.depth,image.nChannels)
6 iLR = cv.CreateImage(size,image.depth,image.nChannels)
7 iAcross = cv.CreateImage(size,image.depth,image.nChannels)
8 h = image.height
9 w = image.width
10 for i in range(h):
11 for j in range(w):
12 iUD[h - 1 - i,j] = image[i,j]
13 iLR[i,w - 1 - j] = image[i,j]
14 iAcross[h - 1 - i,w - 1 - j] = image[i,j]
15 cv.ShowImage( ' image ' ,image)
16 cv.ShowImage( ' iUD ' ,iUD)
17 cv.ShowImage( ' iLR ' ,iLR)
18 cv.ShowImage( ' iAcross ' ,iAcross)
19 cv.WaitKey(0)
2
3 image = cv.LoadImage( ' lena.jpg ' , 1 )
4 size = (image.width,image.height)
5 iUD = cv.CreateImage(size,image.depth,image.nChannels)
6 iLR = cv.CreateImage(size,image.depth,image.nChannels)
7 iAcross = cv.CreateImage(size,image.depth,image.nChannels)
8 h = image.height
9 w = image.width
10 for i in range(h):
11 for j in range(w):
12 iUD[h - 1 - i,j] = image[i,j]
13 iLR[i,w - 1 - j] = image[i,j]
14 iAcross[h - 1 - i,w - 1 - j] = image[i,j]
15 cv.ShowImage( ' image ' ,image)
16 cv.ShowImage( ' iUD ' ,iUD)
17 cv.ShowImage( ' iLR ' ,iLR)
18 cv.ShowImage( ' iAcross ' ,iAcross)
19 cv.WaitKey(0)
运行结果如下:
好啦。是不是很简单呢