我如何使用opencv实现居中剪切图像 [英] How could I implement a centered shear an image with opencv

查看:73
本文介绍了我如何使用opencv实现居中剪切图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用warpAffine剪切图像时:

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 ?

推荐答案

您必须调整翻译参数(第3列)以使图像居中.也就是说,您必须翻译宽度和高度乘以系数的一半.

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.

例如

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

之前

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

这篇关于我如何使用opencv实现居中剪切图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