当前位置:网站首页>彩色图和深度图转点云
彩色图和深度图转点云
2022-08-10 11:10:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
环境:windows10、VS2013、opencv 2.49、openNi、PCL1.8
opencv 环境搭建参考
VS2013+opencv2.4.9(10)配置[zz] – yifeier12 – 博客园
OpenCV3.1.0+VS2013开发环境配置_那么巧合的博客-CSDN博客
PCL1.8+openNi搭建参考
Windows10下VS2013+PCL1.8环境配置_Summit_Yue的博客-CSDN博客
windows系统下配置PCL1.8.0和VS2013_大作家佚名的博客-CSDN博客
将上面的opencv和pcl的配置保存到属性表中,以便下一次快速引用。
新建项目,选择解决方案配置选择Debug x64,属性管理器的Debug|x64中添加上面两个属性表
RGBDtoPC.cpp
#include "stdafx.h"
#include <iostream>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <opencv2/opencv.hpp>
#include <string>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <pcl/visualization/cloud_viewer.h>
using namespace std;
// 定义点云类型
typedef pcl::PointXYZRGBA PointT;
typedef pcl::PointCloud<PointT> PointCloud;
// 相机内参
const double camera_factor = 1000;
const double camera_cx = 325.5;
const double camera_cy = 253.5;
const double camera_fx = 518.0;
const double camera_fy = 519.0;
// 主函数
int main(int argc, char** argv)
{
// 读取./data/rgb.png和./data/depth.png,并转化为点云
// 图像矩阵
cv::Mat rgb, depth;
// 使用cv::imread()来读取图像
// API: http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imread#cv2.imread
rgb = cv::imread("color.png");
cout << "read rgb"<<endl;
// rgb 图像是8UC3的彩色图像
// depth 是16UC1的单通道图像,注意flags设置-1,表示读取原始数据不做任何修改
depth = cv::imread("depth.png");
cout << "read depth" << endl;
// 点云变量
// 使用智能指针,创建一个空点云。这种指针用完会自动释放。
PointCloud::Ptr cloud(new PointCloud);
// 遍历深度图
for (int m = 0; m < depth.rows; m++)
for (int n = 0; n < depth.cols; n++)
{
// 获取深度图中(m,n)处的值
ushort d = depth.ptr<ushort>(m)[n];
// d 可能没有值,若如此,跳过此点
if (d == 0)
continue;
// d 存在值,则向点云增加一个点
PointT p;
// 计算这个点的空间坐标
p.z = double(d) / camera_factor;
p.x = (n - camera_cx) * p.z / camera_fx;
p.y = (m - camera_cy) * p.z / camera_fy;
// 从rgb图像中获取它的颜色
// rgb是三通道的BGR格式图,所以按下面的顺序获取颜色
p.b = rgb.ptr<uchar>(m)[n * 3];
p.g = rgb.ptr<uchar>(m)[n * 3 + 1];
p.r = rgb.ptr<uchar>(m)[n * 3 + 2];
// 把p加入到点云中
cloud->points.push_back(p);
//cout << cloud->points.size() << endl;
}
// 设置并保存点云
cloud->height = 1;
cloud->width = cloud->points.size();
cout << "point cloud size = " << cloud->points.size() << endl;
cloud->is_dense = false;
try{
//保存点云图
pcl::io::savePCDFile("E:\\Visual Studio2013\\project\\RGBDtoPC\\data\\pcd.pcd", *cloud);
}
catch (pcl::IOException &e){
cout << e.what()<< endl;
}
//显示点云图
pcl::visualization::CloudViewer viewer("Simple Cloud Viewer");//直接创造一个显示窗口
viewer.showCloud(cloud);//再这个窗口显示点云
while (!viewer.wasStopped())
{
}
//pcl::io::savePCDFileASCII("E:\\Visual Studio2013\\projectpointcloud.pcd", *cloud);
// 清除数据并退出
cloud->points.clear();
cout << "Point cloud saved." << endl;
return 0;
}
运行后可能直接返回,提示pcl::io Exception
单步运行发现cv::imread()并没有读取到图片。原因如下
opencv有cvLoadImage()和cv::imread()读图片的方法
而后者的链接库版本不正确:(debug下对应的库为xxxd.lib,release的为xxx.lib) 即链接器中的附加依赖项中同时添加带d和不带d的依赖项会出问题,如果用Debug调试则只添加后面带d的即可,将不带d的删除。
我添加了这些
opencv_calib3d249d.lib opencv_contrib249d.lib opencv_core249d.lib opencv_features2d249d.lib opencv_flann249d.lib opencv_gpu249d.lib opencv_highgui249d.lib opencv_imgproc249d.lib opencv_legacy249d.lib opencv_ml249d.lib opencv_nonfree249d.lib opencv_objdetect249d.lib opencv_photo249d.lib opencv_stitching249d.lib opencv_ts249d.lib opencv_video249d.lib opencv_videostab249d.lib
显示点云图参考:
2 pcl读取pcd文件并显示_HxShine的博客-CSDN博客_pcd文件查看器
//显示点云图
pcl::visualization::CloudViewer viewer("Simple Cloud Viewer");//直接创造一个显示窗口
viewer.showCloud(cloud);//再这个窗口显示点云
color.png
depth.png
运行结果
深度图和彩色图没有对准,可能的原因是在代码的相机内参设置不匹配。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/129913.html原文链接:https://javaforall.cn
边栏推荐
- HDU 4372:Count the Buildings (Stirling数)
- LeetCode 82. 删除排序链表中的重复元素 II
- 阻塞 非阻塞 poll机制 异步
- 英特尔推送20220809 CPU微码更新 修补Intel-SA-00657安全漏洞
- LeetCode_628_三个数的最大乘积
- 即时零售业态下如何实现自动做账?
- VSCode remote connection server error: Could not establish connection to "xxxxxx" possible error reasons and solutions
- Nocalhost - Making development more efficient in the cloud-native era
- 有哪些好用的性能测试工具推荐?性能测试报告收费标准
- 快速上手,征服三种不同分布式架构调用方案
猜你喜欢
基于UiAutomator2+PageObject模式开展APP自动化测试实战
常量及数据类型你还记得多少?
ViT结构详解(附pytorch代码)
A little self-deprecating deconstruction about farmers "code"
自媒体爆款标题怎么写?手把手教你写热门标题
学长告诉我,大厂MySQL都是通过SSH连接的
【勇敢饭饭,不怕刷题之链表】链表倒数节点问题
Intel pushes 20220809 CPU microcode update to patch Intel-SA-00657 security vulnerability
孩子自律性不够?猿辅导:计划表要注意“留白”给孩子更多掌控感
网络基础(第一节)
随机推荐
Emulate stm32 directly with proteus - the programmer can be completely discarded
Weilai-software development engineer side record
振弦传感器及核心VM系列振弦采集模块
Licking Exercise - 58 Verifying Binary Search Trees
【LeetCode】640. 求解方程
使用.NET简单实现一个Redis的高性能克隆版(六)
Introduction to Software Architecture
实现内网穿透的最佳解决方案(无实名认证,完全免费)
From the product dimension, why can't we fully trust Layer2?
力扣练习——64 最长和谐子序列
The brave rice rice, does not fear the brush list of 】 list has a ring
POJ 3101 Astronomy (Mathematics)
力扣练习——63 找到字符串中所有字母异位词
从产品角度看 L2 应用:为什么说这是一个游乐场?
微信小程序提交审核历史版本记录从哪里查看
LeetCode_443_压缩字符串
How to join We Media, learn about these 5 monetization modes, and make your account quickly monetize
为什么Redis很快
LeetCode50天刷题计划(Day 17—— 下一个序列(14.50-16.30)
LeetCode 138. 复制带随机指针的链表