简体   繁体   English

ImageDataGenerator 中的图像数组与 CV2 读取的图像不匹配

[英]Image array from ImageDataGenerator does not match image read by CV2

I use the Image Data Generator with flow from directory to read in a single 224 X 224 X 3 image titled 1.jpg.我使用带有目录流的图像数据生成器来读取标题为 1.jpg 的单个 224 X 224 X 3 图像。 The result should be and identical to the initial image since image size is set to 224 X 244 in flow from directory.结果应该与初始图像相同,因为图像大小在目录中设置为 224 X 244。 I then read in the same image using cv2.然后我使用 cv2 读入相同的图像。 I then compare the array provided by the image data generator to the array produced by the cv2 read image.然后我将图像数据生成器提供的数组与 cv2 读取图像生成的数组进行比较。 I expected them to be identical but there are NOT.我希望它们是相同的,但没有。 The saved images from the generator and that saved by cv2 look identical when viewed.生成器保存的图像和 cv2 保存的图像在查看时看起来相同。 To check I printed the first 10 values in each array and they differ.为了检查,我打印了每个数组中的前 10 个值,它们不同。 Why?为什么? Code is shown below.代码如下所示。

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

The issue here was that openCV's standard method for reading images is to read them as BGR, so that blue is the first channel and red is the last.这里的问题是,openCV 读取图像的标准方法是将它们读取为 BGR,因此蓝色是第一个通道,红色是最后一个通道。 However, most other standard libraries will use RGB (keras's ImageGenerator and PIL.Image, as examples), and so you cannot directly compare cv2 objects with these objects, as they will be different mathematically.但是,大多数其他标准库将使用 RGB(例如 keras 的 ImageGenerator 和 PIL.Image),因此您不能直接将 cv2 对象与这些对象进行比较,因为它们在数学上会有所不同。

However, after saving to a picture file, this issue should not be an issue, as formatting like png is standard.但是,保存到图片文件后,这个问题应该不是问题,因为像png这样的格式是标准的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM