Image height and width getting swapped when read using opencv imread
Asked Answered
T

2

7

When I read an image using opencv imread function, I find its height and width being swapped as what it should be. Like my original image is of dimensions (610 by 406) but on being read using opencv::imread function, its dimensions are 406 by 610. Also, if I rotate my original image before passing it to the function then also, no change. The image read still has original dimensions.

Please see example code and images for clarification: So, below I have provided the input images: one is original and second one is rotated (I rotated it using windows rotate command, by right-clicking and selecting 'rotate right'). Output I get for both the images is same. It seems to me that rotating image did not actually change its shape. I think so because, when I try to put the rotated image here then also, it was showing the un-rotated version of it only (in the preview) so, I had to take a screen-capture of it and then, paste it here.

This is the code:

import cv2
import numpy as np
import sys
import os

image = cv2.imread("C:/img_8075.jpg")
print "image shape: ",image.shape
cv2.imshow("image",image)
cv2.waitKey(0)
image2 = cv2.imread("C:/img_8075_Rotated.jpg")
print "image shape: ",image2.shape
cv2.imshow("image",image2)
cv2.waitKey(0)

The result I get for this is: image shape: (406,610,3) image shape: (406,610,3) for both the images.

I am unable to paste input/output pictures here since, it says you should have '10 reputations' and I have just joined. Any suggestions would be helpful. thanks!

Traipse answered 21/4, 2015 at 4:33 Comment(3)
Same dimensions even after rotating the image? Are you sure you're not missing something?Consecrate
how did you find out that the dimensions are swapped? .cols should be your original image width and .rows should be your original image height. If that isn't the case, maybe your image viewer internally rotates the images before displaying?!?Sanborne
Hey Micka, I think you are right in saying my image viewer internally rotated the image before displaying. But, I want to use this rotated image only as input to my opencv (imread function). I am working on a class project where I read images from a folder to create its composite image. So, at times, images in the folder are not oriented properly. So, I rotate them (using, windows right-click->rotate right command). But, opencv reads the original image only. What should I do?Traipse
G
4

I believe you are just getting the conventions mixed up. OpenCV Mat structures can be accessed (ROW,COLUMN).

So a 1920x1080 image will be 1080 ROWS by 1920 COLUMNS (1080,1920)

Guerin answered 21/4, 2015 at 4:44 Comment(9)
You can find more information about it here; docs.opencv.org/modules/core/doc/basic_structures.html#mat-matDeformity
Hey, I know it is not about conventions. It is about reading an image which has been rotated using 'Windows->right-click->rotate right/left' command. I believe this rotation is only done for viewing purpose and it does not change the shape inside the image file. But, I want my opencv function to read rotated image (since other one is not properly oriented). How can I do that?Traipse
Why not just rotate the image in opencv after you read it in; opencvexamples.blogspot.com/2014/01/…Guerin
Hey, thanks for the suggestion. However, I can't do that since I am creating an application which reads a number of images from a folder to create a composite image (like a collage). Some images present in the folder have been rotated and some left as is (basically, all have been oriented well). But, this updated orientation information goes away when they are being read :(Traipse
Or... you could always rotate them in an application that you know will change the underlying bitmap. MS Paint or GIMP for instanceGuerin
I tried rotating in MS Paint. But, it didn't work. Gave me the same result.Traipse
Ohh I see what you are saying now, you should post your code. I just checked, and rotating images in Windows Photo Viewer does appear to rotate the actual image. So you shouldn't be having the issue you are seeing...Guerin
edited my post to have code and result. And yeah, even I thought so that rotating image in photo viewer rotates the underlying bitmap but, I guess it is not the case eventually.Traipse
I tested your code and it worked fine for me with an image rotated in MS photo viewer, or just via the right click "rotate left/right". My advice would be to verify simple things, like your file paths. Results: image shape: (480, 640, 3) image shape: (640, 480, 3)Guerin
L
2

Commonly Mat.rows represent the image's height,and the Mat.cols represent the image's width.

Lagoon answered 21/4, 2015 at 11:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.