Get normal Mat from transformed Mat

时间:2016-08-31 18:34:03

标签: c++ matlab opencv matrix computer-vision

I am a programmer working for a personal project, but I don't have that much experience with math. I took a Mat(matrix of pixels), and changed it's perspective so it looks better for me when I am processing the image. Now the problem is that I want to extract some lines (points from matrix) but my matrix is transformed and I have no clue how to reverse it. Maybe you can help me. Here are the functions that I used :

void FrameAnalyser::applyPerspective(Mat input, Mat output) {
int alpha_ = 9.0, beta_ = 90., gamma_ = 90.;
int f_ = 500, dist_ = 255;

double f, dist;
double alpha, beta, gamma;
alpha = ((double)alpha_ - 90.)*PI / 180;
beta = ((double)beta_ - 90.)*PI / 180;
gamma = ((double)gamma_ - 90.)*PI / 180;
f = (double)f_;
dist = (double)dist_;

Size taille = input.size();
double w = (double)taille.width, h = (double)taille.height;

// Projection 2D -> 3D matrix
Mat A1 = (Mat_<double>(4, 3) <<
    1, 0, -w / 2,
    0, 1, -h / 2,
    0, 0, 0,
    0, 0, 1);

// Rotation matrices around the X,Y,Z axis
Mat RX = (Mat_<double>(4, 4) <<
    1, 0, 0, 0,
    0, cos(alpha), -sin(alpha), 0,
    0, sin(alpha), cos(alpha), 0,
    0, 0, 0, 1);

Mat RY = (Mat_<double>(4, 4) <<
    cos(beta), 0, -sin(beta), 0,
    0, 1, 0, 0,
    sin(beta), 0, cos(beta), 0,
    0, 0, 0, 1);

Mat RZ = (Mat_<double>(4, 4) <<
    cos(gamma), -sin(gamma), 0, 0,
    sin(gamma), cos(gamma), 0, 0,
    0, 0, 1, 0,
    0, 0, 0, 1);

// Composed rotation matrix with (RX,RY,RZ)
Mat R = RX * RY * RZ;

// Translation matrix on the Z axis change dist will change the height
Mat T = (Mat_<double>(4, 4) <<
    1, 0, 0, 0,
    0, 1, 0, 0,
    0, 0, 1, dist,
    0, 0, 0, 1);

// Camera Intrisecs matrix 3D -> 2D
Mat A2 = (Mat_<double>(3, 4) <<
    f, 0, w / 2, 0,
    0, f, h / 2, 0,
    0, 0, 1, 0);

// Final and overall transformation matrix
Mat transfo = A2 * (T * (R * A1));

// Apply matrix transformation
warpPerspective(input, output, transfo, taille, INTER_CUBIC | WARP_INVERSE_MAP);
}

0 个答案:

没有答案