Ok, so i've got one definition on my python code, that defines how to find the position of something in an array, then returns the list of all the places that those things are.

Then I've got another definition to find the accurate centroid of a position.

How do I link the 2? As in, for the first one, I get a list of different co-ordinates, how do I go about then placing those in the second definition, and hence find a more accurate position of the object in the array.

If this doesn't make sense I can try and explain it better, but I'm having trouble sending the various parts between the definitions. If I put a "known" set of coordinates in the second definition, then I can easily find the centroid and that works, but say definition 1 gives me 10 different positions/ points, how do I link these ten, one by one, into the second definition, giving me accurate positions, without making one big definition?

Many thanks,

Furret

Something like this situation?

try:
    from pretty import ppr as pprint # my pretty printer, see snippet archive
except:
    from pprint import pprint # standard fallback
    
def outten(a):
    for i in range(1, 1+10):
        yield float(a) / i

def outone(x):
    yield x / 2.0

values = (1, 2, 3, 4, 21, 32, 12)
pprint([[b  for b in outten(a)] for a in values])

"""Output with pretty:

[
  [1.0, 0.5, 0.333333333333, 0.25, 0.2, 0.166666666667, 0.142857142857, 0.125, 0.111111111111, 0.1], 
  [2.0, 1.0, 0.666666666667, 0.5, 0.4, 0.333333333333, 0.285714285714, 0.25, 0.222222222222, 0.2], 
  [3.0, 1.5, 1.0, 0.75, 0.6, 0.5, 0.428571428571, 0.375, 0.333333333333, 0.3], 
  [4.0, 2.0, 1.33333333333, 1.0, 0.8, 0.666666666667, 0.571428571429, 0.5, 0.444444444444, 0.4], 
  [21.0, 10.5, 7.0, 5.25, 4.2, 3.5, 3.0, 2.625, 2.33333333333, 2.1], 
  [32.0, 16.0, 10.6666666667, 8.0, 6.4, 5.33333333333, 4.57142857143, 4.0, 3.55555555556, 3.2], 
  [12.0, 6.0, 4.0, 3.0, 2.4, 2.0, 1.71428571429, 1.5, 1.33333333333, 1.2]]
"""

Or you can use map function.

Edited 6 Years Ago by pyTony: n/a

Ok, tonyjv I'm not sure I get you, but as Gribouillis stated, I should probably put some code in to illustrate my point.

def find_object(filename, weight): 

#This gives a definition that plugs out the positions of objects over a certain weight, and along with other techniques, gives mid point positions of the objects, but only to the nearest integer, so for one file for example, the definition gives a return of (with heaviest object first):

[[102,254]
[203, 109]
[99, 103]
[370, 100]
[45, 23]
[34, 45]
[24, 23]]

The second definiton:

def measure_CoM(filename):
        img = helper_code.imageFile_to_Array(filename)  #turns same file as used before into a readable array
        x1 = find_object(filename, weight)[0,0] #Uses the x-coordinates of first value of find_object from previous definition
        y1 = find_object(filename, weight)[0,1] #Sam but y- coordinate
        bg = (img[x1-10,y1-10]+img[x1+10,y1-10]+img[x1-10,y1+10]+img[x1+10,y1+10])/4
        nobg = img[x1-10:x1+10, y1-10:y1+10] - bg
        reducedbackground = nobg.clip(0)
        intensity= float(reducedbackground.sum()) #These lines are just to remove the background weight, and find exact positions

        X = 0
        Y = 0
        for i in range(len(reducedbackground)):
                for j in range(len(reducedbackground[i])):
                        X=float(X+(i*reducedbackground[i,j]))
                        Y=float(Y+(j*reducedbackground[i,j]))
        Cx = "%.1f" % float(Y/intensity +(x1-10))
        Cy = "%.1f" % float(X/intensity +(y1-10))
        #Cx1 = "%.1f" % (Cx)
        #Cy1 = "%.1f" % (Cy)
        return (Cx, Cy) #Essentially this returns the function as before, but to a more precise value. and strictly one decimal place.

So yeah, there it is, if you could help me to work out how to get all of the definition 1 values and plug them into definition 2 to work out the various precise positions that'd be fantastic, as at the moment, as you can see, I'm working on 1 value at a time, and I'm sure a loop of some sort of something would be better, but have hit a brick wall as to what this could be. Potentially a third definition?

Many thanks,

Furret

can you post the code of your functions ? It all depends on the way the functions must be called.

Sorry, I replied but forgot to hit reply on the actual forum.

Furrt

Ok, tonyjv I'm not sure I get you, but as Gribouillis stated, I should probably put some code in to illustrate my point.

def find_object(filename, weight): 

#This gives a definition that plugs out the positions of objects over a certain weight, and along with other techniques, gives mid point positions of the objects, but only to the nearest integer, so for one file for example, the definition gives a return of (with heaviest object first):

[[102,254]
[203, 109]
[99, 103]
[370, 100]
[45, 23]
[34, 45]
[24, 23]]

The second definiton:

def measure_CoM(filename):
        img = helper_code.imageFile_to_Array(filename)  #turns same file as used before into a readable array
        x1 = find_object(filename, weight)[0,0] #Uses the x-coordinates of first value of find_object from previous definition
        y1 = find_object(filename, weight)[0,1] #Sam but y- coordinate
        bg = (img[x1-10,y1-10]+img[x1+10,y1-10]+img[x1-10,y1+10]+img[x1+10,y1+10])/4
        nobg = img[x1-10:x1+10, y1-10:y1+10] - bg
        reducedbackground = nobg.clip(0)
        intensity= float(reducedbackground.sum()) #These lines are just to remove the background weight, and find exact positions

        X = 0
        Y = 0
        for i in range(len(reducedbackground)):
                for j in range(len(reducedbackground[i])):
                        X=float(X+(i*reducedbackground[i,j]))
                        Y=float(Y+(j*reducedbackground[i,j]))
        Cx = "%.1f" % float(Y/intensity +(x1-10))
        Cy = "%.1f" % float(X/intensity +(y1-10))
        #Cx1 = "%.1f" % (Cx)
        #Cy1 = "%.1f" % (Cy)
        return (Cx, Cy) #Essentially this returns the function as before, but to a more precise value. and strictly one decimal place.

So yeah, there it is, if you could help me to work out how to get all of the definition 1 values and plug them into definition 2 to work out the various precise positions that'd be fantastic, as at the moment, as you can see, I'm working on 1 value at a time, and I'm sure a loop of some sort of something would be better, but have hit a brick wall as to what this could be. Potentially a third definition?

Many thanks,

Furret

You could use a function structure like this

def measure_CoM(filename):
    result_list = list()
    img = helper_code.imageFile_to_Array(filename)
    for x1, y1 in find_object(filename, weight):
        ...
        Cx = ...
        Cy = ...
        result_list.append((Cx, Cy))
    return result_list

Apart from that, you have a lot to learn in python, for example you don't need to convert to float all the time. There are rules: float + int -> float, float * int ->float, etc.

Edited 6 Years Ago by Gribouillis: n/a

This article has been dead for over six months. Start a new discussion instead.