0

I have a for loop, used to mark circles under Hough function from OpenCV:

   for i in circles[0,:]:
        x=i[0]
        y=i[1]
        cv2.circle(cimg,(i[o],i[1]),i[2],(0,255,0),2)
        cv2.circle(cimg,(i[o],i[1]),2,(0,0,255),3)
        text='x='+str(x)
        text2='y='+str(y)
        cv2.puttext(img, text,(10,90),font,1,(0,0,255),cv2.line_aa)

The thing i want to do is to save every x-y coords of the circle under different variables, like a=x1, b=y1 (for iteration 1) so i would be able to call a,b and use them later. Any ideas?

If i just print i[0] it will output just the x1 (first iteration for the first circle) and x2 (second iteration from the second circle), just like this: 22 42

Thanks!

Minu
  • 11
  • 1
  • 1
  • 3
  • 2
    Store them in a `list` using `.append` – ZdaR Apr 01 '17 at 12:29
  • If you'd save every element anyway, why don't you use original list instead? E.g., `elemente[0]` for `0`, `elemente[1]` for `1` and so on. If you know the size of list (`len(list)`), you know how many iterations you would have and can access the elements by indices. Or am I misunderstanding the issue? – cegas Apr 01 '17 at 12:41
  • 1
    The problem is a bit more complex. What i am trying to do is to find 2 circles in an image and then to save its x,y coordinates under different variables. I will edit the post in a second. Hope i will make it a bit more clear :). Thanks for the answers too – Minu Apr 01 '17 at 12:57

2 Answers2

1

If you only want to assign each element in "elemente" to a different variable, list unpacking is all you need:

a,b = elemente
acidtobi
  • 1,375
  • 9
  • 13
1

As @ZdaR commented,

output = []
elemente = [0, 1]
for x in elemente:
    output.append(x)

print(output)     # [0, 1]
print(output[0])  # 0
print(output[1])  # 1
Sangbok Lee
  • 2,132
  • 3
  • 15
  • 33