图像处理技术之六:深度图像+彩色图像=点云图像

百般思念 提交于 2020-03-04 19:45:24
#include <pcl/visualization/cloud_viewer.h>
#include <iostream>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <opencv2/opencv.hpp>

using namespace std;
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")

int user_data;
const double u0 = 319.52883;
const double v0 = 271.61749;
const double fx = 528.57523;
const double fy = 527.57387;


void viewerOneOff(pcl::visualization::PCLVisualizer& viewer)
{
 viewer.setBackgroundColor(0.0, 0.0, 0.0);
}

int main()
{
 pcl::PointCloud<pcl::PointXYZRGB> cloud_a;
 pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);

 cv::Mat color = cv::imread("co.jpg");
 cv::Mat depth1 = cv::imread("de.jpg");

 //add
 cv::Size dsize = cv::Size(color.cols, color.rows);
 cv::Mat depth = cv::Mat(dsize, CV_8UC3);
 resize(depth1,depth,dsize);
 //add

 int rowNumber = color.rows;
 int colNumber = color.cols;

 cloud_a.height = rowNumber;
 cloud_a.width = colNumber;
 cloud_a.points.resize(cloud_a.width * cloud_a.height);

 for (unsigned int u = 0; u < rowNumber; ++u)
 {
  for (unsigned int v = 0; v < colNumber; ++v)
  {
   unsigned int num = u*colNumber + v;
   double Xw = 0, Yw = 0, Zw = 0;

   //Zw = ((double)depth.at<uchar>(u, v)) / 255.0 * 10001.0;
   //Xw = (u - u0) * Zw / fx;
   //Yw = (v - v0) * Zw / fy;
   Zw = ((double)depth.at<uchar>(u, v));
   Xw = u;
      Yw = v;

   cloud_a.points[num].b = color.at<cv::Vec3b>(u, v)[0];
   cloud_a.points[num].g = color.at<cv::Vec3b>(u, v)[1];
   cloud_a.points[num].r = color.at<cv::Vec3b>(u, v)[2];

   cloud_a.points[num].x = Xw;
   cloud_a.points[num].y = Yw;
   cloud_a.points[num].z = Zw;
  }
 }

 *cloud = cloud_a;

 pcl::visualization::CloudViewer viewer("Cloud Viewer");

 viewer.showCloud(cloud);

 viewer.runOnVisualizationThreadOnce(viewerOneOff);

 while (!viewer.wasStopped())
 {
  user_data = 9;
 }

 return 0;
}



//要把深度图像转换成彩色图像一样的大小,不然会显示内存异常

 

http://blog.csdn.net/yongshengsilingsa/article/details/37935975

http://blog.csdn.net/kh1445291129/article/details/44636685

http://blog.csdn.net/morewindows/article/details/8239560

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!