Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run an algorithm over a list in Python and store the results in a list?

The algorithm shown in the code below calculates the Chebyshev spatial distance between an ellipsoid image drawn in Python by the ellipse function and another black and white image.

a busy cat

translate is a function which moves each point of the ellipsoid by a certain distance. I would like to observe how the Chebyshev distance varies with different translation values. I have a list containing the pairs of translation coordinates I would like to try out:

translation_points = 
[ (5, 6), (5, 7), (5, 8), (5, 9), (5, 10), (5, 11), (5, 12), (5, 13), (5, 14), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (6, 10), (6, 11), (6, 12), (6, 13), (6, 14), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9), (7, 10), (7, 11), (7, 12), (7, 13), (7, 14), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9), (8, 10), (8, 11), (8, 12), (8, 13), (8, 14), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9), (9, 10), (9, 11), (9, 12), (9, 13), (9, 14), (10, 5), (10, 6), (10, 7), (10, 8), (10, 9), (10, 10), (10, 11), (10, 12), (10, 13), (10, 14), (11, 5), (11, 6), (11, 7), (11, 8), (11, 9), (11, 10), (11, 11), (11, 12), (11, 13), (11, 14), (12, 5), (12, 6), (12, 7), (12, 8), (12, 9), (12, 10), (12, 11), (12, 12), (12, 13), (12, 14), (13, 5), (13, 6), (13, 7), (13, 8), (13, 9), (13, 10), (13, 11), (13, 12), (13, 13), (13, 14), (14, 5), (14, 6), (14, 7), (14, 8), (14, 9), (14, 10), (14, 11), (14, 12), (14, 13), (14, 14)]

As seen in translate below, I predefine my DX and DY to be (5,5). I want to change the values of DX and DY to those above (in translation_points) and store the resulting spatial distances in a list. How can I update my code to pass in the list to the algorithm and output a list?

Here is my existing code for the algorithm:

import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from scipy.spatial import distance
import scipy.misc
im = scipy.misc.imread(r'Downloads/irregular1.png', flatten=False, mode='L')


def ellipse(x, y):
    value = (x*x) + (y*y)/3
    if (value >= 600):
        return 0
    else:
        return 1

def translate(x, y):
    DX = 5
    DY = 5
    return (x- DX, y - DY)

def rotate(x, y):
    theta = np.radians(45)
    matrix = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]])
    return np.dot(matrix, (x,y))

data = np.zeros((100,100))

for i in range(0, 100):
    for j in range(0, 100):
        (x, y) = translate(i,j)
        (x, y) = rotate(x, y)
        data[i,j] = ellipse(x, y)

plt.imshow(data, cmap="gray")
#plt.show()


plt.imshow(im)
#plt.show()

counter = 0 #tracking white
counter1 = 0 #tracking black 

#getting the dimensions of the image -> y
yDim = im.shape[0]

#getting the dimensions of the image -> x
xDim = im.shape[1]

for i in range(yDim):
    for j in range (xDim): 
        if np.any(im[i,j]) == 0:
            counter += 1
        else: 
            counter1 += 1

#initialize empty array this array will receive all the white pixels 
a = np.empty([100,100])

for i in range(yDim):
    for j in range (xDim): 
        if np.any(im[i,j]) == 0:
            np.append(a,im[i,j],axis=None)

#spatial distance 
a = a.flatten()
data = data.flatten()

distance = distance.hamming(a,data))
like image 380
M.Bore Avatar asked Jan 27 '26 21:01

M.Bore


1 Answers

Updated to show two ways to do it. The first way is to have it in the function shown below. Make a list to temporarily store the results temp, loop through the list translation_points that is defined in the function, subtract the difference and add it to our temporary list temp. Once its looped all the way through the list translation_points return the temp storing all the new points.

def translate(x, y):
    translation_points = [ (5, 6), (5, 7), (5, 8), (5, 9), (5, 10), (5, 11), (5, 12), (5, 13), (5, 14), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (6, 10), (6, 11), (6, 12), (6, 13), (6, 14), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9), (7, 10), (7, 11), (7, 12), (7, 13), (7, 14), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9), (8, 10), (8, 11), (8, 12), (8, 13), (8, 14), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9), (9, 10), (9, 11), (9, 12), (9, 13), (9, 14), (10, 5), (10, 6), (10, 7), (10, 8), (10, 9), (10, 10), (10, 11), (10, 12), (10, 13), (10, 14), (11, 5), (11, 6), (11, 7), (11, 8), (11, 9), (11, 10), (11, 11), (11, 12), (11, 13), (11, 14), (12, 5), (12, 6), (12, 7), (12, 8), (12, 9), (12, 10), (12, 11), (12, 12), (12, 13), (12, 14), (13, 5), (13, 6), (13, 7), (13, 8), (13, 9), (13, 10), (13, 11), (13, 12), (13, 13), (13, 14), (14, 5), (14, 6), (14, 7), (14, 8), (14, 9), (14, 10), (14, 11), (14, 12), (14, 13), (14, 14)]
    temp = []
    for i in translation_points:
        temp.append((x-i[0],y-i[1]))
    return temp

print(translate(5,5))

The other way is to feed in the list of tuples as an argument and do the same procedure. Can offer more functionality this way by being able to change the translation_points.

translation_points = [ (5, 6), (5, 7), (5, 8), (5, 9), (5, 10), (5, 11), (5, 12), (5, 13), (5, 14), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (6, 10), (6, 11), (6, 12), (6, 13), (6, 14), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9), (7, 10), (7, 11), (7, 12), (7, 13), (7, 14), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9), (8, 10), (8, 11), (8, 12), (8, 13), (8, 14), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9), (9, 10), (9, 11), (9, 12), (9, 13), (9, 14), (10, 5), (10, 6), (10, 7), (10, 8), (10, 9), (10, 10), (10, 11), (10, 12), (10, 13), (10, 14), (11, 5), (11, 6), (11, 7), (11, 8), (11, 9), (11, 10), (11, 11), (11, 12), (11, 13), (11, 14), (12, 5), (12, 6), (12, 7), (12, 8), (12, 9), (12, 10), (12, 11), (12, 12), (12, 13), (12, 14), (13, 5), (13, 6), (13, 7), (13, 8), (13, 9), (13, 10), (13, 11), (13, 12), (13, 13), (13, 14), (14, 5), (14, 6), (14, 7), (14, 8), (14, 9), (14, 10), (14, 11), (14, 12), (14, 13), (14, 14)]

def translate(x,y,l): 
    temp = []
    for i in l:
        temp.append((x-i[0],y-i[1]))
    return temp
like image 200
Edwin Wallis Avatar answered Jan 30 '26 11:01

Edwin Wallis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!