def CalcCentre(data):
    centre = array([0,0,0])
    count = 0
    for p in data:
        centre = add(centre, array(p[:3]))
        count += 1
    centre = dot(1./count, centre)
    return centre

Recommended Answers

All 5 Replies

pretty useless until you give more codes to support that. sorry ..

As posted over we need more info,like input data.
Code use Numpy and it use function add() and dot().
To see what these numpy function dos,look into numpy doc here is add()
To test function so it work i guessed for a nested list as input data.

from numpy import *

def CalcCentre(data):
    centre = array([0,0,0])
    count = 0
    for p in data:
        centre = add(centre, array(p[:3]))
        count += 1
    centre = dot(1./count, centre)
    return centre

print CalcCentre(array([[1,2,3], [4,5,6]]))
#--> [ 2.5  3.5  4.5]
commented: well spotted +13

So the function calculates the barycentre of a set of points in a multidimensional space.

import cv
from math import floor, sqrt, ceil
from numpy import array, dot, subtract, add, linalg as lin



def CalcCentre(data):
    centre = array([0,0,0])
    count = 0
    n = 0
    #data = list(data) 
    for p in data[n:n+3]:
        centre = add(centre, array(p[:3]))
        count += 1
        centre = dot(1./count, centre)
        return centre
    n += 1         
def ReadPointCloud(filename):
    f = open(filename)
    result = []
    for l in f:
        sp = l.split()
        t = tuple(map(float, sp[1:4]))
        result.append(t)
    return result

def main (data):


    j = 0
    for  i in len (data):
        while j != 3:
         centre = CalcCentre(data)
         j += 1 
         print centre 


if __name__ == '__main__':
    data = ReadPointCloud(r'Z:\data\NEHreflectance_Scanner 1_part.txt')

main(data)

now this is the full prog or code..i want to find the centroid of every three points in a 3D data point cloud... but the code is printing the centroid for whole data 3 times...

also i want to know what does 'dot() ' function do ?? Also why len(data) is not working in the code ??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.