我将Image Data Generator与目录流一起使用来读入 一张名为1.jpg的224 X 224 X 3图像。结果应该是 由于图像尺寸设置为,因此与初始图像相同 224 X 244来自目录流。然后,我使用 cv2。然后,我比较图像数据生成器提供的数组 到由cv2读取映像生成的数组。我希望他们会 相同,但没有。从生成器保存的图像 与cv2保存的内容在查看时看起来相同。要检查我打印 每个数组的前10个值不同。为什么?代码如下所示。
dir=r'c:\Temp'# path to directory containing the sub directory imgtest.
# sub directory imgtest contains one directory called class1, it contains the image file 1.jpg which is already shape 224 X 224 X 3
test_dir=r'c:\Temp\imgtest' # path to test images directory
save_dir=dir # where the generator will storge the image on disk so it can be viewed later
test_gen=ImageDataGenerator().flow_from_directory(test_dir,
target_size=(224, 224), batch_size=1, class_mode='categorical',color_mode='rgb',save_to_dir=save_dir,save_format='jpeg' ,shuffle=False )
data=test_gen.next() # get the next batch from the generator -will be only 1 file which is 1.jpg
image1=data[0][0] # this is the single image 1.jpg provided by the test_gen
print ('for image provided by the generator image shape is ', image1.shape, ' data type is ',image1.dtype )
img_dir=r'c:\Temp\imgtest\class1'
img_path=os.path.join(img_dir, '1.jpg')
img=cv2.imread(img_path,cv2.IMREAD_UNCHANGED) # read in the original 1.jpg image from c:\Temp\imgtest\class1
img = img.astype('float32') #cv2 reads in data as int8 so convert to float32 to match generator data type
write_loc=os.path.join(dir, 'cvimage.jpg')
cv2.imwrite(write_loc, img) # save the image to disk so it can be viewed later
print ('for image read in by cv2 image shape is ', img.shape, ' data type is ', img.dtype)
compare_arrays = (image1 == img).all()
if compare_arrays:
print('arrays are the same')
else:
print('arrays do not match')
print ('Generator Data, CV2 Data Delta')
for i in range (0,10):
delta=image1[i][0][0] - img [i][0][0]
print( ' {0} {1} {2}'.format(image1[i][0][0],img [i][0][0], delta ))
# the results if running the code are shown below
Found 1 images belonging to 1 classes.
for image provided by the generator image shape is (224, 224, 3) data type is float32
for image read in by cv2 image shape is (224, 224, 3) data type is float32
arrays do not match
Generator Data, CV2 Data Delta
129.0 155.0 -26.0
141.0 164.0 -23.0
156.0 174.0 -18.0
165.0 180.0 -15.0
173.0 182.0 -9.0
179.0 183.0 -4.0
180.0 181.0 -1.0
181.0 178.0 3.0
185.0 176.0 9.0
180.0 170.0 10.0
The original image, generator image and cv2 image are shown in the composite image
[![composite of original, generator and cv2 saved images][1]][1]
[1]: https://i.stack.imgur.com/TCDDp.jpg
答案 0 :(得分:1)
这里的问题是openCV读取图像的标准方法是将它们读取为BGR,因此蓝色是第一个通道,红色是最后一个通道。但是,大多数其他标准库都将使用RGB(例如,keras的ImageGenerator和PIL.Image),因此您无法直接比较cv2对象与这些对象,因为它们在数学上会有所不同。
但是,保存到图片文件后,这个问题就不成问题了,因为png
这样的格式是标准的。