关于相机拍摄图片带旋转的问题

关于相机拍摄图片带旋转的问题


最近在用paddleDetection这个框架 然后在读数据的时候有点问题 报warning

2021-03-01 02:39:12,601-WARNING: The actual image width: 3024 is not equal to the width: 4032.0 in annotation, and update sample['w'] by actual image width.
2021-03-01 02:39:23,710-WARNING: The actual image height: 3264 is not equal to the height: 2448.0 in annotation, and update sample['h'] by actual image height.
2021-03-01 02:39:23,710-WARNING: The actual image width: 2448 is not equal to the width: 3264.0 in annotation, and update sample['w'] by actual image width.
2021-03-01 02:39:28,401-INFO: iter: 20, lr: 0.000005, 'loss': '1659.900146', eta: 23 days, 17:20:08, batch_cost: 4.09938 sec, ips: 1.95151 images/sec
2021-03-01 02:39:28,683-WARNING: The actual image height: 4032 is not equal to the height: 3024.0 in annotation, and update sample['h'] by actual image height.
2021-03-01 02:39:28,683-WARNING: The actual image width: 3024 is not equal to the width: 4032.0 in annotation, and update sample['w'] by actual image width.
2021-03-01 02:39:34,422-WARNING: The actual image height: 3264 is not equal to the height: 2448.0 in annotation, and update sample['h'] by actual image height.
2021-03-01 02:39:34,422-WARNING: The actual image width: 2448 is not equal to the width: 3264.0 in annotation, and update sample['w'] by actual image width.
2021-03-01 02:39:38,848-WARNING: The actual image height: 4032 is not equal to the height: 3024.0 in annotation, and update sample['h'] by actual image height.
2021-03-01 02:39:38,848-WARNING: The actual image width: 3024 is not equal to the width: 4032.0 in annotation, and update sample['w'] by actual image width.
/paddle/PaddleDetection/ppdet/data/transform/operators.py:131: DeprecationWarning: The 'warn' method is deprecated, use 'warning' instead

查了paddleDetection github 的issue 大概意思是 某图片 标签信息里的w/h与实际图片的w/h 不相等 然后自动帮你给更新了。
基于PIL读取图像的默认方式(忽略旋转信息)
基于CV2读取图像的默认方式(不忽略旋转信息)
一张带旋转的图片的exif信息

标签是用labelimg(基于PIL)打的 其实肉眼看都没啥问题 问题在于图片中的exif里有个旋转信息 出问题的图片都是自带旋转了 。然后labelimg是PIL封装来的 读的是原图 不管它exif信息的 ,直接cv2.imread是会考虑这个exif信息的。所以读的时候参数要带个cv2.IMREAD_COLOR+cv2.IMREAD_IGNORE_ORIENTATION 可参考这篇:关于使用opencv读取经过labelImg工具进行标注后的图像出现的框与图像旋转方向不一致的问题 [author: linkrain]
然后paddleDetection读图片是这样的 。。

if 'image' not in sample:
	 with open(sample['im_file'], 'rb') as f:
		sample['image'] = f.read()
	
	im = sample['image']
	data = np.frombuffer(im, dtype='uint8')
	im = cv2.imdecode(data, 1)  # BGR mode, but need RGB mode

只要把最后一句改成

im = cv2.imdecode(data,cv2.IMREAD_COLOR+cv2.IMREAD_IGNORE_ORIENTATION )

然后还有一点 我在paddle里还看到一个 如果PIL也想按exif的旋转信息来读 就用ImageOps.exif_transpose来进行变换。
PIL默认读图片是忽略exif的旋转信息的

origin_image = Image.open(sample['im_file']).convert('RGB')
origin_image = ImageOps.exif_transpose(origin_image)

最近发现,cv2.imread(path,flag)中的flag<0时 似乎也是忽略旋转信息的。
在这里插入图片描述

再附一个:计算机视觉模型效果不佳,你可能是被相机的Exif信息坑了

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Android相机拍摄图片可能因为设备方向的不同而导致旋转。这是因为Android相机的预览和拍照方向并不一定相同。解决这个问题的方法如下: 1. 读取图片的 Exif 信息,获取图片的方向信息。Exif 信息是一些存储在图片中的元数据,包括拍摄地点、相机型号、拍摄时间等。其中也包括了图片的方向信息。 2. 根据 Exif 中的方向信息,将图片进行旋转。可以使用 Bitmap 类的 createBitmap() 方法创建一个新的 Bitmap,并将原始图片旋转后绘制到新的 Bitmap 上。 以下是示例代码: ``` public static Bitmap rotateBitmap(Bitmap bitmap, String path) { int degree = getBitmapDegree(path); if (degree != 0) { Matrix matrix = new Matrix(); matrix.postRotate(degree); Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); bitmap.recycle(); return rotatedBitmap; } else { return bitmap; } } public static int getBitmapDegree(String path) { int degree = 0; try { ExifInterface exifInterface = new ExifInterface(path); int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: degree = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: degree = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: degree = 270; break; } } catch (IOException e) { e.printStackTrace(); } return degree; } ``` 在使用相机拍照后获取图片的时候,可以调用该方法对图片进行旋转,以得到正确的方向。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值