逆透视变换加速方法----鸟瞰图 opencv

cv::warpPerspective,一次透视变换的时间,需要耗费100多ms。

解决办法: 建立查找表,之后,每张图片调用remap函数。
参考链接

#include <iostream>
#include<stdio.h>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

void perspective_to_maps(const cv::Mat &perspective_mat, const cv::Size img_size,
    cv::Mat &map1, cv::Mat &map2)
{
    
    
    // invert the matrix because the transformation maps must be
    // bird's view -> original
    cv::Mat inv_perspective(perspective_mat.inv());
    inv_perspective.convertTo(inv_perspective, CV_32FC1);

    cv::Mat xy(img_size, CV_32FC2);
    float *pxy = (float*)xy.data;
    for (int y = 0; y < img_size.height; y++)
        for (int x = 0; x < img_size.width; x++)
        {
    
    
            *pxy++ = x;
            *pxy++ = y;
        }
    // perspective transformation of the points
    cv::Mat xy_transformed;
    cv::perspectiveTransform(xy, xy_transformed, inv_perspective);
    //Prevent errors when float32 to int16
       // split x/y to extra maps
    assert(xy_transformed.channels() == 2);
    cv::Mat maps[2]; // map_x, map_y
    cv::split(xy_transformed, maps);
    // remap() with integer maps is faster
    cv::convertMaps(maps[0], maps[1], map1, map2, CV_32F);
}
int main()
{
    
    
    cv::Mat image;
    image=cv::imread("/home/zhy/Documents/Perception/camera_data/lane_image/689.JPG");
    std::vector<cv::Point2f> src_pts{
    
    cv::Point2f(200,1200 - 1), cv::Point2f(800, 600), cv::Point2f(1000, 600), cv::Point2f(1400, 1200 - 1)};
    std::vector<cv::Point2f> dst_pts{
    
    cv::Point2f(960,1200 - 1), cv::Point2f(800, 300), cv::Point2f(960, 300), cv::Point2f(1100, 1200 - 1)};
    cv::Mat perspectiveMatrix = cv::getPerspectiveTransform(src_pts, dst_pts);
    cv::Mat inversePerspectiveMatrix = cv::getPerspectiveTransform(dst_pts, src_pts);
    cv::Size image_size=cv::Size(1920,1200);
    cv::Mat mapx;
    cv::Mat mapy;
   perspective_to_maps(perspectiveMatrix, image_size, mapx, mapy);
   cv:Mat outimage;
   remap(image, outimage, mapx, mapy,INTER_NEAREST);
   cv::imshow("edge",outimage);
   cv::waitKey(0);
   std::string fileName = "rmp_x.txt" ;
   int n=0;
   std::ofstream outfile( fileName.c_str() ) ; // file name and the operation type.
   for(int i=0; i<mapy.rows; i++){
    
    
       for(int j=0; j<mapy.cols; j++){
    
    

           if (n%4!=0)
              outfile<<mapx.at<float>(i,j)<<",";
           else
              outfile<<'\n' <<"        "<<mapx.at<float>(i,j)<<"," ;
            n=n+1;
             }

       outfile << std::endl ;    // a   newline after storing all the values of a line of the img
        }
       outfile.close();
   return 0;
}

猜你喜欢

转载自blog.csdn.net/zhngyue123/article/details/105920770
今日推荐