Image Translation Using Numpy
I want to perform image translation by a certain amount (shift the image vertically and horizontally). The problem is that when I paste the cropped image back on the canvas, I just
Solution 1:
For image translation, you can make use of the somewhat obscure numpy.roll
function. In this example I'm going to use a white canvas so it is easier to visualize.
image = np.full_like(original_image, 255)
height, width = image.shape[:-1]
shift = 100# shift image
rolled = np.roll(image, shift, axis=[0, 1])
# black out shifted parts
rolled = cv2.rectangle(rolled, (0, 0), (width, shift), 0, -1)
rolled = cv2.rectangle(rolled, (0, 0), (shift, height), 0, -1)
If you want to flip the image so the black part is on the other side, you can use both np.fliplr
and np.flipud
.
Result:
Post a Comment for "Image Translation Using Numpy"