Home  >  Article  >  Backend Development  >  python图像处理之镜像实现方法

python图像处理之镜像实现方法

WBOY
WBOYOriginal
2016-06-06 11:17:332402browse

本文实例讲述了python图像处理之镜像实现方法。分享给大家供大家参考。具体分析如下:

图像的镜像变化不改变图像的形状。图像的镜像变换分为三种:水平镜像、垂直镜像、对角镜像

设图像的大小为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

这里运行环境为:

Python为:Python2.7.6
OpenCV2.4.10版(可到http://sourceforge.net/projects/opencvlibrary/files/opencv-win/下载)
numpy为:numpy-1.9.1-win32-superpack-python2.7(可到http://sourceforge.net/projects/numpy/files/NumPy/1.9.1/下载)

下面的代码仍以baby美图为例具体程序如下:

import cv2.cv as cv
image = cv.LoadImage('angelababy.jpg',1)
size = (image.width,image.height)
iUD = cv.CreateImage(size,image.depth,image.nChannels)
iLR = cv.CreateImage(size,image.depth,image.nChannels)
iAcross = cv.CreateImage(size,image.depth,image.nChannels)
h = image.height
w = image.width
for i in range(h):
  for j in range(w):
    iUD[h-1-i,j] = image[i,j]
    iLR[i,w-1-j] = image[i,j]
    iAcross[h-1-i,w-1-j] = image[i,j]
cv.ShowImage('image',image)
cv.ShowImage('iUD',iUD)
cv.ShowImage('iLR',iLR)
cv.ShowImage('iAcross',iAcross)
cv.WaitKey(0)

运行结果如下图所示:

希望本文所述对大家的Python程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn