当前位置:网站首页>Color map and depth map to point cloud
Color map and depth map to point cloud
2022-08-10 12:05:00 【Full stack programmer webmaster】
大家好,又见面了,我是你们的朋友全栈君.
环境:windows10、VS2013、opencv 2.49、openNi、PCL1.8
opencv 环境搭建参考
VS2013+opencv2.4.9(10)配置[zz] – yifeier12 – 博客园
OpenCV3.1.0+VS2013开发环境配置_Such a coincidence blog-CSDN博客
PCL1.8+openNi搭建参考
Windows10下VS2013+PCL1.8环境配置_Summit_Yue的博客-CSDN博客
windows系统下配置PCL1.8.0和VS2013_大作家佚名的博客-CSDN博客
将上面的opencv和pclThe configuration is saved to the property sheet,for a quick reference next time.
新建项目,Select Solution Configuration SelectionDebug x64,property managerDebug|x64Add the above two property sheets to the
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{
//Save the point cloud map
pcl::io::savePCDFile("E:\\Visual Studio2013\\project\\RGBDtoPC\\data\\pcd.pcd", *cloud);
}
catch (pcl::IOException &e){
cout << e.what()<< endl;
}
//Display point cloud map
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;
}May return directly after running,提示pcl::io Exception
Step through discoverycv::imread()The picture was not read.原因如下
opencv有cvLoadImage()和cv::imread()读图片的方法
And the link library version of the latter is incorrect:(debugThe corresponding library is belowxxxd.lib,release的为xxx.lib) i.e. the additional dependencies in the linker are also added with the bandd和不带d's dependencies will go wrong,如果用DebugDebug only adds the back bandd的即可,将不带d的删除.
I added these
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
Displays the point cloud map reference:
2 pcl读取pcd文件并显示_HxShine的博客-CSDN博客_pcd文件查看器
//Display point cloud map
pcl::visualization::CloudViewer viewer("Simple Cloud Viewer");//直接创造一个显示窗口
viewer.showCloud(cloud);//再这个窗口显示点云color.png
depth.png
运行结果
The depth map and color map are not aligned,The possible reason is that the camera internal parameter settings in the code do not match.
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/129913.html原文链接:https://javaforall.cn
边栏推荐
- LeetCode 86. 分隔链表
- 力扣练习——56 寻找右区间
- LeetCode50天刷题计划(Day 18—— 搜索旋转排序数组(8.50-12.00)
- 基于UiAutomator2+PageObject模式开展APP自动化测试实战
- jlink and swd interface definition
- std::move()
- 孩子自律性不够?猿辅导:计划表要注意“留白”给孩子更多掌控感
- Pulling drills - 56 Finding the right interval
- Analysis of the implementation principle of UUID from the perspective of source code
- 第六届”蓝帽杯“全国大学生网络安全技能大赛半决赛部分WriteUp
猜你喜欢
随机推荐
7、Instant-ngp
LeetCode 21. 合并两个有序链表
力扣练习——64 最长和谐子序列
迈矽科推出高性能77GHz毫米波雷达芯片,尚未量产就已获数万颗订单
gpu-admission 源码分析
rider内Mono脚本找不到引用资源
An enhanced dynamic packet buffer management.论文核心部分
Configuration swagger
Network Fundamentals (Section 1)
模块九 - 设计电商秒杀系统
LeetCode 19. 删除链表的倒数第 N 个结点
Licking Exercise - 63 Find all anagrams in a string
WeChat applet, global variables change in one place and the state in other places also changes.
LeetCode 86. 分隔链表
LeetCode 146. LRU 缓存
Apple bucks the trend and expands iPhone 14 series stocking, with a total of 95 million units
LeetCode 61. 旋转链表
LeetCode 92. Reverse Linked List II
项目部署、
LeetCode 82. 删除排序链表中的重复元素 II









