Python 在OpenCV中绘制扭曲的圆

Python 在OpenCV中绘制扭曲的圆,python,opencv,Python,Opencv,我试图在足球场图像上画一个圆圈。 首先,我在图像中的一些点与现实生活中的一些硬编码参考点之间创建单应性。 然后,我画了一个圆覆盖(空白)的图像,这是扭曲的说,单应性 overlay = np.zeros((height, width, 3), np.uint8) cv2.circle(overlay, center, radius, (0,0,255), 1, lineType=cv2.LINE_AA) overlay = cv2.warpPerspective(overlay, homogra

我试图在足球场图像上画一个圆圈。
首先,我在图像中的一些点与现实生活中的一些硬编码参考点之间创建单应性。
然后,我画了一个圆覆盖(空白)的图像,这是扭曲的说,单应性

overlay = np.zeros((height, width, 3), np.uint8)
cv2.circle(overlay, center, radius, (0,0,255), 1, lineType=cv2.LINE_AA)
overlay = cv2.warpPerspective(overlay, homography, (image.shape[1], image.shape[0]))
然而,结果是扭曲的圆远不是尖锐的

我尝试过增加叠加图像的尺寸(调整中心和半径),然后在绘制圆后重新调整大小。它起作用了,但没有产生任何实质性的改进


如何使其看起来更清晰?

您需要更改插值标志,如果使用“最近的”,则插值标志将更清晰

overlay = cv2.warpPerspective(overlay, homography, (image.shape[1], image.shape[0]), cv2.INTER_NEAREST)
在ipython中获取文档,如下所示:

cv2.warpPerspective?
Docstring:
warpPerspective(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]]) -> dst
.   @brief Applies a perspective transformation to an image.
.
.   The function warpPerspective transforms the source image using the specified matrix:
.
.   \f[\texttt{dst} (x,y) =  \texttt{src} \left ( \frac{M_{11} x + M_{12} y + M_{13}}{M_{31} x + M_{32} y + M_{33}} ,
.        \frac{M_{21} x + M_{22} y + M_{23}}{M_{31} x + M_{32} y + M_{33}} \right )\f]
.
.   when the flag #WARP_INVERSE_MAP is set. Otherwise, the transformation is first inverted with invert
.   and then put in the formula above instead of M. The function cannot operate in-place.
.
.   @param src input image.
.   @param dst output image that has the size dsize and the same type as src .
.   @param M \f$3\times 3\f$ transformation matrix.
.   @param dsize size of the output image.
.   @param flags combination of interpolation methods (#INTER_LINEAR or #INTER_NEAREST) and the
.   optional flag #WARP_INVERSE_MAP, that sets M as the inverse transformation (
.   \f$\texttt{dst}\rightarrow\texttt{src}\f$ ).
.   @param borderMode pixel extrapolation method (#BORDER_CONSTANT or #BORDER_REPLICATE).
.   @param borderValue value used in case of a constant border; by default, it equals 0.
.
.   @sa  warpAffine, resize, remap, getRectSubPix, perspectiveTransform
Type:      builtin_function_or_method

另一件可能有帮助的事情是注意观察图像时使用的插值。如果使用matplotlib,可以明确告诉它要使用哪种插值;如果使用常规图像查看器,则可能无法设置插值。更多像素将使抗锯齿“看起来”更好(如您所述),但如果放大,您仍会看到一些瑕疵。下面是一个更完整的示例

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

height = 600
width = 600
center = (300, 300)
radius = 40
image = np.zeros((720, 1080, 3), np.uint8)
homography = np.array([[2, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=np.float32)
overlay = np.zeros((height, width, 3), np.uint8)
cv2.circle(overlay, center, radius, (0,0,255), 1, lineType=cv2.LINE_4)
output = cv2.warpPerspective(overlay, homography, (image.shape[1], image.shape[0]), cv2.INTER_NEAREST)

plt.imshow(overlay, interpolation='nearest')
plt.figure()
plt.imshow(output, interpolation='nearest')

您可能还想更改您的线路类型:很抱歉,它不起作用。不过,我确实设法通过增加空白图像的尺寸而使其更好,而不必调整大小,迫使我用更大的图像计算单应性。