0

I'm working on my first python project where my code extracts and draws contours of a growing blob in a video, and my goal is to create a matrix to fill with all of the contour pixel values for use as an input for a predictive ML model. My idea is to initialize an empty matrix of unspecified size, and append each contour array to it in each step of my for loop, but I've read that this is not a good approach as appending grows the matrix in each loop, and so numpy has to find a new contiguous block of RAM that can fit the new array each run through the loop. Is there a better approach for when I do not know the dimensions in advance? My loop runs through each contour with: for c in contours: and it seems that contours is a list filled with different c's which are ndarrays.

At one period of time,

In: c.shape
Out: (179, 1, 2)

and

In: contours[0]
Out: 
array([[[339, 211]],
 [[338, 212]],
 [[336, 212]],
...
 [[345, 211]]], dtype=int32)

Thanks in advance for any suggestions!

asked Jul 27, 2020 at 18:45
2
  • 1
    "an empty matrix of unspecified size" - no such beast in numpy (or Python). Appending to a list is relatively cheap - it just adds a reference in-place. Growing an array requires making a new array and copying all values (old and new) to it. Commented Jul 27, 2020 at 21:54
  • I appreciate it; that's what I ended up doing and thankfully it didn't noticeably hinder my performance. Commented Jul 27, 2020 at 22:00

1 Answer 1

1

I do not know of any dynamically sized array in numpy, but you should just append the lists to a list, and convert it to a numpy.array in a second step, potentially using np.stack, np.hstack, np.vstack, or np.block.

answered Jul 27, 2020 at 19:05

2 Comments

Thank you, I created an empty list and appended the current value at every step in the loop, and it didn't negatively impact my performance at all! Is there a benefit to converting my list of lists into an array of lists?
there shouldn't be. Stick to the lists as long as you can, in the last step, if you need to, convert to a numpy.array. Array of lists doesn't work AFAIK, and list of arrays is useless since you need to convert it back anyways.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.