Why is matlab reading some images in rotated by 90 degrees?

45 views (last 30 days)
I am reading in many portrait images for a face recognition project.
However Matlab is reading some images into the workspace rotated by 90 degrees, while others have the correct orientation (i.e. the orientation seen in the directory they are stored in).
The only obvious discriminating factor is the sub-folder the images come from, e.g. subfolders 1-45 are OK, but images within subfolders 46-50 are being read in incorrectly.
The method being used is as follows:
I = imread('..\046\IMG_0242.jpg');
imshow(I)
  3 Comments
Guillaume
Guillaume on 4 Apr 2018
It's either a bug in your code or the files are stored rotated 90 degrees.
It is extremely unlikely that it is a bug in matlab. Someone would have noticed by now if imread did not read images properly.
So... show us your code.
Also, what format are the images stored as?
Guillaume
Guillaume on 4 Apr 2018
Can you actually show the whole output of imfinfo for a correct and rotated image?
Note that some jpeg images may also have EXIF metadata. One possible EXIF tag is an orientation tag that says how the image should be rotated after it's been read.

Sign in to comment.

Answers (1)

DGM
DGM on 5 Jan 2023
If you're loading photographs from a digital camera (which is what that looks like), this is a likely outcome. Imread() does not check the Orientation tag in EXIF metadata, so if your camera was rotated during the shot, the image won't be reoriented as it will in other software.
See these threads.
Attached is a reduced-compatibility version of one of the tools from MIMT. The version in MIMT has broader version support and doesn't require IPT, but it relies on MIMT, so it won't run here on the forum.
Imreadort() is a simple wrapper for imread() that attempts to catch any cases where Orientation metadata is present. If orientation info is present and understandable, it will rectify the image.
inpict = imread('Landscape_5.jpg'); % read it with imread()
imshow(inpict)
inpict = imreadort('Landscape_5.jpg'); % read it with imreadort() instead
imshow(inpict)
  3 Comments
DGM
DGM on 27 Aug 2024
The copy attached above should work fine. The version that comes with MIMT simply extends the functionality to versions older than R2014a. I assume that you probably don't need that.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!