Mbatkis
Mbatkis

Reputation: 35

How to store coordinates into an array in python?

I am starting a new project that will involve taking two images and comparing the centers of dots inside the images. The original will be a picture with 27 rows and 15 columns of dots. The second picture will be a distorted version of the original where the dots have changed position and shape.

To begin this project I will need to store the original centers in an array.

I have made a formula to find the center of every circle in the original.

X: n = 0..26 [48n+5]
Y: m = 0..14 [48m+5]

As an example the center of dot (2,5) would be

[48(2)+5, 48(5)+5] = [101, 245]

TLDR: I need help storing (x,y) coorinates using the following formula

[X,Y] = [48(n)+5 , 48(m)+5] for n = 0..26, m = 0..14

Update:

So it looks like the 2D array is what I need to use. In the end I want to assign each coordinate a string name.

arr = []
for n in range(27):
    for m in range(27):
        arr.append([48*(m)+5 , 48*(n)+5])

for x in range(405):
print(arr[x])

Currently using this code numbers 0..404 will yield coordinate pairs (0=[5,5] and 404=[1253,677]). However I want an easier way to reference each coordinate pair rather than finding which object it is numerically.

I want to assign the top left dot the name A0. It will have the propertie A0 = [5,5] As you move right you reach the top right most dot with a center name A26. It will have the following properties A26 = [1253,5] Going to the bottom left dot it's center would be called O0 and have the properties of O0 = [5,677] and that row would span to the right until it hits the bottom right dot's center named O26 having the properties O26 = [1253,677]

As you can see in the diagram below I want to be able to call upon a grid of names that would be formatted in the following way.

A0...                          ...A26
.                                   .
.                                   .
.                                   .



.                                   .
.                                   .
.                                   .
O0...                          ...O26

So if I call for the coordinates of O26 it would return [1253,677]

Upvotes: 1

Views: 3645

Answers (3)

A. Maman
A. Maman

Reputation: 972

This is the straight forward way for 2D array:

arr = []
for n in range(27):
    for m in range(15):
        arr.append([48*(n)+5 , 48*(m)+5])

This is the same for 3D array:

arr = []
for n in range(27):
    sub = []
    for m in range(15):
        sub.append([48*(n)+5 , 48*(m)+5])
    arr.append(sub)

For the second one you can access the value of n and m is arr[n][m].

One liner for the same solution:

arr = [[[48*n+5 , 48*m+5] for m in range(15)] for n in range(27)]

EDIT

As you modified your question for labeling the indices, I believe the best way to do it is by a dictionary (using dict comprehention):

letters = list('ABCDEFGHIJKLMNO')
dic = {letters[m]+str(list(range(27))[n]):[48*n+5 , 48*m+5] 
       for m in range(15) for n in range(27)}

Where:

>>> dic['A1']
[53, 5]
>>> dic['A26']
[1253, 5]
>>> dic['O0']
[5, 677]
>>> dic['O26']
[1253, 677]

Hope that helps, if it does please mark it as one, if not - let me know why.

Upvotes: 2

jimmie_roggers
jimmie_roggers

Reputation: 173

Hi I don't quite understand what your question is, but you can store your image in an array or a python list using the following code, using a nested loop:

centers = []

for i in range(27):
   for j in range(15): 
      centers.append((48*n+5 , 48*m+5))

But if you need more help with image processing and how to convert your image into a numpy array, use the following link: https://www.pluralsight.com/guides/importing-image-data-into-numpy-arrays

Upvotes: 1

Ashwin Shenoy
Ashwin Shenoy

Reputation: 701

You can use a simple list comprehension to achieve your task

[(48*(n) + 5, 48*(m) + 5) for n in range(0, 27) for m in range(0, 15)]

This will give you a list of tuples, with each tuple representing a coordinate point.

Upvotes: 0

Related Questions