Anu*_*dha 5 python opencv ellipse opencv3.0
我正在尝试使用 Open CV 绘制圆弧,使用 cv2.ellipse 函数我尝试阅读相同的文档,但我发现它非常令人困惑。在我的例子中它是一个圆弧,所以axes_x和axes_y是相同的,即半径。我的轴应该是什么,我应该在哪个方向计算开始和结束角度?这个旋转角度是多少?给定的是函数 -
cv2.ellipse(img, center, axes, angle, startAngle, endAngle, color[, thickness[, lineType[, shift]]])
import cv2
import numpy as np
def create_blank(height, width, color):
blank_image = np.zeros((int(height), int(width), 3), np.uint8)
blank_image[:, :] = color
return blank_image
def draw_arc(image):
height, width = image.shape[0:2]
# Ellipse parameters
radius = 100
center = (width / 2, height/2)
axes = (radius, radius)
angle = 0
startAngle = 135
endAngle = 180
cv2.line(image, (0, 150), (300, 150), (0, 0, 0), 2, cv2.CV_AA)
cv2.line(image, (150, 0), (150, 300), (0, 0, 0), 2, cv2.CV_AA)
cv2.ellipse(image, center, axes, angle, startAngle, endAngle, (0, 0, 0), 2, cv2.CV_AA)
cv2.imshow("ellipse", image)
# Create new blank 300x150 white image
width, height = 300, 300
image = create_blank(width, height, color=WHITE)
draw_arc(image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)
当我的 startAngle 为 135 且 endAngle 为 180 时,结果如下
而当 startAngle 为 0 且 endAngle 为 90 时,结果如下所示

所以这让人很困惑,弧线是朝哪个方向旋转的。
api*_*i55 11
您可以非常轻松地查看参数的变化如何影响椭圆的绘制。这是一个简单的代码:
import numpy as np
import cv2
center = (200, 200) # x,y
axes = (100, 75) # first, second
angle = 0. # clockwise, first axis, starts horizontal
for i in range(360):
image = np.zeros((400, 400, 3)) # creates a black image
image = cv2.ellipse(image, center, axes, angle, 0., 360, (0,0,255))
image = cv2.ellipse(image, center, axes, angle, 0., i, (0,255,0))
cv2.imshow("image", image)
cv2.waitKey(5)
cv2.waitKey(0)
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)
这将执行类似以下操作:
让我们看一下参数:
center -> x 和 y 元组,其中椭圆的中心所在。
轴-> 第一和第二轴半径(总大小的一半)。如果应用角度 0,第一个是水平的,第二个将是垂直的。
angle -> 整个椭圆的角度,即如果顺时针移动第一个轴
startAngle -> 您想要开始绘制弧线的位置,例如 0 将像我的示例图像(在第一个轴中)一样,但如果角度有一个值,则 0 将以相同的方式旋转。
endAngle -> 在您想要停止绘制的位置,您可以看到我在示例中改变了它以绘制一个递增的椭圆。
如果你想要一个半径为 50px 的圆弧,比如说从 60 度到 120 度,但是逆时针(360 - 开始/结束角度)你可以这样做:
image = cv2.ellipse(image, (100,100), (50,50), 0.0, 360-120, 360-60, (0,255,0))
Run Code Online (Sandbox Code Playgroud)
如果您对其中任何一个有疑问,请随时在评论中提问
| 归档时间: |
|
| 查看次数: |
9620 次 |
| 最近记录: |