我正在尝试使用cv2.imshow()显示图像,但是在我将其与数据(所有图像集)连接起来后,batch_data(原始图像)被更改了。我注意到可以确定原始图像是如何改变的。
data = np.array([]).reshape([0, IMG_WIDTH , IMG_HEIGHT ,IMG_DEPTH ])
label = np.array([])
batch_label = np.array([255]).reshape(1) #label number represent background
x = True
for (n,address) in enumerate(address_list):
print("Reading all images with background from ", address)
batch_data = cv2.imread(image_dir + address)
dim = (IMG_WIDTH ,IMG_HEIGHT)
if batch_data is not None:
batch_data = cv2.resize(batch_data,dim, interpolation = cv2.INTER_NEAREST)
else:
print("batch_data is not read.")
batch_data = np.expand_dims(batch_data, axis= 0)
data = np.concatenate((data,batch_data))
label = np.concatenate((label,batch_label))
while x:
print("batch_data.shape",batch_data.shape)
print("data.shape", data.shape)
print((np.squeeze(batch_data, axis=0) == data[n,...]).all()) # return true
cv2.imshow('image', np.squeeze(batch_data, axis= 0)) # show original image
cv2.imshow('image2', data[n,...]) #show original image but color is alter to white and red
cv2.waitKey(0)
cv2.destroyAllWindows()
x = False
我认为cv2.imshow('image2',data [n,...])显示原始图像,因为我尝试使用转置将axis = 1交换为axis = 2,并且红色斑点也相应地移动了。我可能是错的。
有人可以发现错误吗?我觉得这将是一个非常愚蠢的错误,但我只是找不到。
答案 0 :(得分:1)
我认为这是一个数据类型问题。
尝试将data
从float64
更改为uint8
:
data = np.array([], dtype=np.uint8).reshape([0, IMG_WIDTH , IMG_HEIGHT ,IMG_DEPTH])
白色和红色是表示饱和的符号。 float64
范围应为[0, 1]
,而uint8
则应为[0, 255]
。您可以找到有关此问题的更多信息here。