简体   繁体   中英

OpenCV drawCircle and draw Rectangles on the circle line

I want to draw circles with different radius and then I want to draw rectangles on this circle. It should look like this:

在此处输入图片说明 ]

I have tried it with the formula for the circle

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

but this is just for the lower part of the circle. For the upper part I need this formula, but with a "-" after Center_Circly.y . The Problem is, that i´m not getting the rectangles in the position like in the image above. It looks like this: 在此处输入图片说明 In this image I draw the rectangles on a circle with the formula above. For a better unterstanding I have drawn two circles by hand to show the problem. You can see, that there is space between the rectangles and in the lower part there is no space between the rectangles. Is there another possibility to do that in an easier way? Maybe like this: Draw a circle with openCV, get access to the coordinates of the circle line and draw the rectangles of this circle line. But I don´t know how to get acces to the coordinates of the circle.

Here is my code-snippet:

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;
         }

盒

Try this script for these results:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM