[英]How can I draw a circle next to each other with a text in center uisng opencv in python?
[英]How can I draw half circle in OpenCV?
我怎樣才能像下面這樣在 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)
結果 :
第二個例子:創建沒有圓角的半圓
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)
結果 :
這里有一個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);
}
結果如下:
有一個ellipse
函數( 鏈接 ),您可以在其中指定起始角度和結束角度。
如果你想要一個橢圓,軸參數不應該彼此相等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.