What is remapping
The process of placing pixels in one image to a specified position in another image is called remapping. To make it simple, it’s just copying an image into another image.
In OpenCV, it gives us the cv2.remap() function as a remap, which is defined as follows:
def remap(src, map1, map2, interpolation, dst=None, borderMode=None, borderValue=None) :
Copy the code
SRC: represents the original image
Map1: can represent a mapping of (x,y) points or the x values of CV_16SC2, CV_32FC1, CV_32FC2 type (x,y) points
Map2: Null when map1 represents a mapping of points (x,y). When map1 represents the x value of CV_16SC2, CV_32FC1, and CV_32FC2 (x,y) points, this value is the Y value of CV_16UC1 and CV_32FC1 (x,y) points.
Interpolation, borderMode and borderValue are similar.
Note that map1 refers to the column number where the pixel is located, and map2 refers to the line number where the pixel is located.
Copy pixels
Now let’s assume that there is a requirement to map all the pixels in the target image to the pixels on row 100, column 200 in the original image. The specific implementation is as follows:
import cv2
import numpy as np
img = cv2.imread("4.jpg")
rows, cols, ch = img.shape
mapx = np.ones(img.shape[:2], np.float32) * 200
mapy = np.ones(img.shape[:2], np.float32) * 100
result_img = cv2.remap(img, mapx, mapy, cv2.INTER_LINEAR)
cv2.imshow("img", img)
cv2.imshow("result_img", result_img)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code
As shown in the code above, we set all pixels to the pixels of the original image (100,200) points, and we get a very solid color image that looks like this:
Copy the entire image
So if you can copy a pixel, you can certainly copy the whole image. Below, we copy all the images on the left of the above image to the right. The specific code is as follows:
import cv2
import numpy as np
img = cv2.imread("4.jpg")
rows, cols, ch = img.shape
mapx = np.ones(img.shape[:2], np.float32)
mapy = np.ones(img.shape[:2], np.float32)
for i in range(rows):
for j in range(cols):
mapx.itemset((i,j),j)Set the Y coordinate of the original map for each point
mapy.itemset((i,j),i)Set the X coordinate of the original map for each point
result_img = cv2.remap(img, mapx, mapy, cv2.INTER_LINEAR)
cv2.imshow("img", img)
cv2.imshow("result_img", result_img)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code
Here, we map all the points to all the points, and each pixel corresponds to one by one, and then copy the original image. After running, the effect looks like this:
Flip it around the X-axis
With the function cv2.remap(), we can not only remap the pixel points, but also reverse the mapping, that is, we can realize the X-axis reversal effect through it, as long as the X-axis is unchanged and the y-coordinate value is exchanged with the X-axis as the symmetry.
Modify a line of the above code as follows:
import cv2
import numpy as np
img = cv2.imread("4.jpg")
rows, cols, ch = img.shape
mapx = np.ones(img.shape[:2], np.float32)
mapy = np.ones(img.shape[:2], np.float32)
for i in range(rows):
for j in range(cols):
mapx.itemset((i,j),j)
mapy.itemset((i,j),rows-1-i)# Modify this line
result_img = cv2.remap(img, mapx, mapy, cv2.INTER_LINEAR)
cv2.imshow("img", img)
cv2.imshow("result_img", result_img)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code
After running, we get an image that is flipped around the X-axis. The effect is as follows:
Flip it around the Y-axis
Since we can flip around X with the cv2.remap() function, we can certainly flip around the Y-axis by swapping the x-coordinate values for symmetry on the Y-axis.
Without further ado, go straight to the code:
import cv2
import numpy as np
img = cv2.imread("4.jpg")
rows, cols, ch = img.shape
mapx = np.ones(img.shape[:2], np.float32)
mapy = np.ones(img.shape[:2], np.float32)
for i in range(rows):
for j in range(cols):
mapx.itemset((i,j),cols-1-j)# Modify this line
mapy.itemset((i,j),i)#
result_img = cv2.remap(img, mapx, mapy, cv2.INTER_LINEAR)
cv2.imshow("img", img)
cv2.imshow("result_img", result_img)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code
After running, we get the image flipped around the Y-axis:
You flip it around the XY axis
How about flipping around the XY axis? Here two lines of code change together:
import cv2
import numpy as np
img = cv2.imread("4.jpg")
rows, cols, ch = img.shape
mapx = np.ones(img.shape[:2], np.float32)
mapy = np.ones(img.shape[:2], np.float32)
for i in range(rows):
for j in range(cols):
mapx.itemset((i,j),cols-1-j)
mapy.itemset((i,j),rows-1-i)
result_img = cv2.remap(img, mapx, mapy, cv2.INTER_LINEAR)
cv2.imshow("img", img)
cv2.imshow("result_img", result_img)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code
After running, the effect looks like this:
Compression half
In other words, to reduce the size of the original image, press the Y axis to reduce the size of the Y axis, only need to set the X axis to double. The specific code is as follows:
import cv2
import numpy as np
img = cv2.imread("4.jpg")
rows, cols, ch = img.shape
mapx = np.ones(img.shape[:2], np.float32)
mapy = np.ones(img.shape[:2], np.float32)
for i in range(rows):
for j in range(cols):
mapx.itemset((i,j),j)
mapy.itemset((i,j),2*i)# Modify this line of code
result_img = cv2.remap(img, mapx, mapy, cv2.INTER_LINEAR)
cv2.imshow("img", img)
cv2.imshow("result_img", result_img)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code
After running, the effect is shown as follows:
Since can compress, mean also can realize narrow, narrow specific realization, can regard as training exercises, convenient everybody consolidate grasp, the blogger is here not to repeat.