List of objects to numpy array
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle4
--
Chapters
00:00 Question
02:11 Accepted answer (Score 0)
03:07 Thank you
--
Full question
https://stackoverflow.com/questions/2886...
Question links:
[KeyPoint objects]: http://docs.opencv.org/modules/features2...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #opencv #imageprocessing #numpy
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle4
--
Chapters
00:00 Question
02:11 Accepted answer (Score 0)
03:07 Thank you
--
Full question
https://stackoverflow.com/questions/2886...
Question links:
[KeyPoint objects]: http://docs.opencv.org/modules/features2...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #opencv #imageprocessing #numpy
#avk47
ACCEPTED ANSWER
Score 0
A trial case:
In [765]: class Keypoints(object):
def __init__(self):
self.pt=[1.,2.]
.....:
In [766]: keypoints=[Keypoints() for i in xrange(1000)]
In [767]: cd=np.array([k.pt for k in keypoints])
In [768]: cd
Out[768]:
array([[ 1., 2.],
[ 1., 2.],
[ 1., 2.],
...,
[ 1., 2.],
[ 1., 2.],
[ 1., 2.]])
In [769]: cd_x=cd[:,0]
In timeit tests, the keypoints step takes just as long as the cd calculation, 1ms.
But the 2 simpler iterations
cd_x=np.array([k.pt[0] for k in keypoints])
cd_y=np.array([k.pt[1] for k in keypoints])
takes half the time. I was expecting the single iteration to save time. But in these simple cases, the comprehension itself takes only half of the time, the rest is creating the array.
In [789]: timeit [k.pt[0] for k in keypoints]
10000 loops, best of 3: 136 us per loop
In [790]: timeit np.array([k.pt[0] for k in keypoints])
1000 loops, best of 3: 282 us per loop