去畸变undistort的 cuda加速版本

undistort.h 做下声明

#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <iostream>

extern "C" void undistortcuda(float * src, float *dst,float *camera_matrix,float *dist,int width,int height);

undistort.cu

#include "cuda_runtime.h"
#include "cuda.h"

#include <iostream>
#include <string>

using namespace std; 


extern "C" void undistortcuda(float * src, float *dst,float *camera_matrix,float *dist,int width,int height);




__global__ void undistort_(float * src, float *dst, float *camera_matrix, float *distCoeffs, int width, int height)

{

    int xIndex = threadIdx.x + blockDim.x * blockIdx.x;

    int yIndex = threadIdx.y + blockDim.y * blockIdx.y;

    int idx = yIndex * width + xIndex;


    float k1 = distCoeffs[0];

    float k2 = distCoeffs[1];

    float p1 = distCoeffs[2];

    float p2 = distCoeffs[3];

    float k3 = distCoeffs[4];


    float fx = camera_matrix[0];

    float fy = camera_matrix[4];

    float cx = camera_matrix[2];

    float cy = camera_matrix[5];



    float x0, y0, x, y;

    float x_undist = 0; float y_undist = 0;


     x0 = x = (xIndex - cx) / fx;

     y0 = y = (yIndex - cy) / fy;


    for (int iter = 0; iter < 5; ++iter)

    {

        float r2 = x * x + y * y;

        float  icdist = 1. / (1 + (k3*r2 + k2)*r2 + k1)*r2;

        float deltaX = 2. * p1 *x*y + p2 *(r2 + 2 * x*x);

        float deltaY = p1*(r2 + 2 * y*y) + 2.*p2*x*y;

        x = (x0 - deltaX) * icdist;

        y = (y0 - deltaY) * icdist;


    }

    x_undist = x * fx + cx;

    y_undist = y * fy + cy;

    // 最近邻

    if (x_undist >= 0 && y_undist >= 0 && x_undist < width && y_undist < height)

    {

        dst[idx] = src[(int)y_undist * width + (int)x_undist];

    }

    else

        dst[idx] = 0;


}







void  undistortcuda(float * src, float *dst,float *camera_matrix,float *dist,int width,int height)
{
dim3 tpb(32,32);
dim3 blocksPerGrid(( width+tpb-1) / tpb.x , (height + tpb.y -1) / tpb.y);

undistort_<<< blocksPerGrid,tpb >>> (src,dst,camer_matrix,dist,width,height);


}

结果:很快吧1080ti 上1ms,tx2上3ms

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值