0

I am trying to append a sub image which is numpy array objects to a list.

    temp = []
    for color in COLOUR_RANGE:
       #Some code to extract the color region which is represented in numpy 
        if cv2.contourArea(coloured_cnt) > 400:
            
            x, y, w, h = cv2.boundingRect(coloured_cnt)

            coloursRect = cv2.cvtColor(img[y:y+h, x:x+w], cv2.COLOR_BGR2RGB)
            coloursRect_1D = np.vstack(coloursRect)/255

            temp.append(coloursRect_1D.copy())
            print(len(temp))
            print(type(temp))

But the length of the list is added by two every loop.

2
<class 'list'>
4
<class 'list'>
6
<class 'list'>
8
<class 'list'>
10

However, if i create a new list as follows

    temp = [
        np.vstack(coloursRectList['blue'])/255,
        np.vstack(coloursRectList['green'])/255,
        np.vstack(coloursRectList['yellow'])/255,
        np.vstack(coloursRectList['red'])/255,
        np.vstack(coloursRectList['black'])/255,

    print('len of temp', len(temp))
    print('type of temp', type(temp))

The output is as expected

len of temp 6
type of temp <class 'list'>

I prefer the first method as it would be more dynamic so that I can

EXTRACTED = np.array(np.vstack(temp))

I am wondering how to append numpy object to a list properly. Any help is appreciated.


Code tried to reproduce the error with numpy but it fails to reappear. I am not sure what goes wrong.

import numpy as np

mylist = []
a = np.zeros(shape=(96, 93, 3))
b = np.vstack(a)/255
print(a.shape)
print(type(a))
print(b.shape)
print(type(b))
for i in range(5):
    mylist.append(b.copy())
    print(len(mylist))

Output

(96, 93, 3)
<class 'numpy.ndarray'>
(8928, 3)
<class 'numpy.ndarray'>
1
2
3
4
5
7
  • 2
    Could you please add a snippet of code to reproduce the error (one that I can run without importing OpenCV)? Commented Dec 13, 2022 at 8:09
  • 3
    I guess that the problem is outside of the shown code. Anyway, can you add a print(temp) to the "print"s and show its output in the question? Commented Dec 13, 2022 at 8:11
  • 2
    list .append() method is appending only a single element regardless of the type, you must have "temp" changed in another place in the loop, please double check Commented Dec 13, 2022 at 8:11
  • @DaniMesejo I tried to reproduce the error but it doesn't work Commented Dec 13, 2022 at 8:25
  • 1
    @svfat thank you for your help! I figured out what went wrong. I thought temp = temp2 = [] could create two empty lists Commented Dec 13, 2022 at 8:38

1 Answer 1

1

Python list.append() method appends only a single element regardless of its type, it's not possible to get the list length increased by two after a single append call. You must have "temp" changed in another place in the loop, please double-check.

Sign up to request clarification or add additional context in comments.

Comments

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.