在 Python 中使用 warpAffine 进行 OpenCV 旋转会导致错误的边界

OpenCV Rotation with warpAffine in Python Causes Erroneous Border

查看此 Python 代码:

degrees = 90
center  = (24, 24)

img     = np.ones((48,48,3)) * 255
mat     = cv2.getRotationMatrix2D(center, degrees, 1.0)
img     = cv2.warpAffine(img, mat, (48, 48))

我的期望是创建一个 3 通道、完全饱和的白色方块并将其存储在 img 中。之后,它将旋转 90 度。将一个白色方块旋转 90 度应该会导致……一个无法区分的白色方块。但是当我:

plt.imshow(img)
plt.show(img)

我看到一个错误的黑色边框:

有什么方法可以让 warpAffine 按预期工作,即旋转图像时不会出现错误的边框?我尝试了以下修改无济于事:

center  = (23, 23)
center  = (24, 23)
center  = (23, 24)
center  = (25, 25)
center  = (24, 25)
center  = (25, 24)

您应该使用图像的确切中心,而不是下一个最接近的东西。使用 (24,24).

旋转稍微偏离中心

由于 getRotationMatrix2D 接受 Point2f,您应该将中心传递为 (23.5,23.5),因为它是 0 and 47.

之间的中点