Home  >  Article  >  Backend Development  >  Mirror implementation method in python

Mirror implementation method in python

PHPz
PHPzOriginal
2017-04-02 13:50:075506browse

Mirroring changes to the image do not change the shape of the image. There are three types of mirror transformations of images: horizontal mirroring, vertical mirroring, and diagonal mirroring

Suppose the size of the image is M×N, then

Horizontal mirroring can be done according to the formula

I = i

J = N - j + 1

The vertical mirror can be calculated according to the formula

I = M - i + 1

J = j

The diagonal mirror can be calculated according to the formula

I = M - i + 1

J = N - j + 1

It is worth noting that in OpenCV The coordinates start from [0,0]

So, the +1 in the formula needs to be changed to -1 when programming

#The running environment here is:

Python is: Python2.7.6
OpenCV2.4.10 version
numpy is: numpy-1.9.1-win32-superpack-python2.7

The following The code still takes baby pictures as an example. The specific procedure is as follows:

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)

Mirror implementation method in python

The above is the detailed content of Mirror implementation method in python. For more information, please follow other related articles on the PHP Chinese website!

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