简体   繁体   English

我如何使用opencv实现居中剪切图像

[英]How could I implement a centered shear an image with opencv

When I use warpAffine to shear an image: 当我使用warpAffine剪切图像时:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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