Ask Your Question
0

image height and width swapping

asked 2015-04-20 23:24:34 -0500

sonal08 gravatar image

updated 2015-04-21 23:05:12 -0500

As I read my image using opencv imread function, I see that its height and width are being swapped. As a result, I see rotated image. This does not change even if I rotate image prior to it being read. What can be the workaround for this problem?

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)'

first_image rotated image

first image result second image result

output

I think the question should make sense now. Let me know if still further information is needed. Thanks for your help!

edit retag flag offensive close merge delete

Comments

2

i just don't believe you.

please add image and code as a proof.

berak gravatar imageberak ( 2015-04-21 01:16:54 -0500 )edit

please see the edit.

sonal08 gravatar imagesonal08 ( 2015-04-21 23:14:04 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-04-21 01:13:13 -0500

matman gravatar image

I think transpose is that what you need.

#include <opencv2/opencv.hpp>

using namespace cv;

Mat image = imread("your/path/to/image.png", IMREAD_ANY);
// here comes the transpose function
Mat imageSwapewd = image.t();

When the direction is wrong you can use cv::flip instead

edit flag offensive delete link more

Comments

1

Actually the solution is correct, but his question is wrong ... I have been using the imread function exhaustively and never met this behaviour... so let him first supply the image and code.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-04-21 02:35:35 -0500 )edit

I have edited my post to provide code and example scenario. I think when an image has been rotated using 'Windows->right-click->rotate right/left' command, it 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. How can I do that?

sonal08 gravatar imagesonal08 ( 2015-04-21 23:13:40 -0500 )edit

By manually rotating the image like suggested above!

StevenPuttemans gravatar imageStevenPuttemans ( 2015-04-22 03:50:34 -0500 )edit

But I don't know which of the ones need to be rotated. So, I am making an application which reads images from a folder and some of them are oriented properly while some have been rotated (using windows right-click->rotate right/left command) to orient it. So, in opencv I just read all the images in the folder. Is there a way where I can find incorrect orientation using opencv? But, I think it would be too much of a task since then, I would need to analyse image and find if the characters in the image have proper orientation or not.

sonal08 gravatar imagesonal08 ( 2015-04-22 06:33:37 -0500 )edit

There is a difference what you expect to be correct orientation and the one that is assigned to the file by the camera inside its properties. There is no way of checking that except for visualy checking it when loading it in OpenCV and let the user press a button to rotate if needed.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-04-22 07:55:50 -0500 )edit

ohk yeah.. i get your point and that is why I wish to rotate the image manually before it can be read by opencv.. so is there a way I can rotate it using windows functionality which actually changes its original orientation and get recognized by open cv (besides using the code mentioned above though, I did not try the code yet)?

sonal08 gravatar imagesonal08 ( 2015-04-22 17:01:32 -0500 )edit

Only way to do this is opening the image in actual image processing software like paint.NET, gimp or photoshop. Rotate the image and resave it. There is software that allows you to do batch like operations on large image collections. You should look into that!

StevenPuttemans gravatar imageStevenPuttemans ( 2015-04-23 01:57:19 -0500 )edit

thanks for the suggestion. I will try these tools in future. For now, I had written a code (which uses numpy->swapaxes function to swap between rows and columns).

sonal08 gravatar imagesonal08 ( 2015-04-24 19:44:55 -0500 )edit

... but if you do this in code, then you can do it with OpenCV also ... what is the point then? Simply load the image in and check the ratio between cols and rows in openCV. If the one is larger than the latter, you will know to rotate or not... if it should be decided based on the content, then you cannot do that in software ...

StevenPuttemans gravatar imageStevenPuttemans ( 2015-04-25 03:19:46 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2015-04-20 23:24:34 -0500

Seen: 3,392 times

Last updated: Apr 21 '15