title: Generate image from an unorganized Point Cloud in PCL
body: <p>I have an unorganized point cloud of the scene. Below is a screenshot of the point cloud-</p>
  
  <p><a href="https://i.stack.imgur.com/mKF2a.png" rel="noreferrer"><img src="https://i.stack.imgur.com/mKF2a.png" alt="a screenshot of the point cloud"></a></p>
  
  <p>I want to compose an image from this point cloud. Below is the code snippet-</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;pcl/io/pcd_io.h&gt;
  #include &lt;pcl/point_types.h&gt;
  #include &lt;opencv2/opencv.hpp&gt;
  
  int main(int argc, char** argv)
  {
      pcl::PointCloud&lt;pcl::PointXYZRGBA&gt;::Ptr cloud(new pcl::PointCloud&lt;pcl::PointXYZRGBA&gt;);
      pcl::io::loadPCDFile("file.pcd", *cloud);
  
      cv::Mat image = cv::Mat(cloud-&gt;height, cloud-&gt;width, CV_8UC3);
      for (int i = 0; i &lt; image.rows; i++)
      {
          for (int j = 0; j &lt; image.cols; j++)
          {
              pcl::PointXYZRGBA point = cloud-&gt;at(j, i);
              image.at&lt;cv::Vec3b&gt;(i, j)[0] = point.b;
              image.at&lt;cv::Vec3b&gt;(i, j)[1] = point.g;
              image.at&lt;cv::Vec3b&gt;(i, j)[2] = point.r;
          }
      }
      cv::imwrite("image.png", image);
  
      return (0);
  }
  </code></pre>
  
  <p>The PCD file can be found <a href="https://www.dropbox.com/s/ivcyugkwt0zuhk1/file.pcd?dl=0" rel="noreferrer">here</a>. The above code throws following error at runtime-</p>
  
  <pre><code>terminate called after throwing an instance of 'pcl::IsNotDenseException'
    what():  : Can't use 2D indexing with a unorganized point cloud
  </code></pre>
  
  <p>Since the cloud is unorganized, the <code>HEIGHT</code> field is 1. This makes me confused while defining the dimensions of the image. </p>
  
  <p><strong>Questions</strong></p>
  
  <ol>
  <li>How to compose an image from an unorganized point cloud?</li>
  <li>How to convert pixels located in composed image back to point cloud (3D space)?</li>
  </ol>
  
  <p>PS: I am using PCL 1.7 in Ubuntu 14.04 LTS OS.</p>