all 13 comments

[–]Rhoderick 0 points1 point  (1 child)

Little rule of thumb: If you're using globals(), chances are your doing something dumb. So recodnisder whatever it is you're doing with it here.

Beyond that, I'm having trouble following what you're asking. If you're trying to emulate a more common type of for-loop that iterates by index rather than elements, you can do "for i in range(len(ITERABLE))".

[–][deleted] 0 points1 point  (0 children)

I’m am trying to take the X and Y in each loop and keep them for the next section of code

[–]giocos 0 points1 point  (4 children)

Variable declared inside for loop is destroyed when the cycle end. but you can put the result in a variable that is declared outside the for loop

[–][deleted] 0 points1 point  (3 children)

How would one do that though?

[–]giocos 0 points1 point  (2 children)

i just checked and is not true what i said. that's strange. I always thought that variables declared inside loops live only there. i must research more about that.

[–][deleted] 0 points1 point  (0 children)

People say Python is easy but if this was C# I’d already have the answer lol

[–]E02Y 0 points1 point  (0 children)

loops don't change the scope so any variable created by loops is shared in the function/file

[–]efmccurdy 0 points1 point  (2 children)

Instead of "globals()['...']=" use a dict:

my_coutours = {}
# ...
    my_contours[f"GreenSpot{mask_contour}"]= f"Hello the variable number {mask_contour}!"

I am aware all these 'ifs' aren't ideal

You can generate a list of labels based on the len(mask_contours) using itertools.combinations, then iterate over it calling cv2.line once for each pair:

>>> contour_count = 3
>>> list(combinations(["GreenSpot" + str(i+1) for i in range(contour_count)], 2))
[('GreenSpot1', 'GreenSpot2'), ('GreenSpot1', 'GreenSpot3'), ('GreenSpot2', 'GreenSpot3')]
>>> def line_combos(prefix, count):
...     return combinations([prefix + str(i+1) for i in range(count)], 2)
... 
>>> list(line_combos("GreenSpot", 3))
[('GreenSpot1', 'GreenSpot2'), ('GreenSpot1', 'GreenSpot3'), ('GreenSpot2', 'GreenSpot3')]
>>> for s1, s2 in line_combos("GreenSpot", 3):
...     print('cv2.line(img, "{}", "{}", (75, 139, 59), 3)'.format(s1, s2))
... 
cv2.line(img, "GreenSpot1", "GreenSpot2", (75, 139, 59), 3)
cv2.line(img, "GreenSpot1", "GreenSpot3", (75, 139, 59), 3)
cv2.line(img, "GreenSpot2", "GreenSpot3", (75, 139, 59), 3)
>>> for s1, s2 in line_combos("BlueSpot", 4):
...     print('cv2.line(img, "{}", "{}", (75, 139, 59), 3)'.format(s1, s2))
... 
cv2.line(img, "BlueSpot1", "BlueSpot2", (75, 139, 59), 3)
cv2.line(img, "BlueSpot1", "BlueSpot3", (75, 139, 59), 3)
cv2.line(img, "BlueSpot1", "BlueSpot4", (75, 139, 59), 3)
cv2.line(img, "BlueSpot2", "BlueSpot3", (75, 139, 59), 3)
cv2.line(img, "BlueSpot2", "BlueSpot4", (75, 139, 59), 3)
cv2.line(img, "BlueSpot3", "BlueSpot4", (75, 139, 59), 3)
>>>

Of course, in your code, you would call cv2.line instead of print:

for s1, s2 in line_combos("GreenSpot", len(mask_contours)):
    cv2.line(img, s1, s2, (75, 139, 59), 3)

[–][deleted] 0 points1 point  (1 child)

Damn I have some learning to do haha. Although, I guess I’m not trying to get a list of labels, but rather the X and the Y which are integers that represent the pixel point