Wie berechne Perspektive-Transformation für OpenCV von Winkel?

Möchte ich berechnen Perspektive-Transformation (matrix warpPerspective-Funktion) ab Drehwinkel und Abstand zum Objekt.

Wie zu tun?

Fand ich den code irgendwo auf OE. Beispiel Programm ist unter:

#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#include <iostream>
#include <math.h>

using namespace std;
using namespace cv;

Mat frame;

int alpha_int;
int dist_int;
int f_int;

double w;
double h; 
double alpha; 
double dist; 
double f;

void redraw() {

    alpha = (double)alpha_int/1000.;
    //dist = 1./(dist_int+1);
    //dist = dist_int+1;
    dist = dist_int-50;
    f = f_int+1;

    cout << "alpha = " << alpha << endl;
    cout << "dist = " << dist << endl;
    cout << "f = " << f << endl;

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

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

    //Translation matrix on the Z axis 
    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);

    Mat m = A2 * (T * (R * A1));

    cout << "R=" << endl << R << endl;
    cout << "A1=" << endl << A1 << endl;
    cout << "R*A1=" << endl << (R*A1) << endl;
    cout << "T=" << endl << T << endl;
    cout << "T * (R * A1)=" << endl << (T * (R * A1)) << endl;
    cout << "A2=" << endl << A2 << endl;
    cout << "A2 * (T * (R * A1))=" << endl << (A2 * (T * (R * A1))) << endl;
    cout << "m=" << endl << m << endl;

    Mat frame1;


    warpPerspective( frame, frame1, m, frame.size(), INTER_CUBIC | WARP_INVERSE_MAP);

    imshow("Frame", frame);
    imshow("Frame1", frame1);
}

void callback(int, void* ) {
    redraw();
}

void main() {


    frame = imread("FruitSample_small.png", CV_LOAD_IMAGE_COLOR);
    imshow("Frame", frame);

    w = frame.size().width;
    h = frame.size().height; 

    createTrackbar("alpha", "Frame", &alpha_int, 100, &callback);
    dist_int = 50;
    createTrackbar("dist", "Frame", &dist_int, 100, &callback);
    createTrackbar("f", "Frame", &f_int, 100, &callback);

    redraw();

    waitKey(-1);
}

Aber leider ist diese Transformation wird etwas seltsam

Wie berechne Perspektive-Transformation für OpenCV von Winkel?

Warum? Was ist die andere Hälfte des Bildes oben, wenn alpha>0? Und wie dreht man die um andere Achsen? Warum dist Werke so seltsam?

es sieht aus wie Ihre Werte für Breite und Höhe sind einzelne Pixel.
Einstellung Hunderte nichts ändern
Zu wissen, warum dieser code funktioniert, wie es funktioniert, werfen Sie einen Blick auf die Gleichungen in der folgenden Papier eee.nuigalway.ie/Research/Auto/Dokumente/docualain_issc10.pdf . Obwohl ich immer noch das Gefühl, dieser code ist etwas komisch. Ich würde alpha = (double)alpha_int/1000.; zu alpha = (double)alpha_int*CV_PI/180.; und auch ich nicht wissen, wie A1-matrix berechnet wird. Ich würde A1 als Mat A1 = (Mat_<double>(4,3) << 1, 0, -w/2, 0, 0, 0, //Y-axis zero 0, 1, -h/2, 0, 0, 1);
Und Sie haben selbstverständlich die rotation um x-Achse [d.h. die Tonhöhe oder Neigung] (werfen Sie einen Blick auf das Diagramm in der pdf-siehe oben). Wenn Ihre Kamera gedreht, mit der roll 'gamma' haben Sie zu vermehren, indem Sie andere 4x4-matrix mit gamma-parameter.
Ich würde entfernen dist_int track bar und setzen dist = Höhe/sin(alpha) (stellen Sie sicher, alpha - >0). Höhe ist die Höhe, wo die Kamera platziert wird, aus dem Boden.

InformationsquelleAutor Suzan Cioc | 2013-06-13

Schreibe einen Kommentar