How can I draw half circle in OpenCV?
Asked Answered
B

4

10

How can I draw half circle in OpenCV like below?

draw half circle like this in OpenCV

If not, how can I do this especially in Python?

Burseraceous answered 5/4, 2014 at 14:1 Comment(1)
Please elaborate on your question so that the community can diagnose it better. Thank you.Lagoon
L
10

Here's two ways how to do a half circle with cv2 using Python. 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:

Half circle with rounded line

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:

Half circle without rounded line

Leaving answered 7/4, 2014 at 18:49 Comment(0)
B
4

here is a C++ sample implementation for you:

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:

enter image description here

Balustrade answered 7/4, 2014 at 10:22 Comment(0)
G
2

There is an ellipse function (link) where you can specify a start angle and an end angle.

Gauche answered 5/4, 2014 at 14:47 Comment(6)
I think i just could create ellipse shape with it. not circle shape. True?Burseraceous
Well, a circle is an ellipse, so you just have to give the right parameters to obtain a circle (i.e. axes of same length).Gauche
Could you give an example parameters?Burseraceous
I'm not familiar with Python, but if you want a circle of radius R, the axes parameter should be something like (R, R) i.e. twice the radius value.Gauche
Can you implement it example in others? I could try to understand it.Burseraceous
This "answer" does nothing more than link to documentation.Springe
O
0

You can change start angle and stop angle and draw different area you need.

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)
Oxyhydrogen answered 23/11, 2023 at 10:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.