Prospective warping in OpenCV based on night camera orientation

I am working on a project that is trying to remove perspective distortion from an image based on a known camera orientation. I think that I can create a rotational matrix based on the known X, Y and Z camera orientations. Then I can apply these matrices to the image using the WarpPerspective method.

In my script (written in Python) I created three rotational matrices, each of which is based on an orientation angle. I got to the point that I'm stuck on two issues. First, when I load each individual matrix into the WarpPerspective method, it does not work correctly. Whenever I deform the image on one axis, it seems to overload the image significantly. Image content is only recognized if I limit the orientation angle to about 1 degree or less.

Secondly, how to combine three rotational matrices into one matrix, which will be loaded into the WarpPerspective method. Can I import a 3x3 rotation matrix into this method, or do I need to create a 4x4 projective matrix. Below is the code I'm working on.

Thank you for your help.

CR

from numpy import *
import cv

#Sets angle of camera and converts to radians
x =  -14 * (pi/180)
y = 20 * (pi/180)
z =  15 * (pi/180)

#Creates the Rotational Matrices
rX = array([[1, 0, 0], [0, cos(x), -sin(x)], [0, sin(x), cos(x)]])
rY = array([[cos(y), 0, -sin(y)], [0, 1, 0], [sin(y), 0, cos(y)]])
rZ = array([[cos(z), sin(z), 0], [-sin(z), cos(z), 0], [0, 0, 1]])

#Converts to CVMat format
X = cv.fromarray(rX)
Y = cv.fromarray(rY)
Z = cv.fromarray(rZ)

#Imports image file and creates destination filespace
im = cv.LoadImage("reference_image.jpg")
dst = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_8U, 3)

#Warps Image
cv.WarpPerspective(im, dst, X)

#Display
cv.NamedWindow("distorted")
cv.ShowImage("distorted", im)
cv.NamedWindow("corrected")
cv.ShowImage("corrected", dst)
cv.WaitKey(0)
cv.DestroyWindow("distorted")
cv.DestroyWindow("corrected")
+5
3

, , ( , ).

, . , (.. ).

, , ( /). , , () , .

+3

. -, x y . . , , , , . , (, ) . , , . , (++ openCV)

  • z

//1
float x =  -14 * (M_PI/180);
float y =  20 * (M_PI/180);
float z =  15 * (M_PI/180);

cv::Matx31f rot_vec(x,y,z);
cv::Matx33f rot_mat;
cv::Rodrigues(rot_vec, rot_mat); //converts to a rotation matrix

cv::Matx33f translation1(1,0,-image.cols/2,
                        0,1,-image.rows/2,
                        0,0,1);
rot_mat(0,2) = 0;
rot_mat(1,2) = 0;
rot_mat(2,2) = 1;

//2 and 3
cv::Matx33f trans = rot_mat*translation1;
//4
trans(2,2) += image.rows;
cv::Matx33f camera_mat(image.rows,0,image.rows/2,
                       0,image.rows,image.rows/2,
                       0,0,1);
//5
cv::Matx33f transform = camera_mat*trans;
//6
cv::Mat final;
cv::warpPerspective(image, final, cv::Mat(transform),image.size());

enter image description here

, . , FindHomography . , , .

+2

The Wikipedia page on rotation matrices shows how you can combine the three main rotation matrices into one.

0
source

All Articles