C++:激光点云bev预处理代码实现

激光点云bev预处理C++代码实现

点云bev预处理

#ifndef PREPROCESS_H_
#define PREPROCESS_H_

#include "kernel.h"

class PreProcessCuda
{
private:
    Params params_;
    cudaStream_t stream_ = 0;
    unsigned int *mask_;

public:
    PreProcessCuda();
    ~PreProcessCuda();

    void generateBevProjection(float *points, size_t points_size, float *bevimage);
};

#endif
#include "preprocess.h"
#include <assert.h>
#include <iostream>
#include <math.h>

PreProcessCuda::PreProcessCuda()
{

    return;
}

PreProcessCuda::~PreProcessCuda()
{
    // checkCudaErrors(cudaFree(mask_));

    return;
}

void PreProcessCuda::generateBevProjection(float *points, size_t points_size, float *bevImage)
{
    float min_x_range = params_.min_x_range;
    float max_x_range = params_.max_x_range;
    float min_y_range = params_.min_y_range;
    float max_y_range = params_.max_y_range;
    float min_z_range = params_.min_z_range;
    float max_z_range = params_.max_z_range;

    float voxel_x_size = params_.voxel_x_size;
    float voxel_y_size = params_.voxel_y_size;
    
    int proj_x_size = params_.grid_x_size;
    int proj_y_size = params_.grid_y_size;

    float z_value_min = params_.z_value_min;
    float z_value_max = params_.z_value_max;
    int i_value_min = params_.i_value_min;
    int i_value_max = params_.i_value_max;
    int r_value_min = params_.r_value_min;
    int r_value_max = params_.r_value_max;

    int nnn = 0;

    for (size_t i = 0; i < points_size; ++i)
    {
        float4 point = ((float4*)points)[i];
        // float point_r = sqrt(point.x * point.x + point.y * point.y + point.z * point.z) * point.w;
        // std::cerr << i << ": "<< point.x << " " << point.y << " " << point.z << " " << point.w << " "<< point_r << std::endl;
        if (point.x > min_x_range && point.x < max_x_range &&
            point.y > min_y_range && point.y < max_y_range &&
            point.z > min_z_range && point.z < max_z_range)
        {
            float point_r = sqrt(point.x * point.x + point.y * point.y + point.z * point.z) * point.w;

            int voxel_idx = floorf((point.x - min_x_range) / voxel_x_size);
            int voxel_idy = floorf((point.y - min_y_range) / voxel_y_size);
            nnn +=1;
            // std::cerr << nnn << ": "<< point.z << " "<< point.w << " " << point_r << " " << voxel_idx << " " << voxel_idy << std::endl;

            // unsigned int voxel_index = voxel_idy * proj_x_size + voxel_idx;            
            unsigned int voxel_index = voxel_idx * proj_x_size + voxel_idy;            

            if (point.z < z_value_min) point.z = z_value_min;
            if (point.z > z_value_max) point.z = z_value_max;
            point.z = (point.z - z_value_min)/(z_value_max - z_value_min);

            if (point.w < i_value_min) point.w = i_value_min;
            if (point.w > i_value_max) point.w = i_value_max;
            point.w = (point.w - i_value_min)/(i_value_max - i_value_min);

            if (point_r < r_value_min) point_r = r_value_min;
            if (point_r > r_value_max) point_r = r_value_max;
            point_r = (point_r - r_value_min)/(r_value_max - r_value_min);
            // std::cerr << nnn << ": "<< point.z << " "<< point.w << " " << point_r << " " << voxel_idx << " " << voxel_idy << " " << voxel_index << std::endl;          

            bevImage[proj_x_size * proj_x_size * 0 + voxel_index] = point.z;
            bevImage[proj_x_size * proj_x_size * 1 + voxel_index] = point.w;
            bevImage[proj_x_size * proj_x_size * 2 + voxel_index] = point_r;
        }
        
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一份简单的Python代码实现,基于OpenCV库和Numpy库: ```python import cv2 import numpy as np # 定义相机内参矩阵 K = np.array([[1000, 0, 500], [0, 1000, 500], [0, 0, 1]]) # 定义相机外参矩阵 R0 = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) t0 = np.array([0, 0, 0]) R1 = np.array([[0.866, -0.5, 0], [0.5, 0.866, 0], [0, 0, 1]]) t1 = np.array([1, 0, 0]) R2 = np.array([[0.5, -0.866, 0], [0.866, 0.5, 0], [0, 0, 1]]) t2 = np.array([0.5, 0.866, 0]) R3 = np.array([[-0.5, -0.866, 0], [0.866, -0.5, 0], [0, 0, 1]]) t3 = np.array([-0.5, 0.866, 0]) R4 = np.array([[-1, 0, 0], [0, -1, 0], [0, 0, 1]]) t4 = np.array([0, -1, 0]) R5 = np.array([[-0.866, 0.5, 0], [-0.5, -0.866, 0], [0, 0, 1]]) t5 = np.array([0.5, -0.866, 0]) # 定义相机的投影矩阵 P0 = K @ np.hstack((R0, -R0 @ t0.reshape(3, 1))) P1 = K @ np.hstack((R1, -R1 @ t1.reshape(3, 1))) P2 = K @ np.hstack((R2, -R2 @ t2.reshape(3, 1))) P3 = K @ np.hstack((R3, -R3 @ t3.reshape(3, 1))) P4 = K @ np.hstack((R4, -R4 @ t4.reshape(3, 1))) P5 = K @ np.hstack((R5, -R5 @ t5.reshape(3, 1))) # 定义图像的尺寸 img_size = (1000, 1000) # 生成网格点坐标 x, y = np.meshgrid(np.arange(0, img_size[0]), np.arange(0, img_size[1])) # 将网格点坐标换成齐次坐标 pts = np.vstack((x.flatten(), y.flatten(), np.ones_like(x.flatten()))).T # 对每个相机进行投影 pts0 = cv2.undistortPoints(pts.reshape(-1, 1, 2), K, None).reshape(-1, 3) pts1 = cv2.undistortPoints(pts.reshape(-1, 1, 2), K, None).reshape(-1, 3) pts2 = cv2.undistortPoints(pts.reshape(-1, 1, 2), K, None).reshape(-1, 3) pts3 = cv2.undistortPoints(pts.reshape(-1, 1, 2), K, None).reshape(-1, 3) pts4 = cv2.undistortPoints(pts.reshape(-1, 1, 2), K, None).reshape(-1, 3) pts5 = cv2.undistortPoints(pts.reshape(-1, 1, 2), K, None).reshape(-1, 3) pts0_proj = cv2.projectPoints(pts0.reshape(-1, 1, 3), R0, t0, K, None)[0].reshape(-1, 2) pts1_proj = cv2.projectPoints(pts1.reshape(-1, 1, 3), R1, t1, K, None)[0].reshape(-1, 2) pts2_proj = cv2.projectPoints(pts2.reshape(-1, 1, 3), R2, t2, K, None)[0].reshape(-1, 2) pts3_proj = cv2.projectPoints(pts3.reshape(-1, 1, 3), R3, t3, K, None)[0].reshape(-1, 2) pts4_proj = cv2.projectPoints(pts4.reshape(-1, 1, 3), R4, t4, K, None)[0].reshape(-1, 2) pts5_proj = cv2.projectPoints(pts5.reshape(-1, 1, 3), R5, t5, K, None)[0].reshape(-1, 2) # 对每个相机的图像进行插值 img0 = cv2.remap(cv2.imread('img0.jpg'), pts0_proj[:, 0].reshape(img_size), pts0_proj[:, 1].reshape(img_size), cv2.INTER_LINEAR) img1 = cv2.remap(cv2.imread('img1.jpg'), pts1_proj[:, 0].reshape(img_size), pts1_proj[:, 1].reshape(img_size), cv2.INTER_LINEAR) img2 = cv2.remap(cv2.imread('img2.jpg'), pts2_proj[:, 0].reshape(img_size), pts2_proj[:, 1].reshape(img_size), cv2.INTER_LINEAR) img3 = cv2.remap(cv2.imread('img3.jpg'), pts3_proj[:, 0].reshape(img_size), pts3_proj[:, 1].reshape(img_size), cv2.INTER_LINEAR) img4 = cv2.remap(cv2.imread('img4.jpg'), pts4_proj[:, 0].reshape(img_size), pts4_proj[:, 1].reshape(img_size), cv2.INTER_LINEAR) img5 = cv2.remap(cv2.imread('img5.jpg'), pts5_proj[:, 0].reshape(img_size), pts5_proj[:, 1].reshape(img_size), cv2.INTER_LINEAR) # 将六个图像拼接成BEV BEV = np.vstack((np.hstack((img0, img1, img2)), np.hstack((img3, img4, img5)))) # 显示结果 cv2.imshow('BEV', BEV) cv2.waitKey(0) ``` 这里的代码中,使用了六个相机分别拍摄了环视图像,并对每个相机的图像进行了校准和投影,最后将它们拼接成了BEV。需要注意的是,上述代码仅为演示代码,实际应用需要对相机参数进行更精确的校准和投影。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ywfwyht

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值