gpt4 book ai didi

python - 如何在 OpenCV 中绘制半圆?

转载 作者:太空宇宙 更新时间:2023-11-03 12:39:47 25 4
gpt4 key购买 nike

如何在 OpenCV 中绘制半圆,如下所示?

draw half circle like this in OpenCV

如果没有,我该如何做,尤其是在 Python 中?

最佳答案

这里有两种使用 Python 使用 cv2 做半圆的方法。这两个示例都是完整的可运行示例脚本

第一个更简单的例子:像你提到的那样创建半圆,但有圆角

import cv2
import numpy as np

# Colors (B, G, R)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)


def create_blank(width, height, color=(0, 0, 0)):
"""Create new image(numpy array) filled with certain color in BGR"""
image = np.zeros((height, width, 3), np.uint8)
# Fill image with color
image[:] = color

return image


def draw_half_circle_rounded(image):
height, width = image.shape[0:2]
# Ellipse parameters
radius = 100
center = (width / 2, height - 25)
axes = (radius, radius)
angle = 0
startAngle = 180
endAngle = 360
thickness = 10

# http://docs.opencv.org/modules/core/doc/drawing_functions.html#ellipse
cv2.ellipse(image, center, axes, angle, startAngle, endAngle, BLACK, thickness)


# Create new blank 300x150 white image
width, height = 300, 150
image = create_blank(width, height, color=WHITE)
draw_half_circle_rounded(image)
cv2.imwrite('half_circle_rounded.jpg', image)

结果:

Half circle with rounded line

第二个例子:创建没有圆角的半圆

import cv2
import numpy as np

# Colors (B, G, R)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)


def create_blank(width, height, color=(0, 0, 0)):
"""Create new image(numpy array) filled with certain color in BGR"""
image = np.zeros((height, width, 3), np.uint8)
# Fill image with color
image[:] = color

return image


def draw_half_circle_no_round(image):
height, width = image.shape[0:2]
# Ellipse parameters
radius = 100
center = (width / 2, height - 25)
axes = (radius, radius)
angle = 0
startAngle = 180
endAngle = 360
# When thickness == -1 -> Fill shape
thickness = -1

# Draw black half circle
cv2.ellipse(image, center, axes, angle, startAngle, endAngle, BLACK, thickness)

axes = (radius - 10, radius - 10)
# Draw a bit smaller white half circle
cv2.ellipse(image, center, axes, angle, startAngle, endAngle, WHITE, thickness)


# Create new blank 300x150 white image
width, height = 300, 150

image = create_blank(width, height, color=WHITE)
draw_half_circle_no_round(image)
cv2.imwrite('half_circle_no_round.jpg', image)

结果:

Half circle without rounded line

关于python - 如何在 OpenCV 中绘制半圆?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22881878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com