简体   繁体   English

opencv imwrite,图像旋转

[英]opencv imwrite, image get rotated

I am using opencv in python (cv2) to do some processing on images with format jpg, png and jpeg, JPG.我在 python (cv2) 中使用 opencv 对 jpg、png 和 jpeg、JPG 格式的图像进行一些处理。 I am doing a test that write image to disk using "cv2.imwrite" right after reading from "cv2.imread".我正在做一个测试,在从“cv2.imread”读取后立即使用“cv2.imwrite”将图像写入磁盘。 I found part of the image get rotated, some of them rotate 90d, some rotate 180d.我发现图像的一部分被旋转了,其中一些旋转了 90d,一些旋转了 180d。 But most image keep the right orientation.但大多数图像保持正确的方向。 I cannot conclude a pattern that causes this rotation.我无法得出导致这种旋转的模式。 Anyone knows more details?有谁知道更多细节? Thanks!谢谢!

It sounds like the EXIF orientation flag:这听起来像 EXIF 方向标志:

Why Are My Images Wrongly Displayed?为什么我的图像显示错误?
When you use a photo viewer to check the image on your computer, if the photo viewer can read the Exif info and respects that info, it will automatically rotate the raw image based on the orientation info.当您使用照片查看器检查计算机上的图像时,如果照片查看器可以读取 Exif 信息并尊重该信息,它将根据方向信息自动旋转原始图像。 The end result is that you can see the correctly-oriented photo no matter how it is actually stored.最终结果是无论实际存储方式如何,您都可以看到正确定向的照片。

Meaning the image looks correct when viewed on the desktop, because the file-info says 'rotate this image 90d before showing'.这意味着在桌面上查看时图像看起来正确,因为文件信息显示“在显示之前将此图像旋转 90d”。 If your script doesn't read the EXIF info, it stores a new image, without the flag, so the image is displayed rotated.如果您的脚本没有读取 EXIF 信息,它会存储一个没有标志的新图像,因此图像会旋转显示。

However, according to this answer opencv handles this automatically since version 3.1.但是,根据此答案opencv 自 3.1 版以来自动处理此问题。 Is your openCV version current?您的 openCV 版本是最新的吗?

Source article来源文章

Update更新
A comment in this question suggests you can't write exif data with openCV. 此问题中的评论表明您无法使用 openCV 编写 exif 数据。
You can also use exifread or PIL to get the exif data and rotate the image in your script.您还可以使用exifreadPIL获取 exif 数据并在脚本中旋转图像。

I had a similar problem when saving frame images using cv2.imwrite() when reading a video file using cv2.VideoCapture().在使用 cv2.VideoCapture() 读取视频文件时,我在使用 cv2.imwrite() 保存帧图像时遇到了类似的问题。 It was due to OpenCV not properly reading video orientation/rotation meta data.这是由于 OpenCV 没有正确读取视频方向/旋转元数据。 This was still an issue for me even with OpenCV 4.即使使用 OpenCV 4,这对我来说仍然是一个问题。

I ended up using scikit-video - a Python wrapper around ffmpeg.我最终使用了 scikit-video - ffmpeg 周围的 Python 包装器。 No more rotation issues: http://www.scikit-video.org/stable/io.html没有更多的旋转问题: http://www.scikit-video.org/stable/io.html

One possible solution is to change cv2.IMREAD_UNCHANGED to cv2.IMREAD_COLOR while loading image with imdecode .一种可能的解决方案是在使用cv2.IMREAD_COLOR加载图像时将cv2.IMREAD_UNCHANGED更改为imdecode From some reason "unchanged" is not able to read EXIF metadata correctly由于某种原因,“未更改”无法正确读取 EXIF 元数据

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

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