cvl-robot's diary

研究ノート メモメモ https://github.com/dotchang/

Open3DのPythonチュートリアルをC++に移植(その6) intaractive_visualization.py

Open3Dは、2018年3月現在v0.1です。頻繁にアップデートがあると思われますので、この記事はすぐ風化する予定です。

interactive_visualizationは、マウス操作によるクロップ(領域きりだし)と、ポイント選択による初期位置指定をつかった位置合わせを提供します。これも、c++のTestコードが用意されていますので、ここのソースは見る必要がありません。

python
Open3D/interactive_visualization.py at master · IntelVCL/Open3D · GitHub

c++
Open3D/ManuallyCropPointCloud.cpp at master · IntelVCL/Open3D · GitHub
Open3D/src/Tools/ManuallyAlignPointCloud at master · IntelVCL/Open3D · GitHub

// ----------------------------------------------------------------------------
// -                        Open3D: www.open3d.org                            -
// ----------------------------------------------------------------------------
// The MIT License (MIT)
//
// Copyright (c) 2018 www.open3d.org
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
// ----------------------------------------------------------------------------

#include <iostream>
#include <memory>
#include <thread>

#include <Core/Core.h>
#include <IO/IO.h>
#include <Visualization/Visualization.h>

#include <assert.h>

using namespace three;

void VisualizeRegistration(const three::PointCloud &source,
		const three::PointCloud &target, const Eigen::Matrix4d &Transformation)
{
	std::shared_ptr<PointCloud> source_transformed_ptr(new PointCloud);
	std::shared_ptr<PointCloud> target_ptr(new PointCloud);
	*source_transformed_ptr = source;
	*target_ptr = target;
	source_transformed_ptr->Transform(Transformation);
	DrawGeometries({ source_transformed_ptr, target_ptr }, "Registration result");
}

void DemoCropGeometry()
{
	std::cout << "Demo for manual geometry cropping" << std::endl;
	std::cout << "1) Press 'Y' twice to align geometry with negative direction of y-axis" << std::endl;
	std::cout << "2) Press 'K' to lock screen and to switch to selection mode" << std::endl;
	std::cout << "3) Drag for rectangle selection," << std::endl;
	std::cout << "   or use ctrl + left click for polygon selection" << std::endl;
	std::cout << "4) Press 'C' to get a selected geometry and to save it" << std::endl;
	std::cout << "5) Press 'F' to switch to freeview mode" << std::endl;
        // std::shared_ptr<PointCloud>
	auto pcd = CreatePointCloudFromFile("../../lib/TestData/ICP/cloud_bin_0.pcd");
	DrawGeometriesWithEditing({pcd});
}

std::vector<size_t>& PickPoints(std::shared_ptr<const Geometry> geom_ptr)
{
	std::cout << std::endl;
	std::cout << "1) Please pick at least three correspondences using [shift + left click]" << std::endl;
	std::cout << "   Press [shift + right click] to undo point picking" << std::endl;
	std::cout << "2) After picking points, press q for close the window" << std::endl;
	VisualizerWithEditing vis;
	vis.CreateWindow();
	vis.AddGeometry(geom_ptr);
	vis.Run(); // user picks points
	vis.DestroyWindow();
	std::cout << std::endl;
	return vis.GetPickedPoints();
}

void DemoManualRegistration()
{
	std::cout << "Demo for manual ICP" << std::endl;
        // std::shared_ptr<PointCloud>
	auto source = CreatePointCloudFromFile("../../lib/TestData/ICP/cloud_bin_0.pcd");
        // std::shared_ptr<PointCloud>
	auto target = CreatePointCloudFromFile("../../lib/TestData/ICP/cloud_bin_2.pcd");
	std::cout << "Visualization of two point clouds before manual alignment" << std::endl;
	//DrawRegistrationResult(source, target, Eigen::Matrix4d::Identity());
	VisualizeRegistration(*source, *target, Eigen::Matrix4d::Identity());

	// pick points from two point clouds and builds correspondences
        // std::vector<size_t>&
	auto picked_id_source = PickPoints(source);
        // std::vector<size_t>&
	auto picked_id_target = PickPoints(target);
	assert((picked_id_source.size() >=3) && (picked_id_target.size() >= 3));
	assert(picked_id_source.size() == picked_id_target.size());
	CorrespondenceSet corr;
	for(size_t i = 0; i < picked_id_source.size(); i++){
		corr.push_back(Eigen::Vector2i(picked_id_source[i], picked_id_target[i]));
	}

	// estimate rough transformation using correspondences
	std::cout << "Compute a rough transformation using the correspondences given by user" << std::endl;
	TransformationEstimationPointToPoint p2p;
        // Eigen::Matrix4d
	auto trans_init = p2p.ComputeTransformation(*source, *target, corr);

	// point-to-point ICP for refinement
	std::cout << "Perform point-to-point ICP refinement" << std::endl;
	double threshold = 0.03; // 3cm distance threshold
        // RegistrationResult
	auto reg_p2p = RegistrationICP(*source, *target, threshold, trans_init,
				TransformationEstimationPointToPoint());

	VisualizeRegistration(*source, *target, reg_p2p.transformation_);
	std::cout << std::endl;
}

int main(int argc, char *argv[])
{
	SetVerbosityLevel(VerbosityLevel::VerboseAlways);

	DemoCropGeometry();
	DemoManualRegistration();

	return 1;
}

f:id:cvl-robot:20180306143728p:plainf:id:cvl-robot:20180306143743p:plain
Crop
f:id:cvl-robot:20180306143834p:plainf:id:cvl-robot:20180306143841p:plain
Selection
f:id:cvl-robot:20180306143848p:plain
f:id:cvl-robot:20180306143951p:plainf:id:cvl-robot:20180306144002p:plain
Manual Registration
f:id:cvl-robot:20180306144127p:plainf:id:cvl-robot:20180306144131p:plain
Manual Registration-Missed
f:id:cvl-robot:20180306144135p:plain
f:id:cvl-robot:20180306144224p:plainf:id:cvl-robot:20180306144228p:plain
Manual Registration-Succes?
f:id:cvl-robot:20180306144231p:plain

なかなかうまく合いません。Point-to-Point ICPが厳しいのだとは思いますが、ColoredICPかFPFHに任せてしまった方がよさそうですね。