Welcome to DOFBOT-JetsonNANO repository
9.Picture mirroring
Picture mirroring
There are two types of image mirroring: horizontal mirroring and vertical mirroring.
Horizontal mirroring uses the vertical centerline of the image as the axis to swap the pixels of the image, that is, swap the left and right halves of the image.
Vertical mirroring uses the horizontal centerline of the image as the axis, that is, swap the upper and lower halves of the image.
Transformation principle: Let the width of the image be width and the length be height. (x,y) is the coordinate after transformation, (x0,y0) is the coordinate of the original image.
Horizontal mirror transformation
Forward Mapping
Its inverse transformation is
Backward Mapping
Vertical mirror transformation

Its inverse transformation is

Summary:
During the horizontal mirror transformation, the entire image is traversed, and then each pixel is processed according to the mapping relationship.
In fact, the horizontal mirror transformation is to swap the image coordinate column to the right and the right column to the left, and the transformation can be done in columns.
The same is true for the vertical mirror transformation, which can be transformed in rows.
Here we take the vertical transformation as an example and check the code.
Code path:
x~/dofbot_ws/src/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 is shown above.