简体   繁体   中英

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 center. I can see black triangular areas on one side of the image, and the other side does not have black areas.

How could I let the image be sheared symmetricly ?

You have to adjust your translation parameters (3rd column) to center your image. ie 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

在此处输入图片说明

After

在此处输入图片说明

Full code

import cv2
import numpy as np
import matplotlib.pyplot as plt

im = np.ones((100,100))
H, W = im.shape

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))

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM