There are two types of image mirroring transformation: horizontal mirroring and vertical mirroring. Horizontal mirroring takes the vertical centerline of the image as the axis and swaps the pixels of the image, that is, swapping the left and right halves of the image. Vertical mirroring takes the horizontal centerline of the image as the axis and swaps the upper and lower parts of the image.
Transformation principle: Let the width of the image be width and the length be height. (x,y) are the transformed coordinates, (x0,y0) are the coordinates of the original image
Horizontal mirror transformation
forward mapping
Its inverse transformation is
backward mapping
Vertical mirror transformation
Its inverse transformation is
Summary:
During 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 change the image coordinate column to the right, and the right column to the left. The transformation can be done in column units. The same is true for vertical mirror transformation, which can be transformed in row units. Here we take vertical transformation as an example to see how Python is written:
Code path:muto/Samples/OpenCV/02_OpenCV Transform/04 Image mirroring.ipynb
import cv2
import numpy as np
img = cv2.imread('yahboom.jpg',1)
#cv2.imshow('src',img)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
deep = imgInfo[2]
newImgInfo = (height*2,width,deep)
dst = np.zeros(newImgInfo,np.uint8)#uint8
for 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) #BGR
xxxxxxxxxx
#bgr8 to jpeg format
import enum
import cv2
def bgr8_to_jpeg(value, quality=75):
return bytes(cv2.imencode('.jpg', value)[1])
xxxxxxxxxx
import ipywidgets.widgets as widgets
image_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 output
display(image_widget1)
#display(image_widget2)
image_widget1.value = bgr8_to_jpeg(dst)
You can see the mirror image from the picture.