2021年3月17日星期三

Python Numpy get difference between 2 two-dimensional array

Well, I have a simple problem that is giving me a headache, basically I have two two-dimensional arrays, full of [x,y] coordinates, and I want to compare the first with the second and generate a third array that contains all the elements of the first array that doesn't appear in the second. It's simple but I couldn't make it work at all. The size varies a lot, the first array can have between a thousand and 2 million coordinates, while the first array has between 1 and a thousand.

This operation will occur many times and the larger the first array, the more times it will occur

sample:

arr1 = np.array([[0, 3], [0, 4], [1, 3], [1, 7], ])    arr2 = np.array([[0, 3], [1, 7]])    result = np.array([[0, 4], [1, 3]])  

In Depth: Basically I have a binary image with variable resolution, it is composed of 0 and 1 (255) and I analyze each pixel individually (with an algorithm that is already optimized), but (on purpose) every time this function is executed it analyzes only a fraction of the pixels, and when it is finished it gives me back all the coordinates of these pixels. The problem is that when it executes it runs the following code:

ones = np.argwhere(img == 255) # ones = pixels array  

It takes about 0.02 seconds and is by far the slowest part of the code. My idea is to create this variable once and, each time the function ends, it removes the parsed pixels and passes the new array as parameter to continue until the array is empty

https://stackoverflow.com/questions/66674537/python-numpy-get-difference-between-2-two-dimensional-array March 17, 2021 at 09:53PM

没有评论:

发表评论