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

标签 python opencv image-processing affinetransform

当我使用warpAffine剪切图像时:

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

我获得的图像在图像中心周围没有被剪切。我可以在图像的一侧看到黑色三角形区域,而另一侧没有黑色区域。

如何让图像对称地剪切?

最佳答案

您必须调整翻译参数(第3列)以使图像居中。即您必须翻译宽度和高度乘以系数的一半。

例如

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

之前

enter image description here



enter image description here

完整代码
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")

关于python - 我如何使用opencv实现居中剪切图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57881430/

相关文章:

python - 在python中读取csv文件时跳过第二行数据帧

image - 使用 GPU (CUDA) 时 opencv 中的像素访问

image-processing - Opencv特征提取与图像匹配

google-chrome - 具有大量 html 的 Chrome 性能缓慢/糟糕

python - Pyinstaller 构建应用程序后设计变得丑陋

python - OpenCV 图像减法 vs Numpy 减法

c++ - opencv重写一个压缩视频文件

java - PNG 到位图文件转换

image - 定位系统中相机倾斜的程序校正

python - train_test_split 中的 X 参数是没有目标类的整个数据还是也包含目标类?