用于视频的快速梯形到矩形

时间:2014-02-26 10:04:38

标签: c++ opencv video-processing

我想将梯形区域变换为矩形。 示例图片(不是完美的梯形,但你明白了):

enter image description here 对此: enter image description here

我已经可以标记梯形区域的角落,并使用getPerspectiveTransform计算warpPerspective的正确矩阵来转换图像。

不幸的是,这种转变非常缓慢。在720p网络摄像头流的约80%面积上使用它会导致下降到~5fps。我怀疑这可能是因为warpPerspective允许比我需要更多的转换。

有没有更快的方法将图像从梯形变换为矩形? (最好使用OpenCV)

更多信息:

  • warpAffine可用于进行仿射变换。它比warpPerspective快,但是(如果我说得对)你只能改变平行四边形的角度。
  • 有点相关但没有回答:Trapezoid to Rectangle

基于AldurDisciple使用我帖子中第一张图片的答案的最低工作示例(不完全工作)。

#include <opencv2/core/core.hpp>  
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/opencv.hpp"
using namespace cv;
int main() {

    //Mat img = imread("EQ9in.png");
    Mat img = imread("C:\\ss819729\\Aufnahmen\\arbeiten\\EQ9in.png");
    int height = img.rows;
    int width = img.cols;

    vector<Point2f> corners_rectangle, corners_trapezoid;

    corners_rectangle.push_back(Point2f(0, 0));
    corners_rectangle.push_back(Point2f(img.cols, 0));
    corners_rectangle.push_back(Point2f(img.cols, img.rows));
    corners_rectangle.push_back(Point2f(0, img.rows));

    corners_trapezoid.push_back(Point2f(35, 6));
    corners_trapezoid.push_back(Point2f(419, 55));
    corners_trapezoid.push_back(Point2f(404, 44));
    corners_trapezoid.push_back(Point2f(10, 477));

    Mat_<float> H_rectangle_to_trapezoid = cv::getPerspectiveTransform(corners_rectangle, corners_trapezoid);

    cv::Mat_<float> mapx_32f(height, width), mapy_32f(height, width);
    for(int y = 0; y<height; ++y) {
        float *buff_mapx = ((float*) mapx_32f.data)+y*width;
        float *buff_mapy = ((float*) mapy_32f.data)+y*width;
        for(int x = 0; x<width; ++x) {
            cv::Mat_<float> pt(3, 1);
            pt(0) = x;
            pt(1) = y;
            pt(2) = 1;
            pt = H_rectangle_to_trapezoid*pt;
            pt /= pt(2);
            buff_mapx[x] = pt(0);
            buff_mapy[x] = pt(1);
        }
    }
    cv::Mat map1_16u, map2_16u;
    cv::convertMaps(mapx_32f, mapy_32f, map1_16u, map2_16u, CV_16SC2);

    cv::Mat img_rectified;
    cv::remap(img, img_rectified, map1_16u, map2_16u, cv::INTER_LINEAR);

    namedWindow("Rectangle Image", CV_WINDOW_AUTOSIZE);
    while(waitKey(1)!='q') {
        cv::remap(img, img_rectified, map1_16u, map2_16u, cv::INTER_LINEAR);
        imshow("Rectangle Image", img_rectified);
    }
    return 1;
}

1 个答案:

答案 0 :(得分:3)

如果变形变换是常数,则使用函数warpPerspectivedocumentation link)比在每个帧使用remap快得多。此功能可以如下使用。

首先,在程序开始时,计算转换图,包含源图像中(x,y)坐标,用于源图像的每个像素:

cv::Mat_<float> corners_rectangle, corners_trapezoid;
// TODO: fill corners_rectangle and corners_trapezoid
cv::Mat_<float> H_rectangle_to_trapezoid = cv::getPerspectiveTransform(corners_rectangle, corners_trapezoid);
cv::Mat_<float> mapx_32f(height,width), mapy_32f(height,width);
for(int y=0; y<height; ++y)
{
    float *buff_mapx=((float*)mapx_32f.data)+y*width;
    float *buff_mapy=((float*)mapy_32f.data)+y*width;
    for(int x=0; x<width; ++x)
    {
        cv::Mat_<float> pt(3,1);
        pt(0) = x;
        pt(1) = y;
        pt(2) = 1;
        pt = H_rectangle_to_trapezoid*pt;
        pt /= pt(2);
        buff_mapx[x] = pt(0);
        buff_mapy[x] = pt(1);
    }
}
cv::Mat map1_16u,map2_16u;
cv::convertMaps(mapx_32f,mapy_32f,map1_16u,map2_16u,CV_16SC2);
// Keep map1_16u & map2_16u, discard the rest

然后在每一帧,您只需要使用remap函数进行插值:

cv::Mat img_rectified;
cv::remap(img_src, img_rectified, map1_16u, map2_16u, cv::INTER_LINEAR);

由于坐标转换的计算是离线完成的,因此比重复使用warpPerspective要快得多。

相关问题