【问题标题】:Fast Trapezoid to Rectangle for Video视频的快速梯形到矩形
【发布时间】:2014-02-26 23:00:51
【问题描述】:

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

对此:

我已经可以标记梯形区域的角,并使用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;
}

【问题讨论】:

  • 相机的位置和扭曲是恒定的,还是每帧都需要重新计算?
  • 它是恒定的。我曾想过做某种静态映射,但不知道该怎么做。我的 C++ 知识很基础。
  • 进行一次初始化,创建转换矩阵并将其存储在 cv::Mat 中,在主循环中使用它。如果您提供一些代码或指向它的链接,则更容易发现任何问题。
  • 计算只完成了一次。但是,我将构建一个小型工作示例。
  • 如果质量不重要,您可以在任何处理之前对高清图像进行采样。

标签: c++ opencv video-processing


【解决方案1】:

如果扭曲变换是恒定的,则有一种比在每一帧使用warpPerspective 更快的方法,使用函数remap (documentation link)。这个函数可以如下使用。

首先,在程序开始时,计算转换图,其中包含源图像中每个像素的(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要快得多。

【讨论】:

  • 这看起来很有希望!只有两个问题: 1. 如何填充 Mat_ corners_rectangle?我使用向量 直到知道。 2.我认为mapx应该是mapx_32f。
  • 很好,我更正了它,以及填写buff_mapxbuff_mapy 时的一个小错误。对于您的第一个问题,您可以像之前对 getPerspectiveTransform 调用所做的那样:vector&lt;Point&gt; 参数也应该起作用。
  • 稍微说明一下:map1_16u的类型实际上是CV_16SC2,只有map2_16u是CV_16UC1类型,所以变量名不是100%正确的;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-24
  • 1970-01-01
  • 1970-01-01
  • 2015-10-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多