当前位置:   article > 正文

两百行代码实现简易点云标注工具_自制点云标注工具

自制点云标注工具

夏天来了非常热,LZ周末不想出去玩,于是乎继之前的图片标注工具利用两个晚上写了一个简单的点云标注工具。该工具基于Qt5.14.2-msvc2017(其实LZ的VS版本是2019,似乎兼容)平台C++语言开发,用到的第三方库为PCL1.12.1+VTK9.1.0。之前了解到VTK9之前需要编译集成Qt的QVTKWidget插件,而之后改为将QWidget提升为QVTKOpenGLNativeWidget(同样需要自己编译VTK,编译和配置方法可参考:VTK笔记-Qt5.12.11编译VTK9.0.3-QVTKOpenGLNativeWidgetQT5+VTK9.1最新配置方法)。
实现ui界面:其实就是拖拽各种控件啦~ LZ特别喜欢弄这个,感觉挺有成就感的。包括信号和槽也可以用designer可视化编辑实现,就懒得写代码了。在这里插入图片描述

受于篇幅限制,只贴出部分核心实现代码,一共也就两百多行:
mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H


#include "label_settings.h"
#include "scale_settings.h"
#include "help_settings.h"

#include <QMainWindow>
#include <QFileDialog>
#include <QMessageBox>

#include <iostream>

#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/features/moment_of_inertia_estimation.h>
#include <vtkGenericOpenGLRenderWindow.h>
#include <vtkOutputWindow.h>


QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE


class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);

    ~MainWindow();

private slots:
    void on_action_open_cloud_triggered();

    void on_action_close_cloud_triggered();

    void on_action_save_label_triggered();

    void on_action_delete_label_triggered();

    void update_viewer();

    void on_action_add_box_triggered();

    void on_action_x_bigger_triggered();

    void on_action_x_smaller_triggered();

    void on_action_y_bigger_triggered();

    void on_action_y_smaller_triggered();

    void on_action_z_bigger_triggered();

    void on_action_z_smaller_triggered();

    void on_action_l_bigger_triggered();

    void on_action_l_smaller_triggered();

    void on_action_w_bigger_triggered();

    void on_action_w_smaller_triggered();

    void on_action_h_bigger_triggered();

    void on_action_h_smaller_triggered();

    void on_action_set_scale_triggered();

    void on_action_show_help_triggered();

private:
    Ui::MainWindow *ui;

    Label_Settings *label_settings;

    Scale_Settings *scale_settings;

    Help_Settings *help_settings;

    QString cloud_path, label_path;

    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud;

    pcl::visualization::PCLVisualizer::Ptr viewer;

    struct BoundingBox
    {
        Eigen::Vector3f position;
        Eigen::Quaternionf quat;
        float l;
        float w;
        float h;
        int label;
    } box;

    std::vector<BoundingBox> boxes;

    static int box_id;
};


#endif // MAINWINDOW_H
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "helpers.h"


int MainWindow::box_id = 0;


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    label_settings = new Label_Settings;
    scale_settings = new Scale_Settings;
    help_settings = new Help_Settings;

    cloud.reset(new pcl::PointCloud<pcl::PointXYZ>);

    auto renderer = vtkSmartPointer<vtkRenderer>::New();
    auto renderWindow = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();
    viewer.reset(new pcl::visualization::PCLVisualizer(renderer, renderWindow, "viewer", false));

    vtkOutputWindow::SetGlobalWarningDisplay(0);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_action_open_cloud_triggered()
{
    cloud_path = QFileDialog::getOpenFileName(this, QString("打开.pcd文件"), "", "*.pcd");
    if(cloud_path.isEmpty())
    {
        warningbox(QString("请选择有效的点云路径!"));
        return;
    }

    pcl::io::loadPCDFile(cloud_path.toStdString(), *cloud);
    boxes.clear();

    viewer->removeAllPointClouds();
    viewer->removeAllShapes();
    viewer->addPointCloud(cloud, "cloud");
    viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "cloud");
    viewer->setupInteractor(ui->qvtkWidget->interactor(), ui->qvtkWidget->renderWindow());
    ui->qvtkWidget->setRenderWindow(viewer->getRenderWindow());
    ui->qvtkWidget->renderWindow()->Render();
    ui->statusBar->showMessage(cloud_path);
}

void MainWindow::on_action_close_cloud_triggered()
{
    cloud->clear();
    boxes.clear();
    viewer->removeAllPointClouds();
    viewer->removeAllShapes();
    ui->qvtkWidget->renderWindow()->Render();
    ui->statusBar->clearMessage();
}

void MainWindow::on_action_save_label_triggered()
{
    if(cloud_path.isEmpty())  return;

    QString cloud_path_temp = cloud_path;
    label_path = cloud_path_temp.replace(".pcd", ".txt");

    std::fstream txt(label_path.toStdString(), 'w');
    for(auto box : boxes)
    {
        txt << box.label << " "
            << box.position.x() << " " << box.position.y() << " " << box.position.z() << " "
            << box.l << " " << box.w << " " << box.h << std::endl;
    }
    txt.close();
}

void MainWindow::on_action_delete_label_triggered()
{
    boxes.pop_back();
    viewer->removeShape(std::to_string(box_id));
    ui->qvtkWidget->renderWindow()->Render();
}

void MainWindow::update_viewer()
{
    viewer->removeShape(std::to_string(box_id));
    viewer->addCube(box.position, box.quat, box.l, box.w, box.h, std::to_string(box_id));
    viewer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 1, 0, 0, std::to_string(box_id));
    viewer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_OPACITY, 0.1, std::to_string(box_id));
    viewer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_LINE_WIDTH, 1, std::to_string(box_id));
    ui->qvtkWidget->renderWindow()->Render();
}

