更改代码以在图片中使用多个圆圈

时间:2018-10-11 15:10:00

标签: python opencv

enter image description here

我有一个黑色的背景图像,并使用opencv在其上绘制了一个简单的圆圈。我要做的是将圆圈移向目标。现在,我想修改代码,以便有一个以上的运动圈,并且我希望它是动态的,就像我想添加的一样多,但是我不知道怎么做。这是到目前为止我在python中的代码...

#Class of drawing circles
class my_circles:

    def __init__(self):

        self.radius = 30
        return

    src = cv2.imread(r'C:\Users\100689998\Documents\Python Scripts\black.jpg')
    folder = r'C:\Users\100689998\Documents\Python Scripts\Images'

    def draw(self, x_location, y_location, index):
        self.x = x_location
        self.y = y_location
        self.i = index
        im = cv2.circle(src, (math.ceil(self.x), math.ceil(self.y)), radius, (255,255,255), -1)
        cv2.imwrite('{!s}/{!s}.JPEG'.format(folder, self.i), im)


#coordinates of the target 
x_target = 200
y_target = 300
src = cv2.circle(src, (200,300), radius, (255,255,255), -1)
cv2.imwrite('circles.jpg', src)
path_vector = []


def move(x_cord, y_cord):
    path_vector =np.array([x_target - x_first, y_target - y_first])
    norm = math.sqrt(math.pow(path_vector[0],2) + math.pow(path_vector[1],2))
    unit = np.array([path_vector[0] / norm, path_vector[1] / norm])
    x_cord = np.array(x_first + unit[0])  
    y_cord = np.array(y_first + unit[1]) 
    return x_cord, y_cord

for time in range(0,100):

    #moving the circles toward target
    new_cord_x , new_cord_y = move(x_first, y_first)
    c = my_circles()
    circle = c.draw(new_cord_x, new_cord_y, time)

    #update the first coordinates
    x_first = new_cord_x
    y_first = new_cord_y

0 个答案:

没有答案