Python - How to add the location of each object in the image?
<p>I have tracked more than one objects in the image and now my final step is to add the location of each object in the image here is the <a href="http://i.stack.imgur.com/guyKa.png" rel="nofollow">image</a></p>

<p>Another question is when I am printing the x, y, and radius of each object, I found that the locations are printed in a push pop behaviour, for example, if I have 3 Locations of 3 objects:</p>

<p>object 1 (1,4)</p>

<p>object 2 (7,6)</p>

<p>object 3 (5,3)</p>

<p>the output is shown like following:</p>

<p>1,4</p>

<p>7,6</p>

<p>5,3 and so on</p>

<p>Thus I want to sort each location in an array like this <code>[1,4],[ 7,6],[5,3]</code></p>

<p>And I want to print the locations of each object in the image. Any ideas ??</p>

<p>Without seeing all of your code, it's hard to suggest a solution, but it looks like you are at least iterating over your objects.  If not, you can put them in a list like so:</p>

<pre><code>obj_list = [obj1, obj2, obj3]
</code></pre>

<p>Then you can add their coordinates to a list like this:</p>

<pre><code>obj_coords = [(obj.x, obj.y) for obj in obj_list]
</code></pre>

<p>Then you can sort the list with python's excellent <code>sorted</code>:</p>

<pre><code>obj_coords = sorted(obj_coords, key=lambda x: x[0])
</code></pre>

<p>You can also sort by the y value by changing <code>key=lambda x: x[0]</code> to <code>key=lambda x: x[1]</code>.</p>

<p><strong><em>EDIT:</em></strong>  It would be helpful if you copy-paste all your code into your question so I can test it, See the help section for how to make an <a href="http://stackoverflow.com/help/mcve">MCVE</a>.</p>

<p>Anyway, I didn't mean literally <code>[obj1, obj2, obj3]</code> unless those are the variable names you chose.  Your code has a line:</p>

<pre><code>print int(x), int(y), int(radius)
</code></pre>

<p>if you create an empty list like this:  <code>obj_list = []</code> then add tuples to it by replacing this print statement with:</p>

<pre><code>obj_list.append( (int(x), int(y)) )
</code></pre>

<p>If you're still having trouble, open a new question with an MVCE.  Most people will not help you unless they can copy paste your code to test it out and fix it.</p>

python python-2.7 opencv computer-vision