【问题标题】:OpenCV drawCircle and draw Rectangles on the circle lineOpenCV drawCircle 并在圆线上绘制矩形
【发布时间】:2019-01-06 04:33:30
【问题描述】:

我想画不同半径的圆,然后我想在这个圆上画矩形。 它应该是这样的:

]

我用圆的公式试过了

y_Circle = Center_Circle.y + sqrt(pow(Radius, 2) - pow(x_Circle - Center_Circle.x, 2));

但这仅适用于圆圈的下部。对于上半部分,我需要这个公式,但在Center_Circly.y 之后有一个“-”。 问题是,我没有在上图中的位置获得矩形。看起来像这样: 在这张图片中,我使用上面的公式在一个圆上绘制了矩形。为了更好地理解,我用手画了两个圆圈来显示问题。 您可以看到,矩形之间有空间,而在下部,矩形之间没有空间。还有另一种可能以更简单的方式做到这一点吗?可能是这样:用openCV画一个圆,获取圆线的坐标并画出这个圆线的矩形。但我不知道如何获得圆的坐标。

这是我的代码-sn-p:

for (int Radius = Rect_size; Radius < MaxRadius;)
         {
             x_Circle = MaxRadius - Radius;
             circumference_half = 2 * 3.1415 * Radius / 2;
             Rectangle_count = circumference_half / Rect_size;

             for (int i = 0; i < Rectangle_count - 1; i++)
             {
                  y_Circle = Center_Circle.y + sqrt(pow(Radius, 2) - pow(x_Circle - Center_Circle.x, 2));

                 if (y_Circle <= FRAME_Heigth && x_Circle <= FRAME_WIDTH && x_Circle >=0)
                 {
                     test = Rect(x_Circle, y_Circle, Rect_size, Rect_size);
                     rectangle(RectangePic, test, Scalar(0, 255, 255), 1, 8);
                     imshow("testee", RectangePic);
                     waitKey();
                 }
                 x_Circle += Rect_size;
             }

             Radius += Rect_size;
         }

【问题讨论】:

  • 使用极坐标在概念上可能更简单?例如y_Circle = Center_Circle.y + Radius*std::sin(theta) 其中theta = (2*PI*i)/Rectangle_count;。这使xy 处于平等地位:如果y 坐标为r*sin(theta),x 坐标为r*cos(theta),您无需担心显式签名。在性能方面,我不确定 std::sin 是否比一个 sqrt 和两个 pow(,2) 更好或更差。
  • 谢谢,就是这样。 :-)

标签: c++ opencv


【解决方案1】:

试试这个脚本以获得这些结果:

import cv2, numpy as np, math
# Parameters for the window
hw = 789
# Parameters for the circle
circle_center = hw/2, hw/2
radius = hw/2.25
circle_thickness = 2
circle_color = (255,0,255)
# Parameters for the boxes
num_boxes = 50
box_size = 30
box_color = (0,255,0)
box_thickness = 2
# Create background image
bg = np.zeros((hw, hw, 3), np.uint8)
# Draw circle
cv2.circle(bg, tuple(np.array(circle_center, int)), int(radius), circle_color, circle_thickness)
# Time to draw some boxes!
for index in range(num_boxes):
    # Compute the angle around the circle
    angle = 2 * math.pi * index / num_boxes
    # Compute the center of the box
    x, y = circle_center[0] + math.sin(angle)*radius, circle_center[1] + math.cos(angle)*radius
    # Compute the corners of the
    pt1 = x-box_size/2, y-box_size/2
    pt2 = x+box_size/2, y+box_size/2
    # Draw Box
    cv2.rectangle(bg, tuple(np.array(pt1, int)),tuple(np.array(pt2, int)), box_color, box_thickness)

cv2.imshow('img', bg)
cv2.waitKey(0)
cv2.destroyAllWindows()

【讨论】:

  • 这就是解决方案。非常感谢!
猜你喜欢
  • 2014-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多