-1

I have not been able to format my numpy array the way I want it.I checked several post like How to remove specific elements in a numpy array most seemed close but none has what I want. If this question is a repition point me in the right direction.

im2, contours, hierarchy = cv2.findContours(th2,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

contours gives me this:

print(contours)

I get this:

[array([[[618, 737]]], dtype=int32), array([[[615, 737]]], dtype=int32), array([[[656, 731]],
   [[655, 732]],

   [[651, 732]],

   [[649, 734]],

   [[648, 734]],

   [[647, 735]],

   [[646, 734]],

   [[645, 735]],

   [[644, 735]],

   [[643, 736]],

   [[641, 736]],

   [[640, 737]],

       [[686, 737]],

       [[686, 734]],

but I'd like to get an output that could give me a tuple in this format:

(x, y)

or if there was another way I could access the integers in the array that would be neat I already tried but did not get what I want

contours = tuple(map(tuple, contours))

contours = totuple(contours)

please help I'm a newbiee

Community
  • 1
  • 1
Durodola Opemipo
  • 319
  • 2
  • 12

1 Answers1

1

Are you looking for something like this?

[[tuple(xy[0]) for xy in contour] for contour in contours]

#[[(618, 737)],
# [(615, 737)],
# [(656, 731), (655, 732), (651, 732), (649, 734), (648, 734)]]

If you want a flat list:

[tuple(xy[0]) for contour in contours for xy in contour]

#[(618, 737),
# (615, 737),
# (656, 731),
# (655, 732),
# (651, 732),
# (649, 734),
# (648, 734)]
Psidom
  • 209,562
  • 33
  • 339
  • 356
  • I'm looking for something more like {(618, 737),(615,737),(656, 731), (655, 732), (651, 732), (649, 734), (648, 734)} would that be possible – Durodola Opemipo May 09 '17 at 14:13
  • You can move the inner for loop to end of the list comprehension, which will produce a flattened list. If you want a set, use a set comprehension `{tuple(xy[0]) for contour in contours for xy in contour}` – Psidom May 09 '17 at 14:15
  • The results is still the same. I tried this: `co_ord = {}` `co_ord.update([tuple(xy[0]) for contour in contours for xy in contour])` and got something like this `{618: 20, 615: 19, 656: 75, 655: 55, 651: 3, 649: 3, 648: 53, 647: 54, 646: 54,.....}` – Durodola Opemipo May 09 '17 at 14:25
  • What do you need at the end? a dictionary? And also a list comprehension doesn't change `contours`, if you want a new contours, you need to assign the result back. `contours = [tuple(xy[0]) for contour in contours for xy in contour]`. – Psidom May 09 '17 at 14:29
  • `co_ord = []` `co_ord.append ([tuple(xy[0]) for contour in contours for xy in contour]) ` and got something like this `[[(618, 737), (615, 737), (656, 731), (655, 732), (651, 732), (649, 734),.....]] ` I think this is better I used a dictionary instead of a list my Bad. Thanks your the best I wonder why someone voted my question down like I didn't know what I'm saying – Durodola Opemipo May 09 '17 at 14:35
  • 1
    Does this work now? Maybe `co_ord.extend([...])` is what you need. – Psidom May 09 '17 at 14:37
  • `co_ord.extend([tuple(xy[0]) for contour in contours for xy in contour]) ` `co_ord.append([tuple(xy[0]) for contour in contours for xy in contour])` and this `co_ord =[[tuple(xy[0]) for contour in contours for xy in contour]] `also works but what really is the difference between extend an append they seem to give the same result whenever I use them and I really cared less but I know in python one is always the best option for different cases – Durodola Opemipo May 09 '17 at 14:40
  • `extend` concatenate another list to your list while `append` add another element to your list. No option is necessarily the best one, they are meant for different purposes. – Psidom May 09 '17 at 14:51