Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ OpenCV drawCircle并在圆线上绘制矩形_C++_Opencv - Fatal编程技术网

C++ OpenCV drawCircle并在圆线上绘制矩形

C++ OpenCV drawCircle并在圆线上绘制矩形,c++,opencv,C++,Opencv,我想画不同半径的圆,然后在这个圆上画矩形。 应该是这样的: ] 我用圆的公式试过了 y_Circle = Center_Circle.y + sqrt(pow(Radius, 2) - pow(x_Circle - Center_Circle.x, 2)); 但这只是圆的下半部分。对于上半部分,我需要这个公式,但在中间加一个“-”圈.y。 问题是,我没有得到像上图中那样的矩形位置。看起来是这样的: 在这幅图中,我用上面的公式画了一个圆上的矩形。为了更好地理解,我用手画了两个圆圈来表示这个问题。

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

]

我用圆的公式试过了

y_Circle = Center_Circle.y + sqrt(pow(Radius, 2) - pow(x_Circle - Center_Circle.x, 2));
但这只是圆的下半部分。对于上半部分,我需要这个公式,但在
中间加一个“-”圈.y
。 问题是,我没有得到像上图中那样的矩形位置。看起来是这样的: 在这幅图中,我用上面的公式画了一个圆上的矩形。为了更好地理解,我用手画了两个圆圈来表示这个问题。 你可以看到,矩形之间有空间,而在下部,矩形之间没有空间。还有其他更简单的方法吗?可能是这样的:用openCV绘制一个圆,访问圆线的坐标并绘制这条圆线的矩形。但是我不知道如何获得圆的坐标

以下是我的代码片段:

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;
         }
for(int Radius=Rect_size;Radius
请尝试此脚本以获得以下结果:

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()

从概念上讲,使用极坐标可能更简单,例如
y\u圆=中心圆。y+半径*std::sin(θ)
其中
theta=(2*PI*i)/Rectangle\u count;
。这使得
x
y
处于同等地位:如果y坐标是
r*sin(θ)
,则x坐标是
r*cos(θ)
而且您不必担心显式签名。就性能而言,我不确定
std::sin
比一个
sqrt
和两个
pow(,2)
更好还是更差。谢谢,这就是解决方法。:-)这就是解决方案。非常感谢!