简体   繁体   English

图像方向(python + openCV)

[英]Image Orientation (python+openCV)

Using Python and OpenCV, I try to read an image which size is (3264*2448), but the resulting size is always (2448*3264). 使用Python和OpenCV,我尝试读取大小为(3264 * 2448)的图像,但结果大小始终为(2448 * 3264)。 That means the direction of the image is changed by 90 degrees. 这意味着图像的方向改变了90度。 The code is following: 代码如下:

img1 = cv2.imread("C:\\Users\\test.jpg", 0) 
cv2.namedWindow("test", 0) 
cv2.imshow("test", img1)

the orignal image is this: 原始图像是这样的:

在此输入图像描述

but I get this image: 但我得到这个图像:

在此输入图像描述

I faced a similar issue in one program. 我在一个程序中遇到了类似的问题。 In my case, the problem was due to the camera orientation data stored in the image. 在我的情况下,问题是由于相机方向数据存储在图像中。

The problem was resolved after I used CV_LOAD_IMAGE_COLOR instead of CV_LOAD_IMAGE_UNCHANGED in OpenCV Java. 在OpenCV Java中使用CV_LOAD_IMAGE_COLOR而不是CV_LOAD_IMAGE_UNCHANGED后,问题得以解决。

OpenCV only applys EXIF 'Orientation' tags with an OpenCV version >= 3.1. OpenCV仅将EXIF'Orientation'标签应用于OpenCV版本> = 3.1。 If you are stuck with a lower version and PIL is available: 如果您遇到较低版本且PIL可用:

import PIL, cv2, numpy

path = 'test.jpg'
pix = PIL.Image.open(path) 
# get correction based on 'Orientation' from Exif (==Tag 274)
try:
    deg = {3:180,6:270,8:90}.get(pix._getexif().get(274,0),0)
except:
    deg = 0
if deg != 0:
    pix=pix.rotate(deg, expand=False)
# convert PIL -> opencv
im0 = numpy.array(pix)
if len(im0.shape)==3 and im0.shape[2] >= 3:
    # fix bgr rgb conventions
    # note: this removes a potential alpha-channel (e.g. if path point to a png)
    im0 = cv2.cvtColor(im0, cv2.COLOR_BGR2RGB) 

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

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