herrDeng網內搜尋

自訂搜尋

Ads

2021年12月7日 星期二

柴犬3D ply圖檔點雲擷取realsense C++ opencv程式

 
 這類程式不難,要訣就是要「對齊」『深度攝影處理C++ realsense2加上影格對齊align功能』https://youtu.be/GP88KJzUxgY 

柴犬3D ply圖檔點雲擷取realsense C++ opencv程式。relsense/ Kinect之類的深度攝影機原來是可以這樣用,程式產生的點雲ply圖檔用MeshLab自由軟體開啟,效果超乎意料之外的好,再多加些程式設計自製的3D Scanner軟體就快完成了。
//本程式測試於D435i
#include <librealsense2/rs.hpp>
#include <librealsense2/rs_advanced_mode.hpp>//進階設定
#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>
using namespace std;
using namespace cv;
using namespace rs2;
using namespace rs400;//進階設定
int main() try
{
	cout << "存點雲到point_cloud.ply\n";
	colorizer color_map(3);
	//0 - Jet,  1 - Classic  2 - WhiteToBlack 3 - BlackToWhite 4 - Bio 5 - Cold 
	//6 - Warm 7 - Quantized 8 - Pattern 9 - Hue

	rs2::align align2(RS2_STREAM_COLOR);

	pipeline pipe;
	//多加幾行就能錄製整個深度攝影機的錄製資訊
	config cfg;
	int w = 1280, h = 720, FPS = 30;
	cfg.enable_record_to_file("test001.bag");//錄製
	cfg.enable_stream(RS2_STREAM_COLOR);
	cfg.enable_stream(RS2_STREAM_DEPTH);
	cfg.enable_stream(RS2_STREAM_INFRARED, 1, w, h, RS2_FORMAT_Y8, FPS);
	cfg.enable_stream(RS2_STREAM_INFRARED, 2, w, h, RS2_FORMAT_Y8, FPS);

	auto profile = pipe.start(cfg);

	//參考https://github.com/IntelRealSense/librealsense/issues/3067
	auto sensor = profile.get_device().first<depth_sensor>();
	sensor.set_option(rs2_option::RS2_OPTION_VISUAL_PRESET,
		rs2_rs400_visual_preset::RS2_RS400_VISUAL_PRESET_HIGH_DENSITY);

	pointcloud pc;
	points points;
	hole_filling_filter holeFilter;//補洞filter
	while (1)
	{
		frameset data = pipe.wait_for_frames();//
		data.apply_filter(holeFilter);//點雲補洞
		data = align2.process(data);

		frame depth = data.get_depth_frame();
		frame color = data.get_color_frame();
		if (depth)
		{
			points = pc.calculate(depth);
			pc.map_to(color);
			Mat pc_mat(720, 1280, CV_64FC3, (void*)points.get_data());
			Mat depth_mat(h, w, CV_8UC3, (void*)depth.apply_filter(color_map).get_data());
			Mat color_mat(h, w, CV_8UC3, (void*)color.get_data()), color_mat2;
			// display depth_mat
			imshow("depth_mat", depth_mat);
			dilate(depth_mat, depth_mat, 1);
			erode(depth_mat, depth_mat, 1);
			threshold(depth_mat, depth_mat, 180, 255, THRESH_BINARY);
			cvtColor(color_mat, color_mat2, COLOR_RGB2BGR);
			color_mat2=depth_mat  | color_mat2;
			imshow("color_mat & depth_mat", color_mat2);

			int k = waitKey(1);
			if (k == 27) {
				points.export_to_ply("point_cloud.ply", color);
				break;
			}
		}
	}
	fstream ff("point_cloud.ply");
	string ss;
	do{
		getline(ff, ss);
		cout << ss << endl;
	} while (ss != "end_header");
	return 0;
}
catch (const rs2::error& e)
{
	cerr << "RealSense error calling " << e.get_failed_function()
		<< "(" << e.get_failed_args() << "):\n    "
		<< e.what();
	return 1;
}
catch (exception& e) {
	cerr << e.what();
	return 1;
}

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章