open3d+opencv实现矩形框裁剪点云操作(C++)

👑主页:吾名招财

👓简介:工科学硕,研究方向机器视觉,爱好较广泛...

​💫签名:面朝大海,春暖花开!

open3d+opencv实现矩形框裁剪点云操作(C++)

1,引言

针对彩色图和深度图以及相机内参可以合成点云,而对某一区域的点云数据截取,可以通过二维ROI区域截取深度图及彩色图出来。不过不能整张图像裁剪,其图像大小不能变,把除了截取区域外的给置为0就行了。

2,相关测试数据资源如下

本人上传了一个用于三维重建测试的公开数据集,内含彩色图、深度图、相机内参、相机位姿等相关数据,可用于相关测试
https://download.csdn.net/download/qq_44870829/90236553

3,彩色图、深度图和相机内参


4,C++代码

复制代码
#include <string>
#include <iostream>
#include "Open3D/Open3D.h"
#include <opencv2/opencv.hpp>

using namespace std;


open3d::geometry::Image o3d_cv(const cv::Mat& A) {
	open3d::geometry::Image B;
	int bytes_per_channel = (A.depth() / 2 + 1);//refer to the fuction depth
	B.Prepare(A.cols, A.rows, A.channels(), bytes_per_channel);
	std::memcpy(B.data_.data(), A.data, A.total() * A.channels() * bytes_per_channel);
	return B;
}

int main(int argc, char* argv[]) {

	//--------------------------------------1,相关参数----------------------------------
	//要截取的ROI区域大小
	int x = 285;
	int y = 363;
	int w = 150;
	int h = 120;

    //相机内参设置
	int width = 640;   // 输入图像的宽度
	int height = 480;  // 输入图像的高度
	double fx = 585; // x轴焦距 
	double fy = 585; // y轴焦距
	double cx = 320; // 相机原点的x坐标
	double cy = 240; // 相机原点的y坐标

	// 方式一
	auto intrinsic = open3d::camera::PinholeCameraIntrinsic(width, height, fx, fy, cx, cy); // 使用自定义内参
	 方式二
	open3d::camera::PinholeCameraIntrinsic intrinsic = open3d::camera::PinholeCameraIntrinsic();
	intrinsic.SetIntrinsics(width, height, fx, fy, cx, cy);
	//open3d::camera::PinholeCameraIntrinsic intrinsic = open3d::camera::PinholeCameraIntrinsic(
	//	open3d::camera::PinholeCameraIntrinsicParameters::PrimeSenseDefault); // 使用默认内参

    // -RGBD图像参数设置
	double depth_scale = 1000.0; // 深度值的缩放倍数
	double depth_trunc = 3.0;    // 深度值的截断系数
	bool convert_rgb_to_intensity = false; // 是否将彩色图转为强度图

	//-------------------------------------2,读取图像并裁剪-------------------------------------
	//用opencv读取二维彩色图及深度图像,将某一ROI区域裁剪出来,然后再进行点云生成
	cv::Mat colorimg = cv::imread("frame-000276.color.jpg");
	cv::Mat depthimg = cv::imread("frame-000276.depth.png", -1);  //深度图要读取原图

	//制作ROI区域掩膜
	cv::Mat templateImg_color = cv::Mat::zeros(colorimg.size(), colorimg.type());
	cv::Mat mask = cv::Mat::zeros(colorimg.size(), colorimg.type());
	cv::rectangle(mask, cv::Point(x, y), cv::Point(x + w, y + h), cv::Scalar(255, 255, 255), -1);//矩形的两个顶点,两个顶点都包括在矩形内部
	colorimg.copyTo(templateImg_color, mask);

	cv::Mat templateImg_depth = cv::Mat::zeros(depthimg.size(), depthimg.type());
	cv::Mat mask2 = cv::Mat::zeros(depthimg.size(), CV_8UC1);
	cv::rectangle(mask2, cv::Point(x, y), cv::Point(x + w, y + h), cv::Scalar(255, 255, 255), -1);//矩形的两个顶点,两个顶点都包括在矩形内部
	//cv::bitwise_and();
	depthimg.copyTo(templateImg_depth, mask2);

	//-------------------------------------3,显示未裁剪前的原始点云-------------------------------------
	open3d::geometry::Image color_o = o3d_cv(colorimg);
	open3d::geometry::Image depth_o = o3d_cv(depthimg);
	// 生成RGBD图像
	std::shared_ptr<open3d::geometry::RGBDImage> rgbd_image_o = open3d::geometry::RGBDImage::CreateFromColorAndDepth(
		color_o,                    // 输入的彩色图像
		depth_o,                    // 输入的深度图像
		depth_scale,              // 深度值的缩放倍数
		depth_trunc,              // 深度值大于该值将被截断为0
		convert_rgb_to_intensity);// 设置是否将彩色图像转为强度图
   // RGBD转点云
	auto pcd_o = open3d::geometry::PointCloud::CreateFromRGBDImage(*rgbd_image_o, intrinsic);
	open3d::visualization::DrawGeometries({ pcd_o });

	//-------------------------------------4,显示并保存裁剪后的点云-------------------------------------
	open3d::geometry::Image color = o3d_cv(templateImg_color);
	open3d::geometry::Image depth = o3d_cv(templateImg_depth);

	直接使用open3d读取图像
	//open3d::geometry::Image color, depth;
	//open3d::io::ReadImage("0.png", color); // 读取彩色图像
	//open3d::io::ReadImage("0_depth.png", depth); // 读取深度图像

	//将裁剪后的深度图及彩色图转换成点云并显示保存

	//输出图像基本信息
	open3d::utility::LogInfo("Reading RGBD image : ");
	open3d::utility::LogInfo("     Color : {:d} x {:d} x {:d} ({:d} bits per channel)",
		color.width_, color.height_, color.num_of_channels_,
		color.bytes_per_channel_ * 8);
	open3d::utility::LogInfo("Depth : {:d} x {:d} x {:d} ({:d} bits per channel)",
		depth.width_, depth.height_, depth.num_of_channels_,
		depth.bytes_per_channel_ * 8);

	// 生成RGBD图像
	std::shared_ptr<open3d::geometry::RGBDImage> rgbd_image = open3d::geometry::RGBDImage::CreateFromColorAndDepth(
		color,                    // 输入的彩色图像
		depth,                    // 输入的深度图像
		depth_scale,              // 深度值的缩放倍数
		depth_trunc,              // 深度值大于该值将被截断为0
		convert_rgb_to_intensity);// 设置是否将彩色图像转为强度图
    // RGBD转点云
	auto pcd = open3d::geometry::PointCloud::CreateFromRGBDImage(*rgbd_image, intrinsic);
	open3d::visualization::DrawGeometries({ pcd });

	//5.保存点云文件 保存
	open3d::io::WritePointCloudToPCD("moban.pcd", *pcd, false);
	//open3d::io::WritePointCloudToPCD("search.pcd", *pcd, false);

	return 0;
}

