自定义cv2.circle

时间:2017-09-09 06:28:54

标签: python opencv

我使用以下代码在脸上画圆圈。

for (x, y, w, h) in faces:
    cv2.circle(img, ( int((x + x + w )/2), int((y + y + h)/2 )), int (h / 2), (0, 255, 0), 5)
然而,绘制圆的厚度完全是绿色。有没有办法让圆圈的百分之几(比方说30%)变成粉红色?

1 个答案:

答案 0 :(得分:6)

正如我在评论中建议的那样,您可以使用cv2.ellipse()分别绘制两个弧。例如:

import numpy as np
import cv2

img = np.ones((400,400,3), np.uint8) * 255

# See:
#   http://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html#cv2.ellipse
#   http://docs.opencv.org/3.1.0/dc/da5/tutorial_py_drawing_functions.html

circ_center = (200,200)
circ_radius = 150
circ_thick  = 12
circ_axes   = (circ_radius,circ_radius)

# cv2.ellipse(img, center, axes, angle, startAngle, endAngle, color[, thickness[, lineType[, shift]]])    
cv2.ellipse(img, circ_center, circ_axes, 0,  0,  90, (255,0,0), circ_thick, cv2.LINE_AA)
cv2.ellipse(img, circ_center, circ_axes, 0, 90, 360, (0,255,0), circ_thick, cv2.LINE_AA)

cv2.imshow("Image", img)
cv2.imwrite("circ1.png", img)
cv2.waitKey()

产地:

Arcs1

现在,弧线具有圆形边缘。这对您来说可能是也可能不是问题。我不确定在OpenCV中是否有更好的方法,但是我用平边创建粗线的一种方法是用许多细线构建粗线。

例如:

import numpy as np
import cv2

img = np.ones((400,400,3), np.uint8) * 255

# See:
#   http://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html#cv2.ellipse
#   http://docs.opencv.org/3.1.0/dc/da5/tutorial_py_drawing_functions.html

circ_center = (200,200)
circ_radius = 150
circ_thick  = 12

def draw_arc(img, center, rad, angle, startAngle, endAngle, color, thickness, lineType, thick=1):
    for r in range(rad,rad+thickness):
        cv2.ellipse(img, center, (r,r), angle, startAngle, endAngle, color, thick, lineType)

draw_arc(img, circ_center, circ_radius, 0,  0,  90, (255,0,0), circ_thick, cv2.LINE_AA)
draw_arc(img, circ_center, circ_radius, 0, 90, 360, (0,255,0), circ_thick, cv2.LINE_AA)

cv2.imshow("Image", img)
cv2.imwrite("circ2.png", img)
cv2.waitKey()

产地:

Arcs2

您可以通过调整startAngleendAngle参数来调整着色的起点和终点。您可能需要在其中调整一些其他参数,但这应该让您了解一种方法。

你也可以画一个完整的圆圈,并在它上面加一个弧,对应你想要的颜色不同,最后可能会更容易。

相关问题