最近在做相机照片的物体检测,处理数据进行数据增强(data augument)时候,采用的读图操作:
img = cv2.imread(path)
然后在检查数据增强后的数据和对应的box框是否对应时候,发现,居然出问题了!新resize扩充的图像的Box与原图并不对应!!!
经查找资料,发现opencv 2.x版本在读取相机jpg照片时候,没有正确解析相机的EXIF信息,所以读取的长宽和实际的长宽是反的!
这里可以换用PIL Image库来读取照片,能够得到正确的长宽
img = Image.open(path)
也可以通过PIL Image库分析照片的exif中方向信息,然后对图像做对应的旋转变换:
def _orientation(image):
try:
exif = image._getexif()
except AttributeError:
exif = None
if exif:
orientation = exif.get(0x0112)
if orientation == 2:
image = image.transpose(Image.FLIP_LEFT_RIGHT)
elif orientation == 3:
image = image.rotate(180)
elif orientation == 4:
image = image.transpose(Image.FLIP_TOP_BOTTOM)
elif orientation == 5:
image = image.rotate(-90).transpose(Image.FLIP_LEFT_RIGHT)
elif orientation == 6:
image = image.rotate(-90)
elif orientation == 7:
image = image.rotate(90).transpose(Image.FLIP_LEFT_RIGHT)
elif orientation == 8:
image = image.rotate(90)
return image