0

I have 3d array like as:

arr = [[-0.2384903,   0.28638432, -0.52100003], [-0.23179091,  0.28418559, -0.51700002], [-0.22692253,  0.28418559, -0.51700002], [-0.22046956,  0.28528494, -0.51899999], [-0.21518661,  0.28638432, -0.52100003], [-0.24017692,  0.28570513, -0.52200001], [-0.23345869,  0.28351582, -0.51800001], [-0.22813963,  0.2829685 , -0.51700002], [-0.22370311,  0.28351582, -0.51800001], [-0.21967019,  0.28461046, -0.51999998], [-0.21518661,  0.28515781, -0.52100003], [-0.20773969,  0.28844177, -0.52700001], [-0.24017692,  0.28447627, -0.52200001], [-0.23345869,  0.28229637, -0.51800001], [-0.22813963,  0.2817514 , -0.51700002], [-0.22370311,  0.28229637, -0.51800001], [-0.21967019,  0.2833863 , -0.51999998], [-0.21518661,  0.2839313 , -0.52100003], [-0.21025782,  0.2855662 , -0.52399999], [-0.20728272,  0.28829107, -0.52899998], [-0.24017692,  0.2832474 , -0.52200001], [-0.23345869,  0.28107692, -0.51800001], [-0.22813963,  0.28053431, -0.51700002], [-0.22370311,  0.28107692, -0.51800001], [-0.21967019,  0.28216215, -0.51999998], [-0.21559964,  0.2832474 , -0.52200001], [-0.21149139,  0.28433263, -0.52399999], [-0.20813387,  0.28650311, -0.528     ], [-0.2384903 ,  0.28147828, -0.52100003], [-0.23223924,  0.27985747, -0.51800001], [-0.22736145,  0.27985747, -0.51800001], [-0.22248366,  0.27985747, -0.51800001], [-0.21886614,  0.28147828, -0.52100003], [-0.21437077,  0.28201854, -0.52200001], [-0.21065907,  0.28363932, -0.52499998], [-0.20806641,  0.28688093, -0.53100002], [-0.23513117,  0.27917593, -0.51899999], [-0.22980034,  0.27863803, -0.51800001], [-0.22492255,  0.27863803, -0.51800001], [-0.22089435,  0.27971384, -0.51999998], [-0.2168285 ,  0.28078968, -0.52200001], [-0.21272496,  0.28186548, -0.52399999], [-0.20937686,  0.28401713, -0.528     ], [-0.23726379,  0.27902526, -0.52100003], [-0.23101979,  0.27741858, -0.51800001], [-0.22657856,  0.27795413, -0.51899999], [-0.2221185 ,  0.27848968, -0.51999998], [-0.21805736,  0.27956081, -0.52200001], [-0.21395853,  0.28063191, -0.52399999], [-0.21061985,  0.28277415, -0.528     ], [-0.23986355,  0.27939834, -0.52399999], [-0.2331359 ,  0.27726552, -0.51999998], [-0.22780037,  0.27673233, -0.51899999], [-0.22334266,  0.27726552, -0.51999998], [-0.21928623,  0.27833195, -0.52200001], [-0.2151921 ,  0.27939834, -0.52399999], [-0.21186284,  0.28153116, -0.528     ], [-0.24821244,  0.28347325, -0.53399998], [-0.23649032,  0.27710308, -0.52200001], [-0.23068759,  0.27604137, -0.51999998], [-0.22579097,  0.27604137, -0.51999998], [-0.22131916,  0.27657224, -0.52100003], [-0.21765925,  0.27816477, -0.52399999], [-0.21394286,  0.27975733, -0.52700001], [-0.2113596 ,  0.28294241, -0.53299999], [-0.23908537,  0.27745968, -0.52499998], [-0.23235775,  0.27534573, -0.52100003], [-0.22701513,  0.27481721, -0.51999998], [-0.22254567,  0.27534573, -0.52100003], [-0.21889282,  0.2769312 , -0.52399999], [-0.21518349,  0.27851669, -0.52700001], [-0.21221546,  0.28115916, -0.53200001], [-0.24215232,  0.27832832, -0.52899998], [-0.23448093,  0.27517149, -0.523     ], [-0.22867822,  0.27411922, -0.52100003], [-0.22420168,  0.27464536, -0.52200001], [-0.22012639,  0.27569762, -0.52399999], [-0.2168348 ,  0.27780219, -0.528     ], [-0.21346787,  0.27990676, -0.53200001], [-0.24090698,  0.27708298, -0.52899998], [-0.23369569,  0.27446405, -0.52399999], [-0.22788828,  0.27341649, -0.52200001], [-0.22382711,  0.27446405, -0.52399999], [-0.2197283 ,  0.27551164, -0.52600002], [-0.21640843,  0.27760676, -0.52999997], [-0.24398335,  0.27792337, -0.53299999], [-0.23582595,  0.27427336, -0.52600002], [-0.22955606,  0.27270905, -0.523     ], [-0.22506068,  0.27323048, -0.52399999], [-0.22180675,  0.27531621, -0.528     ], [-0.21806682,  0.27688052, -0.53100002], [-0.24272859,  0.27666861, -0.53299999], [-0.23503365,  0.27355415, -0.52700001], [-0.2287614 ,  0.27199691, -0.52399999], [-0.22468143,  0.27303508, -0.52600002], [-0.22139922,  0.27511137, -0.52999997], [-0.21804171,  0.27718768, -0.53399998], [-0.23592561,  0.27334695, -0.52899998], [-0.22963455,  0.2717968 , -0.52600002], [-0.22553572,  0.27283023, -0.528     ], [-0.2222347 ,  0.27489713, -0.53200001], [-0.23681759,  0.27313037, -0.53100002], [-0.23050767,  0.27158725, -0.528     ], [-0.22639001,  0.27261597, -0.52999997], [-0.22307016,  0.27467346, -0.53399998], [-0.23181739,  0.27188031, -0.53100002], [-0.22767147,  0.27290433, -0.53299999], [-0.23143575,  0.27164957, -0.53299999], [-0.15836202,  0.20225762, -0.53200001], [-0.16369571,  0.19987172, -0.52899998], [-0.15781427,  0.19873824, -0.52600002], [-0.15286115,  0.19873824, -0.52600002], [-0.14987638,  0.20138304, -0.53299999], [-0.15937662,  0.19637353, -0.523     ], [-0.15415643,  0.19599806, -0.52200001], [-0.15038459,  0.19749996, -0.52600002], [-0.16307028,  0.19514231, -0.523     ], [-0.15784303,  0.19476919, -0.52200001], [-0.15292757,  0.19476919, -0.52200001], [-0.14914631,  0.19626168, -0.52600002], [-0.1461121 ,  0.19887352, -0.53299999], [-0.16152962,  0.19354033, -0.52200001], [-0.15661416,  0.19354033, -0.52200001], [-0.15169871,  0.19354033, -0.52200001], [-0.14790803,  0.1950234 , -0.52600002], [-0.14431379,  0.19687723, -0.53100002], [-0.17176128,  0.19415353, -0.52700001], [-0.16553271,  0.19267987, -0.523     ], [-0.16030075,  0.19231147, -0.52200001], [-0.1553853 ,  0.19231147, -0.52200001], [-0.15046984,  0.19231147, -0.52200001], [-0.14639089,  0.19341669, -0.52499998], [-0.14306374,  0.19562718, -0.53100002], [-0.17019708,  0.19254684, -0.52600002], [-0.16430149,  0.19144866, -0.523     ], [-0.15876716,  0.19071655, -0.52100003], [-0.15415643,  0.1910826 , -0.52200001], [-0.14924098,  0.1910826 , -0.52200001], [-0.14487849,  0.19181471, -0.52399999], [-0.14154661,  0.19401105, -0.52999997], [-0.1689588 ,  0.19130856, -0.52600002], [-0.16275848,  0.18985374, -0.52200001], [-0.15754065,  0.18949004, -0.52100003], [-0.15263461,  0.18949004, -0.52100003], [-0.14801211,  0.18985374, -0.52200001], [-0.14364491,  0.19058114, -0.52399999], [-0.14029891,  0.19276336, -0.52999997], [-0.1689588 ,  0.19007028, -0.52600002], [-0.16307028,  0.18898622, -0.523     ], [-0.15784303,  0.18862487, -0.52200001], [-0.15292757,  0.18862487, -0.52200001], [-0.14801211,  0.18862487, -0.52200001], [-0.14364491,  0.18934756, -0.52399999], [-0.13976949,  0.19079297, -0.528     ], [-0.17365846,  0.18990897, -0.52899998], [-0.16708279,  0.18811399, -0.52399999], [-0.16152962,  0.18739601, -0.52200001], [-0.15661416,  0.18739601, -0.52200001], [-0.15169871,  0.18739601, -0.52200001], [-0.14678325,  0.18739601, -0.52200001], [-0.14241134,  0.18811399, -0.52399999], [-0.1385265 ,  0.18954998, -0.528     ], [-0.17523443,  0.18902027, -0.52999997], [-0.1689588 ,  0.18759372, -0.52600002], [-0.16307028,  0.18652378, -0.523     ], [-0.15784303,  0.18616715, -0.52200001], [-0.15292757,  0.18616715, -0.52200001], [-0.14801211,  0.18616715, -0.52200001], [-0.14364491,  0.18688042, -0.52399999], [-0.13924006,  0.18759372, -0.52600002], [-0.13607402,  0.1900902 , -0.5329999 ],

how i can set x, y area radus and find local maximum? i have some code, but its return wrong num maximum, what could be the reasons? I changed the parameters

points  = np.array(arr)
# list to collect local maxima
local_maxima = []

# distance in x / y to define region of interest around current center coordinate
radius = 0.15

for i in range(points.shape[0]):
        # radial mask with radius, could be beautified via numpy.linalg
        mask = np.sqrt((points[:, 0] - points[i, 0])**2 + (points[:, 1] - points[i, 1])**2) <= radius
        # if current z value equals z_max in current region of interest, append to result list
        if points[i, 2] == np.max(points[mask], axis=0)[2]:
            local_maxima.append(tuple(points[i]))

print(len(local_maxima))

I need find 9 peak on my point cloud enter image description here

5
  • is the distance between adjacent points always equal 1= Commented Jul 5, 2021 at 9:25
  • Or is each point (x,y,z)? Commented Jul 5, 2021 at 9:26
  • For one, you are missing a **2 on the radius in your comparison Commented Jul 5, 2021 at 9:44
  • As a second I posted an optimized version using pandas as an answer Commented Jul 5, 2021 at 9:44
  • @FloLie why i missing **2 on the radius, i have sqrt in string? Or do I still need to exponentiate the radius? Commented Jul 5, 2021 at 10:53

1 Answer 1

1

Your comparison is wrong, if the points are within the circle, it needs to be <= radius**2. Than your code works, also find below an optimized version using pandas, without a loop


import pandas as pd

def in_cycle(center_x, center_y, radius, x, y):
    return (x - center_x)**2 + (y - center_y)**2 <= radius**2


l = arr = [[-0.2384903,   0.28638432, -0.52100003], [-0.23179091,  0.28418559, -0.51700002], [-0.22692253,  0.28418559, -0.51700002], [-0.22046956,  0.28528494, -0.51899999], [-0.21518661,  0.28638432, -0.52100003], [-0.24017692,  0.28570513, -0.52200001], [-0.23345869,  0.28351582, -0.51800001], [-0.22813963,  0.2829685 , -0.51700002], [-0.22370311,  0.28351582, -0.51800001], [-0.21967019,  0.28461046, -0.51999998], [-0.21518661,  0.28515781, -0.52100003], [-0.20773969,  0.28844177, -0.52700001], [-0.24017692,  0.28447627, -0.52200001], [-0.23345869,  0.28229637, -0.51800001], [-0.22813963,  0.2817514 , -0.51700002], [-0.22370311,  0.28229637, -0.51800001], [-0.21967019,  0.2833863 , -0.51999998], [-0.21518661,  0.2839313 , -0.52100003], [-0.21025782,  0.2855662 , -0.52399999], [-0.20728272,  0.28829107, -0.52899998], [-0.24017692,  0.2832474 , -0.52200001], [-0.23345869,  0.28107692, -0.51800001], [-0.22813963,  0.28053431, -0.51700002], [-0.22370311,  0.28107692, -0.51800001], [-0.21967019,  0.28216215, -0.51999998], [-0.21559964,  0.2832474 , -0.52200001], [-0.21149139,  0.28433263, -0.52399999], [-0.20813387,  0.28650311, -0.528     ], [-0.2384903 ,  0.28147828, -0.52100003], [-0.23223924,  0.27985747, -0.51800001], [-0.22736145,  0.27985747, -0.51800001], [-0.22248366,  0.27985747, -0.51800001], [-0.21886614,  0.28147828, -0.52100003], [-0.21437077,  0.28201854, -0.52200001], [-0.21065907,  0.28363932, -0.52499998], [-0.20806641,  0.28688093, -0.53100002], [-0.23513117,  0.27917593, -0.51899999], [-0.22980034,  0.27863803, -0.51800001], [-0.22492255,  0.27863803, -0.51800001], [-0.22089435,  0.27971384, -0.51999998], [-0.2168285 ,  0.28078968, -0.52200001], [-0.21272496,  0.28186548, -0.52399999], [-0.20937686,  0.28401713, -0.528     ], [-0.23726379,  0.27902526, -0.52100003], [-0.23101979,  0.27741858, -0.51800001], [-0.22657856,  0.27795413, -0.51899999], [-0.2221185 ,  0.27848968, -0.51999998], [-0.21805736,  0.27956081, -0.52200001], [-0.21395853,  0.28063191, -0.52399999], [-0.21061985,  0.28277415, -0.528     ], [-0.23986355,  0.27939834, -0.52399999], [-0.2331359 ,  0.27726552, -0.51999998], [-0.22780037,  0.27673233, -0.51899999], [-0.22334266,  0.27726552, -0.51999998], [-0.21928623,  0.27833195, -0.52200001], [-0.2151921 ,  0.27939834, -0.52399999], [-0.21186284,  0.28153116, -0.528     ], [-0.24821244,  0.28347325, -0.53399998], [-0.23649032,  0.27710308, -0.52200001], [-0.23068759,  0.27604137, -0.51999998], [-0.22579097,  0.27604137, -0.51999998], [-0.22131916,  0.27657224, -0.52100003], [-0.21765925,  0.27816477, -0.52399999], [-0.21394286,  0.27975733, -0.52700001], [-0.2113596 ,  0.28294241, -0.53299999], [-0.23908537,  0.27745968, -0.52499998], [-0.23235775,  0.27534573, -0.52100003], [-0.22701513,  0.27481721, -0.51999998], [-0.22254567,  0.27534573, -0.52100003], [-0.21889282,  0.2769312 , -0.52399999], [-0.21518349,  0.27851669, -0.52700001], [-0.21221546,  0.28115916, -0.53200001], [-0.24215232,  0.27832832, -0.52899998], [-0.23448093,  0.27517149, -0.523     ], [-0.22867822,  0.27411922, -0.52100003], [-0.22420168,  0.27464536, -0.52200001], [-0.22012639,  0.27569762, -0.52399999], [-0.2168348 ,  0.27780219, -0.528     ], [-0.21346787,  0.27990676, -0.53200001], [-0.24090698,  0.27708298, -0.52899998], [-0.23369569,  0.27446405, -0.52399999], [-0.22788828,  0.27341649, -0.52200001], [-0.22382711,  0.27446405, -0.52399999], [-0.2197283 ,  0.27551164, -0.52600002], [-0.21640843,  0.27760676, -0.52999997], [-0.24398335,  0.27792337, -0.53299999], [-0.23582595,  0.27427336, -0.52600002], [-0.22955606,  0.27270905, -0.523     ], [-0.22506068,  0.27323048, -0.52399999], [-0.22180675,  0.27531621, -0.528     ], [-0.21806682,  0.27688052, -0.53100002], [-0.24272859,  0.27666861, -0.53299999], [-0.23503365,  0.27355415, -0.52700001], [-0.2287614 ,  0.27199691, -0.52399999], [-0.22468143,  0.27303508, -0.52600002], [-0.22139922,  0.27511137, -0.52999997], [-0.21804171,  0.27718768, -0.53399998], [-0.23592561,  0.27334695, -0.52899998], [-0.22963455,  0.2717968 , -0.52600002], [-0.22553572,  0.27283023, -0.528     ], [-0.2222347 ,  0.27489713, -0.53200001], [-0.23681759,  0.27313037, -0.53100002], [-0.23050767,  0.27158725, -0.528     ], [-0.22639001,  0.27261597, -0.52999997], [-0.22307016,  0.27467346, -0.53399998], [-0.23181739,  0.27188031, -0.53100002], [-0.22767147,  0.27290433, -0.53299999], [-0.23143575,  0.27164957, -0.53299999], [-0.15836202,  0.20225762, -0.53200001], [-0.16369571,  0.19987172, -0.52899998], [-0.15781427,  0.19873824, -0.52600002], [-0.15286115,  0.19873824, -0.52600002], [-0.14987638,  0.20138304, -0.53299999], [-0.15937662,  0.19637353, -0.523     ], [-0.15415643,  0.19599806, -0.52200001], [-0.15038459,  0.19749996, -0.52600002], [-0.16307028,  0.19514231, -0.523     ], [-0.15784303,  0.19476919, -0.52200001], [-0.15292757,  0.19476919, -0.52200001], [-0.14914631,  0.19626168, -0.52600002], [-0.1461121 ,  0.19887352, -0.53299999], [-0.16152962,  0.19354033, -0.52200001], [-0.15661416,  0.19354033, -0.52200001], [-0.15169871,  0.19354033, -0.52200001], [-0.14790803,  0.1950234 , -0.52600002], [-0.14431379,  0.19687723, -0.53100002], [-0.17176128,  0.19415353, -0.52700001], [-0.16553271,  0.19267987, -0.523     ], [-0.16030075,  0.19231147, -0.52200001], [-0.1553853 ,  0.19231147, -0.52200001], [-0.15046984,  0.19231147, -0.52200001], [-0.14639089,  0.19341669, -0.52499998], [-0.14306374,  0.19562718, -0.53100002], [-0.17019708,  0.19254684, -0.52600002], [-0.16430149,  0.19144866, -0.523     ], [-0.15876716,  0.19071655, -0.52100003], [-0.15415643,  0.1910826 , -0.52200001], [-0.14924098,  0.1910826 , -0.52200001], [-0.14487849,  0.19181471, -0.52399999], [-0.14154661,  0.19401105, -0.52999997], [-0.1689588 ,  0.19130856, -0.52600002], [-0.16275848,  0.18985374, -0.52200001], [-0.15754065,  0.18949004, -0.52100003], [-0.15263461,  0.18949004, -0.52100003], [-0.14801211,  0.18985374, -0.52200001], [-0.14364491,  0.19058114, -0.52399999], [-0.14029891,  0.19276336, -0.52999997], [-0.1689588 ,  0.19007028, -0.52600002], [-0.16307028,  0.18898622, -0.523     ], [-0.15784303,  0.18862487, -0.52200001], [-0.15292757,  0.18862487, -0.52200001], [-0.14801211,  0.18862487, -0.52200001], [-0.14364491,  0.18934756, -0.52399999], [-0.13976949,  0.19079297, -0.528     ], [-0.17365846,  0.18990897, -0.52899998], [-0.16708279,  0.18811399, -0.52399999], [-0.16152962,  0.18739601, -0.52200001], [-0.15661416,  0.18739601, -0.52200001], [-0.15169871,  0.18739601, -0.52200001], [-0.14678325,  0.18739601, -0.52200001], [-0.14241134,  0.18811399, -0.52399999], [-0.1385265 ,  0.18954998, -0.528     ], [-0.17523443,  0.18902027, -0.52999997], [-0.1689588 ,  0.18759372, -0.52600002], [-0.16307028,  0.18652378, -0.523     ], [-0.15784303,  0.18616715, -0.52200001], [-0.15292757,  0.18616715, -0.52200001], [-0.14801211,  0.18616715, -0.52200001], [-0.14364491,  0.18688042, -0.52399999], [-0.13924006,  0.18759372, -0.52600002], [-0.13607402,  0.1900902 , -0.5329999 ]]
df = pd.DataFrame(l, columns=["x","y", "z"])

radius = 3
center_x = 0
center_y = 0

df_in_radius = df[df.apply(lambda x: in_cycle(center_x,center_y,radius, x.x,x.y), axis=1)]
local_maximum = df_in_radius.loc[df_in_radius.z.idxmax() ]
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks! I'll check and unsubscribe a little later, but so far it looks correct)
Hmm, I don't really understand the logic of the code, maybe I did not correctly indicate the question, but my task is to find all the peaks in my array in PointCloud, now I will add a photo to the post, or did you do the same?)
Hi, thanks for clarification. No i didn't, I only looked for the local maximum in a given radius, you however are looking for all maxima within the grid right?
I think I would take an other approach to the radius version and try utilize Delaunay Triangulation. Basically you create triangles in your data grid, such that no point lies within another triangle. Then you find the set of points, that has the maximum z value in all triangles it is contained in, thus being a local maximum
Alternatively, and easier, you can sort all points by descending Z values (highest first), having the global maximum as the first entry. You take that, and then exclude all other entries in the list, within the radius of that maximum from the list. Take the next on and repeat the process. However, that will leave you with quiet a few "wrong" maxima, that are on the 'hill' of another one, just outside the radius
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.