如何在OpenCV中绘制半圆?

9
我该如何在OpenCV中画出像下面这样的半圆形? draw half circle like this in OpenCV 如果不能,那么我应该如何做到这一点,尤其是在Python中?

请详细阐述您的问题,以便社区能够更好地诊断。谢谢。 - sshashank124
4个回答

9
这是两种使用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)

结果:

带有圆角的半圆形

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

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)

结果:

没有圆弧线的半圆形


(注:该文本为中英双语对照,已翻译完成)

4

这是一个C++的示例实现:

int main()
{
cv::Mat outImage = cv::Mat(256,256,CV_8UC3, cv::Scalar(255,255,255));

cv::Point center(128,128);
int radius = 100;

//draw upright black halfcircle
cv::Scalar colorBlack = cv::Scalar(0,0,0);
double startAngleUpright = 180;
cv::ellipse(outImage,center,cv::Size(radius,radius),0,startAngleUpright,startAngleUpright+180,colorBlack,8,0);

//draw downright red halfcircle
cv::Scalar colorRed = cv::Scalar(0,0,255);
double startAngleDownright = 0;
cv::ellipse(outImage,center,cv::Size(radius,radius),0,startAngleDownright,startAngleDownright+180,colorRed,8,0);

cv::imshow("outImage", outImage);
cv::imwrite("DrawHalfCircle.png", outImage);

cv::waitKey(-1);
}

使用此结果:
使用这个结果: enter image description here

2

有一个椭圆函数 (链接),可以指定起始角度和结束角度。


我认为我只能用它创建椭圆形状,而不是圆形状。对吗? - Yuda Prawira
一个圆是一个椭圆,所以你只需要给出正确的参数来获得一个圆(即两个轴具有相同的长度)。 - BConic
你能给一个参数的例子吗? - Yuda Prawira
我不熟悉Python,但如果你想要一个半径为R的圆形,axes参数应该是类似于(R, R)这样的东西,即两倍的半径值。 - BConic
你能在其他地方实现这个例子吗?我可以尝试理解它。 - Yuda Prawira
1
这个“答案”除了链接到文档之外什么都没有做。 - Bort

0
您可以更改起始角度和停止角度,并绘制您所需的不同区域。
import cv2
import numpy

img=numpy.zeros((100,100))

radius=5
axes = (radius,radius)
angle=0;
startAngle=0;
endAngle=180;
center=(50,50)
color=255

cv2.ellipse(img, center, axes, angle, startAngle, endAngle, color)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接