[Python] Difference between numpy 3D array and openCV imread object

Discussão em 'Python' iniciado por Stack, Junho 15, 2025.

  1. Stack

    Stack Membro Participativo

    This is the code I used to generate CSV file for all images.

    """Generate data.csv from data folder in working directory. """
    from cv2 import imread,cvtColor,COLOR_RGB2GRAY,resize
    from os import listdir
    from numpy import array,savetxt,hstack

    data=[]
    for c in listdir('data'):
    i = listdir('data').index(c)
    print('Reading',c)
    for sample in listdir('data/'+c):
    path = 'data/' + c + '/' + sample
    img = imread(path)
    #img = cvtColor(img,COLOR_RGB2GRAY)
    img = resize(img,(150,150))
    row = hstack([img.ravel(),i])
    data.append(row)

    data = array(data)
    savetxt('data.csv',data,delimiter=',',fmt='%i')


    i recover this data using

    print('Reading data...')
    data = genfromtxt('data.csv',delimiter=',')
    X = reshape(data[:,:-1],(data.shape[0],150,150,3))
    y = data[:,-1]
    print('Data: ',X.shape)


    when i use imshow function on this data, it shows white box with some random colored pixels, but when i save this with imsave it saves a normal image.

    Why it appeared different for imshow?

    I imported numpy, opencv and os for these scripts.

    Continue reading...

Compartilhe esta Página