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

如何解决来自ImageDataGenerator的图像数组与CV2读取的图像不匹配

我将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,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

解决方法

这里的问题是openCV读取图像的标准方法是将它们读取为BGR,因此蓝色是第一个通道,红色是最后一个通道。但是,大多数其他标准库都将使用RGB(例如,keras的ImageGenerator和PIL.Image),因此您无法直接比较cv2对象与这些对象,因为它们在数学上会有所不同。

但是,保存到图片文件后,这个问题就不成问题了,因为png这样的格式是标准的。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?