Skip to content Skip to sidebar Skip to footer

How Could I Implement A Centered Shear An Image With Opencv

When I use warpAffine to shear an image: M2 = np.float32([[1, 0, 0], [0.2, 1, 0]]) aff2 = cv2.warpAffine(im, M2, (W, H)) I obtain an image that is not sheared around the image c

Solution 1:

You have to adjust your translation parameters (3rd column) to center your image. i.e. you have to translate half the width and height multiplied by a factor.

For example

M2 = np.float32([[1, 0, 0], [0.2, 1, 0]])
M2[0,2] = -M2[0,1] * W/2
M2[1,2] = -M2[1,0] * H/2
aff2 = cv2.warpAffine(im, M2, (W, H))

Before

enter image description here

After

enter image description here

Full code

import cv2
import numpy as np
import matplotlib.pyplot as pltim= np.ones((100,100))
H, W = im.shapeM2= np.float32([[1, 0, 0], [0.2, 1, 0]])
M2[0,2] = -M2[0,1] * W/2
M2[1,2] = -M2[1,0] * H/2
aff2 = cv2.warpAffine(im, M2, (W, H))

plt.imshow(aff2, cmap="gray")

Post a Comment for "How Could I Implement A Centered Shear An Image With Opencv"