【发布时间】:2021-06-03 02:35:23
【问题描述】:
这是我用来为所有图像生成 CSV 文件的代码。
"""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')
我使用恢复此数据
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)
当我对该数据使用imshow 函数时,它会显示带有一些随机颜色像素的白框,但是当我使用imsave 保存它时,它会保存一个普通图像。
为什么imshow 看起来不一样?
我为这些脚本导入了 numpy、opencv 和 os。
【问题讨论】:
-
数据应该是 uint8
-
@Miki,试试看。
标签: python-3.x image numpy opencv3.1