I am trying to translate an image (from a video capture) 100px left by cutting off the 100px on the left and adding 100px of black pixels on the right so that the image stays the same size. I know this can be done with cv2.warpAffine() and a translation matrix, but doing this to each frame is adding high amounts of latency. I have read that there may be a way to do this using cv2.copyTo(), but I am still not sure how exactly this can be done, whether it is using copyTo or another method. Thanks!
This is done in Python 3 with OpenCV 4.1
Current (slow) method:
translation_matrix = np.float32([[1, 0, x_shift], [0, 1, y_shift]])
img = cv2.warpAffine(img, translation_matrix, (w, h))
OpenCV-Python OpenCV provides a function cv2. warpAffine() that applies an affine transformation to an image. You just need to provide the transformation matrix (M). The basic syntax for the function is given below.
Image-to-image translation is the process of transforming an image from one domain to another, where the goal is to learn the mapping between an input image and an output image.
Create a new image that is the same size as the old image, but index into your old image such that you start indexing column-wise at 100 pixels. After, make sure you assign these pixels such that we start at the first column in the new image but going up to the 100th last column.
import cv2
import numpy as np
# Define your image somewhere...
# ...
# ...
img2 = np.zeros_like(img)
img2[:,:-100] = img[:,100:]
The above code creates a new image called img2
that is initialised to all zeroes that is the same size as the original input image img
. After, we copy pixels starting from column 100 of the original image to the end, then assign this to all columns in the target image from the first column up until the last 100. The rest of the columns afterwards will still be all zero which effectively gives you black pixels.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With