【问题标题】:Array with the number of times that appears出现次数的数组
【发布时间】:2020-04-16 16:07:58
【问题描述】:

print(exit) 打印两点之间基点的方向!!!!!! print(exit.count('N'))统计North出现的次数!!! print(dist(x,y,x+grad_x,y+grad_y))显示这两点之间的距离!!!!!

我想知道如何创建一个['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW', 'W'] 类型的数组,其中的值是每个基点出现的次数

代码

for x,y in new_points:
    cv2.circle(frame, (x, y), 10, (0, 255,), 1)

    if vector_points.size != 0:
        grad_x, grad_y = x-vector_points[i][0], y-vector_points[i][1]
        cv2.arrowedLine(frame, (x,y),(x+grad_x, y+grad_y) , (0,255,255), 1)
        exit = direcao(x+grad_x, x, y+grad_y, y)
        print(exit)
        print(exit.count('N'))
        print(dist(x,y,x+grad_x,y+grad_y))
        #tmp.append((dist(x,y,x+grad_x,y+grad_y)))
        #print(tmp)
    i+=1

【问题讨论】:

  • 好的,您已经知道如何获得北方向所需的结果。我假设您可以使用相同的技术来获取所有其他方向,然后将它们放入列表中。有什么问题?
  • 打印在我看来是循环的,具有不同的值和位置N10.8800021212 每行一个。如何提取出现在相应位置的所有内容,例如:'N',然后我们将它们相加?

标签: python arrays python-3.x opencv arraylist


【解决方案1】:

虽然我不熟悉你使用的方法,但如果我理解你的问题,我可以告诉你我的解决方案是defaultdict

from collections import defaultdict

counts = defaultdict(int)
directions = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW', 'W']

for x,y in new_points:
    cv2.circle(frame, (x, y), 10, (0, 255,), 1)

    if vector_points.size != 0:
        grad_x, grad_y = x-vector_points[i][0], y-vector_points[i][1]
        cv2.arrowedLine(frame, (x,y),(x+grad_x, y+grad_y) , (0,255,255), 1)
        exit = direcao(x+grad_x, x, y+grad_y, y)
        print(exit)

        for direction in directions:
            # this assumes exit.count() returns an int
            counts[direction] += exit.count(direction)
        print(dist(x,y,x+grad_x,y+grad_y))
        #tmp.append((dist(x,y,x+grad_x,y+grad_y)))
        #print(tmp)
    i+=1

for direction, count in counts.items():
    print(f'{direction} appears a total of {count} times.')

如果您想知道new_points 中每个点的次数,您只需在循环的每次迭代中创建一个新的defaultdict 并将其附加到list

您也可以按照您的要求将其分配给一个数组。

directions = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW', 'W']
counts = [0, 0, 0, 0, 0, 0, 0, 0, 0]

for x,y in new_points:
    cv2.circle(frame, (x, y), 10, (0, 255,), 1)

    if vector_points.size != 0:
        grad_x, grad_y = x-vector_points[i][0], y-vector_points[i][1]
        cv2.arrowedLine(frame, (x,y),(x+grad_x, y+grad_y) , (0,255,255), 1)
        exit = direcao(x+grad_x, x, y+grad_y, y)
        print(exit)

        for elem, direction in enumerate(directions):
            counts[elem] += exit.count(direction)

        print(dist(x,y,x+grad_x,y+grad_y))
    i+=1

print(counts)

【讨论】:

  • 我认为它有效。顺便说一句,是否有可能只使用 for 的最后一次打印?并将“字符串”传递给数组?
  • 我不确定我是否明白你在问什么。你能举一个期望输出的例子吗?
  • 例如:N appears a total of 13 times. NE appears a total of 0 times. E appears a total of 0 times. SE appears a total of 0 times. S appears a total of 1 times. SW appears a total of 0 times. W appears a total of 108 times. NW appears a total of 12 times. N appears a total of 13 times. NE appears a total of 0 times. E appears a total of 0 times. SE appears a total of 0 times. S appears a total of 1 times. SW appears a total of 0 times. W appears a total of 113 times. NW appears a total of 12 times. 我只想要最后 8 个位置并将其放入数组 [N,NE,E]...
  • 这是它现在返回的吗?或者这就是你想要的?如果不知道new_points 中的内容,我无法知道。当你说你想要最后 8 个位置时,你是指最后 8 个方向还是最后 8 个 new_points
  • 这是现在返回的,基本上我在视频中选择点并跟踪它们,新点就是那些点,我想返回的是那个但只是8个方向的最终总和,并且然后在数组中放入一个变量,给出每个位置的最后一个“counts”(数字),真心解释有点难,不知道大家看懂了没有
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多