Welcome to DOFBOT-Ultra repository
9. Picture Mirroring
Picture Mirroring
Image mirroring is divided into two types: horizontal mirroring and vertical mirroring. Horizontal mirroring uses the vertical center line of the image as the axis to swap the pixels of the image, i.e., swapping the left and right halves. Vertical mirroring uses the horizontal center line of the image as the axis to swap the upper and lower halves.
Transformation principle: Let the width of the image be width and the height be height. (x,y) are the transformed coordinates, (x0,y0) are the original image coordinates.
Horizontal mirror transformation
Forward mapping
Its inverse transformation is:
Backward mapping
Vertical mirror transformation

Its inverse transformation is:

Summary:
In horizontal mirroring, the entire image is traversed and each pixel is processed according to the mapping relationship. In fact, horizontal mirroring swaps the columns of the image coordinates from left to right, and from right to left. It can be done at the column unit level. Similarly, vertical mirroring can be done at the row unit level. Here we take vertical transformation as an example to see how Python implements it:
Code path:
~/dofbot_opencv/scripts/2.Transform/04_mirror_image.ipynb
xxxxxxxxxximport cv2import numpy as npimg = cv2.imread('yahboom.jpg',1)#cv2.imshow('src',img)imgInfo = img.shapeheight = imgInfo[0]width = imgInfo[1]deep = imgInfo[2]newImgInfo = (height*2,width,deep)dst = np.zeros(newImgInfo,np.uint8)#uint8for i in range(0,height): for j in range(0,width): dst[i,j] = img[i,j] #x y = 2*h - y -1 dst[height*2-i-1,j] = img[i,j]for i in range(0,width): dst[height,i] = (0,0,255) #BGRxxxxxxxxxx#bgr8 to jpeg formatimport enumimport cv2def bgr8_to_jpeg(value, quality=75): return bytes(cv2.imencode('.jpg', value)[1])xxxxxxxxxximport ipywidgets.widgets as widgetsimage_widget1 = widgets.Image(format='jpg', )# image_widget2 = widgets.Image(format='jpg', )# create a horizontal box container to place the image widget next to eachother# image_container = widgets.HBox([image_widget1, image_widget2])# display the container in this cell's outputdisplay(image_widget1)#display(image_widget2)image_widget1.value = bgr8_to_jpeg(dst)
The mirror image can be seen from the figure.