简体   繁体   中英

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). That means the direction of the image is changed by 90 degrees. 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 only applys EXIF 'Orientation' tags with an OpenCV version >= 3.1. If you are stuck with a lower version and PIL is available:

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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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