【问题标题】:Difference between numpy 3D array and openCV imread objectnumpy 3D数组和openCV imread对象之间的区别
【发布时间】: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


【解决方案1】:

由于到目前为止没有人回答它,因此来自 open CV 的 imshow 显示 dtype int 和形状 (h,w,3) 的 numpy 数组,其值介于 0 和 255 之间或 dtype float 和值介于 0 和 1 之间,形状 (h ,w,3)。如果图像显示为白色和一些随机像素,则可能是因为值在 (0,255) 范围内,但由于 dtype 浮点数或其他任何值。

就像@Miki 建议的那样,将 dtype 更改为 uint 也应该可以解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-01
    • 2023-03-23
    • 2018-08-07
    • 2015-06-24
    • 2021-11-19
    • 2013-07-04
    • 1970-01-01
    相关资源
    最近更新 更多