void MainWindow::on_action_add_box_triggered()
{
    if(cloud->size() == 0)  return;

    label_settings->exec();

    pcl::PointXYZ min_point_AABB, max_point_AABB;
    pcl::MomentOfInertiaEstimation<pcl::PointXYZ> feature_extractor;
    feature_extractor.setInputCloud(cloud);
    feature_extractor.compute();
    feature_extractor.getAABB(min_point_AABB, max_point_AABB);

    box.position.x() = (min_point_AABB.x + max_point_AABB.x) / 2;
    box.position.y() = (min_point_AABB.y + max_point_AABB.y) / 2;
    box.position.z() = (min_point_AABB.z + max_point_AABB.z) / 2;
    box.quat = Eigen::Quaternionf(1, 0, 0, 0);
    box.l = max_point_AABB.x - min_point_AABB.x;
    box.w = max_point_AABB.y - min_point_AABB.y;
    box.h = max_point_AABB.z - min_point_AABB.z;
    box.label = Label_Settings::label;

    boxes.push_back(box);
    box_id++;

    update_viewer();
    viewer->addCoordinateSystem(std::max(box.l, std::max(box.w, box.h)) / 10);
}

void MainWindow::on_action_x_bigger_triggered()
{
    if(cloud->size() == 0)  return;

    box.position.x() += Scale_Settings::xyz_scale * box.l;
    boxes.pop_back();
    boxes.push_back(box);
    update_viewer();
}

void MainWindow::on_action_x_smaller_triggered()
{
    if(cloud->size() == 0)  return;

    box.position.x() -= Scale_Settings::xyz_scale * box.l;
    boxes.pop_back();
    boxes.push_back(box);
    update_viewer();
}

void MainWindow::on_action_y_bigger_triggered()
{
    if(cloud->size() == 0)  return;

    box.position.y() += Scale_Settings::xyz_scale * box.w;
    boxes.pop_back();
    boxes.push_back(box);
    update_viewer();
}

void MainWindow::on_action_y_smaller_triggered()
{
    if(cloud->size() == 0)  return;

    box.position.y() -= Scale_Settings::xyz_scale * box.w;
    boxes.pop_back();
    boxes.push_back(box);
    update_viewer();
}

void MainWindow::on_action_z_bigger_triggered()
{
    if(cloud->size() == 0)  return;

    box.position.z() += Scale_Settings::xyz_scale * box.h;
    boxes.pop_back();
    boxes.push_back(box);
    update_viewer();
}

void MainWindow::on_action_z_smaller_triggered()
{
    if(cloud->size() == 0)  return;

    box.position.z() -= Scale_Settings::xyz_scale * box.h;
    boxes.pop_back();
    boxes.push_back(box);
    update_viewer();
}

void MainWindow::on_action_l_bigger_triggered()
{
    if(cloud->size() == 0)  return;

    box.l += Scale_Settings::lwh_scale * box.l;
    boxes.pop_back();
    boxes.push_back(box);
    update_viewer();
}

void MainWindow::on_action_l_smaller_triggered()
{
    if(cloud->size() == 0)  return;

    box.l -= Scale_Settings::lwh_scale * box.l;
    boxes.pop_back();
    boxes.push_back(box);
    update_viewer();
}

void MainWindow::on_action_w_bigger_triggered()
{
    if(cloud->size() == 0)  return;

    box.w += Scale_Settings::lwh_scale * box.w;
    boxes.pop_back();
    boxes.push_back(box);
    update_viewer();
}

void MainWindow::on_action_w_smaller_triggered()
{
    if(cloud->size() == 0)  return;

    box.w -= Scale_Settings::lwh_scale * box.w;
    boxes.pop_back();
    boxes.push_back(box);
    update_viewer();
}

void MainWindow::on_action_h_bigger_triggered()
{
    if(cloud->size() == 0)  return;

    box.h += Scale_Settings::lwh_scale * box.h;
    boxes.pop_back();
    boxes.push_back(box);
    update_viewer();
}

void MainWindow::on_action_h_smaller_triggered()
{
    if(cloud->size() == 0)  return;

    box.h -= Scale_Settings::lwh_scale * box.h;
    boxes.pop_back();
    boxes.push_back(box);
    update_viewer();
}

void MainWindow::on_action_set_scale_triggered()
{
    scale_settings->exec();
}

void MainWindow::on_action_show_help_triggered()
{
    help_settings->exec();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255

代码很容易看懂。需要解释的地方:boxes是用于储存所有BoundingBox的容器,box_id是用于定义每个box的唯一id。类的构造函数中的

vtkOutputWindow::SetGlobalWarningDisplay(0);
  • 1

用来消除VTK的warning窗口。
另外,旧版本Qt+VTK刷新窗口方式为

ui->qvtkWidget->update();
  • 1

新版本对应语句为

ui->qvtkWidget->renderWindow()->Render();
  • 1

本工具实现了打开点云、关闭点云,新建点云3d boundingbox(初始化为点云的AABB包围盒)并调整包围盒的位置、大小,以及保存标注、删除标注的功能。不过代码量很小,就别指望有什么高级功能了hh~ LZ感觉用是能用,就是调整包围盒太位置和大小的时候麻烦了,可能是没有实现鼠标拖动相应的功能吧。其他功能待感兴趣的小伙伴发掘和完善~
最后贴上一张界面效果图和保存的标注(格式:每行为box的cls x y z w h l)
在这里插入图片描述
在这里插入图片描述
工程下载链接:https://download.csdn.net/download/taifyang/87998855

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号