Home > Software engineering >  Python numpy comparing two 3D Arrays for similarity
Python numpy comparing two 3D Arrays for similarity

Time:04-27

I am trying to compare two 3D numpy arrays to calculate similarity. I have found these two posts, which I am trying to stich together to something useful.

To make a long story short, I have two arrays created from 3D point clouds so they are filled with 3D coordinates, but because the 3D objects are different, the arrays have different lengths.

If requested, I can post some sample arrays, but they are 1000 points, so that would be a lot of text to post.

Here is what I am trying to do now. You can get array1 and array2 data here: https://pastebin.com/WbNvRUwG (array2 starts at line 1858).

array1 = [long np array with 3D coordinates]
array2 = [long np array with 3D coordinates]

array1_original = array1.copy()
if len(array1) < len(array2):
        array1, array2 = array2, array1
array_difference = np.subtract(array1, array2[:,None]) # The [:,None] is from the second link to make the arrays have same length to enable subtractraction
array_abs_difference = np.absolute(array_difference)
array_total_difference = np.sum(array_abs_difference)
similarity = 1 - (array_total_difference / 
np.sum(array1_original))

My array differences are fine and represent what I want, so the most similar arrays have small differences, but when I do the sum of array1_original it comes out way smaller than my differences and therefore my similarity score becomes negative.

I also tried to calculate the difference from an array filled with zeros to array1_original, but it comes out about the same.

Can anyone tell me why np.sum(array1_original) would not be bigger than np.sum(array_abs_difference)?

CodePudding user response:

The numpy comparison ended up being to slow, so I just used open3D instead. It works for me

  • Related