python - How can I draw half circle in OpenCV? -
how can draw half circle in opencv below?
if not, how can in python?
here's 2 ways how half circle cv2 using python. both examples complete , runnable example scripts
first easier example: creating half circle mentioned rounded corners
import cv2 import numpy 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 color in bgr""" image = np.zeros((height, width, 3), np.uint8) # fill image 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 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 color in bgr""" image = np.zeros((height, width, 3), np.uint8) # fill image 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 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:
Comments
Post a Comment