Updated April 10, 2023
Introduction to OpenCV remap
Sometimes there arises a necessity to remove the pixels in the image from one place and replace them in a different position in the new image, this process of replacing the pixels in one image to a different place in the new image is called remapping in OpenCV. And to perform remapping of images, the locations of non-integer pixels in the image must be interpolated and in order to perform remapping of images in OpenCV, we make use of a function called remap() function using which the image can be resized using interpolation, flipped horizontally and vertically and can be turned upside down as well.
Syntax to define remap() function in OpenCV:
remap(source_image, map_x, map_y, interpolation_flag)
Where,
- source_image is the image that is to be remapped.
- map_x represents the mapping matrix in x direction.
- map_y represents the mapping matrix in y direction.
- interpolation_flag is the interpolation flag that is to set to resize the source image.
Working of remap() Function in OpenCV
- The process of replacing the pixels from one location in a given image to a different location in a new image is called remapping in OpenCV.
- In order to replace the pixels from one location in a given image to a different location in a new image, we make use of a function called remap() function in OpenCV.
- Before using the remap() function, the corresponding mapping matrices must be updated.
- The mapping matrices can be updated to either turn the source image upside down in the new image or flip the source image horizontally or vertically in the new image.
- Once the mapping matrices are updated, the source image is read and passed to the remap function along with mapping matrix in x direction and y direction
- The remap() function performs the remapping and returns the remapped image as the output.
Examples of OpenCV remap
Given below are the examples of OpenCV remap:
Example #1
OpenCV program in python to demonstrate remap() function and display the resulting images as the output on the screen.
Code:
#importing all the necessary modules
from __future__ import print_function
import cv2 as cv
import numpy as np
import argparse
#defining the function find_map which reduces the size of the input image to half and then turn the image upside down, left to right and right to left
def find_map(index, mapx, mapy):
if index == 0:
for a in range(mapx.shape[0]):
for b in range(mapx.shape[1]):
if b > mapx.shape[1]*0.25 and b < mapx.shape[1]*0.75 and a > mapx.shape[0]*0.25 and a < mapx.shape[0]*0.75:
mapx[a,b] = 2 * (b-mapx.shape[1]*0.25) + 0.5
mapy[a,b] = 2 * (a-mapy.shape[0]*0.25) + 0.5
else:
mapx[a,b] = 0
mapy[a,b] = 0
elif index == 1:
for a in range(mapx.shape[0]):
mapx[a,:] = [x for x in range(mapx.shape[1])]
for b in range(mapy.shape[1]):
mapy[:,b] = [mapy.shape[0]-y for y in range(mapy.shape[0])]
elif index == 2:
for a in range(mapx.shape[0]):
mapx[a,:] = [mapx.shape[1]-x for x in range(mapx.shape[1])]
for b in range(mapy.shape[1]):
mapy[:,b] = [y for y in range(mapy.shape[0])]
elif index == 3:
for a in range(mapx.shape[0]):
mapx[a,:] = [mapx.shape[1]-x for x in range(mapx.shape[1])]
for b in range(mapy.shape[1]):
mapy[:,b] = [mapy.shape[0]-y for y in range(mapy.shape[0])]
#using ArgumentParser() function to read the input image that is to be remapped
parser = argparse.ArgumentParser(description='OpenCV remap')
parser.add_argument('--input', help='Path to input image.')
args = parser.parse_args()
#reading the input image using imread() function
imageread = cv.imread(cv.samples.findFile(args.input), cv.IMREAD_COLOR)
if imageread is None:
print('Could not open or find the image: ', args.input)
exit(0)
#defining the window in which the remapped images are to be displayed and applying find_map() function on the source image to remap the image and displaying it as the output on the screen
mapx = np.zeros((imageread.shape[0], imageread.shape[1]), dtype=np.float32)
mapy = np.zeros((imageread.shape[0], imageread.shape[1]), dtype=np.float32)
window_name = 'Remapped_Image'
cv.namedWindow(window_name)
index = 0
while True:
find_map(index, mapx, mapy)
index = (index + 1) % 4
resultimage = cv.remap(imageread, mapx, mapy, cv.INTER_LINEAR)
cv.imshow(window_name, resultimage)
c = cv.waitKey(1000)
if c == 27:
break
Output:
In the above program, we are importing the required modules. Then we are defining the function find_map which reduces the size of the input image to half and then turn the image upside down, left to right and right to left. Then we are using ArgumentParser() function to read the input image that is to be remapped. Then we are reading the input image using imread() function. Then we are defining the window in which the remapped images are to be displayed and applying find_map() function on the source image to remap the image and displaying it as the output on the screen.
Example #2
OpenCV program in python to demonstrate remap() function and display the resulting images as the output on the screen.
Code:
#importing all the necessary modules
from __future__ import print_function
import cv2 as cv
import numpy as np
import argparse
#defining the function find_map which reduces the size of the input image to half and then turn the image upside down, left to right and right to left
def find_map(index, mapx, mapy):
if index == 0:
for a in range(mapx.shape[0]):
for b in range(mapx.shape[1]):
if b > mapx.shape[1]*0.25 and b < mapx.shape[1]*0.75 and a > mapx.shape[0]*0.25 and a < mapx.shape[0]*0.75:
mapx[a,b] = 2 * (b-mapx.shape[1]*0.25) + 0.5
mapy[a,b] = 2 * (a-mapy.shape[0]*0.25) + 0.5
else:
mapx[a,b] = 0
mapy[a,b] = 0
elif index == 1:
for a in range(mapx.shape[0]):
mapx[a,:] = [x for x in range(mapx.shape[1])]
for b in range(mapy.shape[1]):
mapy[:,b] = [mapy.shape[0]-y for y in range(mapy.shape[0])]
elif index == 2:
for a in range(mapx.shape[0]):
mapx[a,:] = [mapx.shape[1]-x for x in range(mapx.shape[1])]
for b in range(mapy.shape[1]):
mapy[:,b] = [y for y in range(mapy.shape[0])]
elif index == 3:
for a in range(mapx.shape[0]):
mapx[a,:] = [mapx.shape[1]-x for x in range(mapx.shape[1])]
for b in range(mapy.shape[1]):
mapy[:,b] = [mapy.shape[0]-y for y in range(mapy.shape[0])]
#using ArgumentParser() function to read the input image that is to be remapped
parser = argparse.ArgumentParser(description='OpenCV remap')
parser.add_argument('--input', help='Path to input image.')
args = parser.parse_args()
#reading the input image using imread() function
imageread = cv.imread(cv.samples.findFile(args.input), cv.IMREAD_COLOR)
if imageread is None:
print('Could not open or find the image: ', args.input)
exit(0)
#defining the window in which the remapped images are to be displayed and applying find_map() function on the source image to remap the image and displaying it as the output on the screen
mapx = np.zeros((imageread.shape[0], imageread.shape[1]), dtype=np.float32)
mapy = np.zeros((imageread.shape[0], imageread.shape[1]), dtype=np.float32)
window_name = 'Remapped_Image'
cv.namedWindow(window_name)
index = 0
while True:
find_map(index, mapx, mapy)
index = (index + 1) % 4
resultimage = cv.remap(imageread, mapx, mapy, cv.INTER_LINEAR)
cv.imshow(window_name, resultimage)
c = cv.waitKey(1000)
if c == 27:
break
Output:
In the above program, we are importing the required modules. Then we are defining the function find_map which reduces the size of the input image to half and then turn the image upside down, left to right and right to left. Then we are using ArgumentParser() function to read the input image that is to be remapped. Then we are reading the input image using imread() function. Then we are defining the window in which the remapped images are to be displayed and applying find_map() function on the source image to remap the image and displaying it as the output on the screen.
Recommended Articles
We hope that this EDUCBA information on “OpenCV remap” was beneficial to you. You can view EDUCBA’s recommended articles for more information.