【问题标题】:Drawn a circle around the centroid of a image in opencv-python在opencv-python中围绕图像的质心画一个圆圈
【发布时间】:2017-12-20 06:27:13
【问题描述】:

我想在图像中一些物体的质心周围画一个红色圆圈(图像中的物体是一些昆虫,所以这些圆圈有助于人类在视觉上检测昆虫);我已经有了质心(如下),但不知道如何在 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


    【解决方案1】:

    您可以将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)
    

    输出:

    【讨论】:

      【解决方案2】:

      画一个圆:

      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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-03
        • 2013-05-05
        • 2021-06-24
        • 2019-09-04
        • 1970-01-01
        • 2011-10-13
        • 2017-11-20
        • 1970-01-01
        相关资源
        最近更新 更多