在opencv-python中围绕图像的质心绘制一个圆圈

问题描述 投票:-2回答:2

我想在图像中的某些物体的质心周围绘制一个红色圆圈(图像中的物体是一些昆虫,因此圆圈有助于人类在视觉上检测昆虫);我已经有了质心(吼叫),但不知道如何在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.]])

有谁知道怎么做我想要的?

python image opencv image-processing
2个回答
2
投票

您可以使用cv2.circle 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


0
投票

绘制圆圈:

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

例如:

cv2.circle(loaded_cv2_img, (100, 100), 20, (255, 0, 0), 5)
© www.soinside.com 2019 - 2024. All rights reserved.