5,最终效果

未裁剪前的点云

裁剪后的点云

相关推荐
isyoungboy19 小时前
PIL与OpenCV双线性插值实现差异导致模型精度不够踩坑
人工智能·opencv·计算机视觉
std787919 小时前
用C++ 实现屏幕保护程序
开发语言·c++
tumu_C19 小时前
无用知识研究:在trailing return type利用decltype,comma operator在对函数进行sfinae原创 [二]
开发语言·c++·算法
红糖生姜19 小时前
P12874 [蓝桥杯 2025 国 Python A] 巡逻||题解||图论
c++·蓝桥杯·图论
云泽80820 小时前
C/C++内存管理详解:从基础原理到自定义内存池原理
java·c语言·c++
小年糕是糕手21 小时前
【数据结构】双向链表“0”基础知识讲解 + 实战演练
c语言·开发语言·数据结构·c++·学习·算法·链表
将车24421 小时前
C++实现二叉树搜索树
开发语言·数据结构·c++·笔记·学习
arron88991 天前
Visual Studio 2017(VS2017)可以编译 OpenCV 4.5.5 为 32 位(x86)版本
ide·opencv·visual studio
Dream it possible!1 天前
LeetCode 面试经典 150_栈_简化路径(53_71_C++_中等)(栈+stringstream)
c++·leetcode·面试·
爱和冰阔落1 天前
【C++继承下】继承与友元 / static 菱形继承与虚继承 组合的详解分析
c++·面试·腾讯云ai代码助手