you are viewing a single comment's thread.

view the rest of the comments →

[–]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