简体   繁体   English

如何在 OpenCV 中绘制半圆?

[英]How can I draw half circle in OpenCV?

How can I draw half circle in OpenCV like below?我怎样才能像下面这样在 OpenCV 中画半圆?

在 OpenCV 中像这样画半圆

If not, how can I do this especially in Python?如果没有,我该怎么做,尤其是在 Python 中?

Here's two ways how to do a half circle with cv2 using Python. 以下是使用Python使用cv2进行半圈的两种方法。 Both examples are complete and runnable example scripts 这两个示例都是完整且可运行的示例脚本

First easier example: Creating half circle like you mentioned but with rounded corners 第一个更简单的例子:像你提到的那样创建半圆但是有圆角

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)

Result : 结果

半圆圆线

Second example: Creating half circle without rounded corners 第二个例子:创建没有圆角的半圆

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)

Result : 结果

半圆没有圆线

here is a C++ sample implementation for you: 这里有一个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);
}

with this result: 结果如下:

在此输入图像描述

有一个ellipse函数( 链接 ),您可以在其中指定起始角度和结束角度。

If you want to a ellipse, axes parameters should not be equal each other.如果你想要一个椭圆,轴参数不应该彼此相等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM