James Mallett - 1 year ago 47

Python Question

I am trying to generate an array of coordinates that will lie on an image.

The image being used is a 640x480 pixel frame. I have been able to make an array of all the **x** and **y** points that I wish to use. I am trying to plot small circles at each of these points just for visualization and for later use so I am trying to get the coordinates into a form that can be entered into the OpenCV circle function. Below is what I have got so far:

`Ypts = np.arange(5, 480, 5)`

Xpts = np.arange(5, 640, 5)

I have tried using

`[pts]= np.vstack([Xpts, Ypts]).T`

and

`coordinate = []`

for x in range(Xpts.size):

for y in range(Ypts.size):

coordinate.append((x, y))

With no success, the output I get for coordinate is

`[0, 0], [0, 1], [0, 2], ...`

`Xpts`

`Ypts`

On a smaller scale this is an example of what the x and y arrays are:

`Xpts = [5, 10, 15, 20, 25, 30, 35]`

Ypts = [5, 10, 15]

and what i am trying to get to as an answer is:

`Points = [[5, 5],`

[5, 10],

[5, 15],

[10, 5],

[10, 10],

[10, 15],

[15, 5],

[15, 10],

......,

[35, 15]]

Answer

You can use `itertools.product`

which will basically return all possible combinations of the provided lists. See this code for an example:

```
import numpy as np
import itertools
Xpts=[5, 10, 15, 20, 25, 30, 35]
Ypts=[5, 10, 15]
Points = np.array(list(itertools.product(Xpts, Ypts)))
```

This will return the following:

```
array([[ 5, 5],
[ 5, 10],
[ 5, 15],
[10, 5],
[10, 10],
[10, 15],
[15, 5],
[15, 10],
[15, 15],
[20, 5],
[20, 10],
[20, 15],
[25, 5],
[25, 10],
[25, 15],
[30, 5],
[30, 10],
[30, 15],
[35, 5],
[35, 10],
[35, 15]])
```

Source (Stackoverflow)