1

I'm working on image processing with some images I collected myself. Dlib's dlib.load_rgb_image('image_path') method swaps the rows and columns on some images while OpenCV's cv2.imread('image_path') method does not.

I don't want to go in and rotate some of these offending images myself manually because I'm creating an app.

Check out the results below

img = dlib.load_rgb_image("myimg.jpg")
print(img.shape)

--------------------
OUTPUT: (1944, 2592, 3)
(the resultant image is rotated 90 degrees clockwise)

while OpenCV's method returns the correct shape:

img = cv2.imread("myimg.jpg")
print(img.shape)

--------------------
OUTPUT: (2592, 1944, 3)

Does anyone have any idea why this is happening?

I am also attaching the image details of one of the offending photos:

image.png

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
Rafay Khan
  • 1,070
  • 13
  • 25
  • it is on the dlib document that `dlib.load_rgb_image(filename: unicode) → numpy.ndarray[(rows,cols,3),uint8]`. It's just different in the order of width and height – Ha Bom Mar 28 '19 at 08:26
  • Yeah, I did check out the docs. This doesn't make sense the in a numpy array of an image the _rows=height_of_image_, and _cols=width_of_image_. – Rafay Khan Mar 28 '19 at 08:38
  • you can use `np.transpose` to get cv2 format – Ha Bom Mar 28 '19 at 08:40
  • Plus, as stated above the problem of rotated image occurs in **some** of the images, **not all** – Rafay Khan Mar 28 '19 at 08:40
  • I was planning to use Dlib all the way, due to some portability constraints. But again how to know which images to transpose and which to not transpose. – Rafay Khan Mar 28 '19 at 08:42
  • 1
    I see. I think if it happened only in some images, it could be an issue. You better inform this problem in the dlib github – Ha Bom Mar 28 '19 at 08:43
  • 1
    Some images have `Orientation` set in their EXIF data - particularly camera phone ones. OpenCV ignores it. You can check with `exiftool` or ImageMagick. – Mark Setchell Mar 28 '19 at 08:52
  • 1
    https://stackoverflow.com/a/48146220/2836621 – Mark Setchell Mar 28 '19 at 08:59

1 Answers1

2

Thanks to @Mark Setchell, for pointing me in the right direction.

The EXIF data is the key, here.

dlib.load_rgb_image() does not take into account the EXIF orientation metadata, so some images are read incorrectly. To remedy this EXIF orientation tag of an image needs to be checked to perform the correct rotation on it.

Here are a few good answers: Rotating an image with orientation specified in EXIF using Python without PIL including the thumbnail

Apparently, since OpenCV 3.1 imread handles EXIF orientation perfectly.

Rafay Khan
  • 1,070
  • 13
  • 25
  • Thank you for working it out and sharing back with the Stack Overflow community. You can accept your own answer and bag the points, by the way. – Mark Setchell Mar 28 '19 at 17:03
  • Thanks, will do that. Apparently, can't accept my own answer for at least two days. LOL – Rafay Khan Mar 29 '19 at 05:27