Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image height and width getting swapped when read using opencv imread

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!

like image 354
sm08 Avatar asked Sep 28 '22 05:09

sm08


2 Answers

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)

like image 88
CodyF Avatar answered Oct 08 '22 08:10

CodyF


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

like image 26
an unique monkey Avatar answered Oct 08 '22 07:10

an unique monkey