在opencvpython中围绕图像的质心绘制一个圆

2024-09-19 20:56:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在图像中一些物体的质心周围画一个红色的圆圈(图像中的物体是一些昆虫,所以这些圆圈有助于人类视觉上检测昆虫);我已经有了质心(如下所示),但不知道如何在python/opencv中实现这一点

array([[  265.,   751.],
   [  383.,   681.],
   [  386.,   889.],
   [  434.,   490.],
   [  446.,   444.],
   [  450.,   451.],
   [  539.,  1365.],
   [  571.,  1365.],
   [  630.,   645.],
   [  721.,  1365.],
   [  767.,    70.],
   [  767.,    82.],
   [  767.,   636.]])

有人知道怎么做我想做的吗?在


Tags: 图像视觉人类arrayopencv物体红色质心
2条回答

您可以将^{}API用作:

import numpy as np
import cv2

centroids = np.array([[265., 751.],
                      [383., 681.],
                      [386., 889.],
                      [434., 490.],
                      [446., 444.],
                      [450., 451.],
                      [539., 1365.],
                      [571., 1365.],
                      [630., 645.],
                      [721., 1365.],
                      [767., 70.],
                      [767., 82.],
                      [767., 636.]])

canvas = np.ones((1000, 1000, 3), dtype=np.uint8) * 255
CIRCLE_RADIUS = 10
CIRCLE_THICKNESS = 2
COLOR_RED = np.array([0, 0, 255])

for c in centroids:
    o_c = (int(c[0]), int(c[1]))
    cv2.circle(canvas, o_c, CIRCLE_RADIUS, COLOR_RED, CIRCLE_THICKNESS)

cv2.imwrite("./debug.png", canvas)

输出:

enter image description here

绘制圆:

cv2.circle(img, center, radius, color, thickness1, lineType, shift)

**Parameters:** 
img (CvArr) – Image where the circle is drawn
center (CvPoint) – Center of the circle
radius (int) – Radius of the circle
color (CvScalar) – Circle color
thickness (int) – Thickness of the circle outline if positive, otherwise this indicates that a filled circle is to be drawn
lineType (int) – Type of the circle boundary, see Line description
shift (int) – Number of fractional bits in the center coordinates and radius value

例如:

^{pr2}$

相关问题 更多 >