title: reprojectImageTo3D() in OpenCV and point cloud 3d modelling
body: <p>! I'm doing some work on 3d modelling with opencv and point cloud library. 
  I have done a lot of research and came to know that after cameraCalibration -> use stereorectification then -> reprojectImageto3d -> show results in 3d viewer with PointCloud.</p>
  
  <p>Since I've done all the steps but I couldn't make it to the results I really wanted. The problem I'm facing is after stereorectification the initUndistortrectifymap and remap gives me very bad results like only a type of 3 lines and because of these the 3d view result is very bad. 
  Anyone here please help me cause I had such a short time to complete this work.<br>
  P.S.
  I can't be able to attach any picture of my result here Cause I'm new and I heard that I've to make 50+ reputations etc. 
  I'm posting my code may be that will help you. 
  below is the code I'm using.</p>
  
  <pre><code>  Mat imgLeft = imread("trible.jpg", 0);
      Mat imgRight = imread("trible1.JPG", 0);
  
      cv::Mat_&lt;double&gt; CameraMatrix1(3, 3);
      cv::Mat_&lt;double&gt; CameraMatrix2(3, 3);
      cv::Mat_&lt;double&gt; disCoeffs1(8, 1);
      cv::Mat_&lt;double&gt; disCoeffs2(8, 1);
      cv::Mat_&lt;double&gt; R(3,3);
      cv::Mat_&lt;double&gt; T(3, 1);
  
      CameraMatrix1 &lt;&lt; 3.7418800905573778e+002, 0, 2.8292646601205792e+002, 0,
          3.7418800905573778e+002, 1.9600567927223364e+002, 0, 0, 1;
      CameraMatrix2 &lt;&lt; 3.7418800905573778e+002, 0, 1.9738001176691122e+002, 0,
          3.7418800905573778e+002, 1.9213950093058386e+002, 0, 0, 1;
      disCoeffs1 &lt;&lt; 8.4846284211974010e-001, -6.8586657466127905e+000, 0, 0, 0,
          0, 0, -2.1939018609332440e+001;
      disCoeffs2 &lt;&lt; -1.0450215273644869e+000, 5.6341039084121618e+001, 0, 0, 0,
          0, 0, 2.5604871618687031e+002;
      R &lt;&lt; 9.9004326094229356e-001, -3.0104115731476408e-002,
          -1.3750666776115050e-001, 4.9787774041489000e-002,
          9.8860863120095721e-001, 1.4203574152643209e-001,
          1.3166441819543800e-001, -1.4746767961487581e-001,
          9.8026412994165057e-001;
      T &lt;&lt; 3.6832840828600544e+000, -1.3250441137852145e+000,
          3.1984415574963926e-001;
  
      cv::Mat R1, R2, P1, P2, Q;
      cv::stereoRectify(CameraMatrix1, disCoeffs1, CameraMatrix2, disCoeffs2, imgLeft.size(), R, T, R1, R2, P1, P2, Q, 0, 0, imgLeft.size(),0,0);
  
      double Q03, Q13, Q23, Q32, Q33;
      Q03 = Q.at&lt;double&gt;(0, 3);
      Q13 = Q.at&lt;double&gt;(1, 3);
      Q23 = Q.at&lt;double&gt;(2, 3);
      Q32 = Q.at&lt;double&gt;(3, 2);
      Q33 = Q.at&lt;double&gt;(3, 3);
  
      std::cout &lt;&lt; "Q(0,3) = " &lt;&lt; Q03 &lt;&lt; "; Q(1,3) = " &lt;&lt; Q13 &lt;&lt; "; Q(2,3) = " &lt;&lt; Q23 &lt;&lt; "; Q(3,2) = " &lt;&lt; Q32 &lt;&lt; "; Q(3,3) = " &lt;&lt; Q33 &lt;&lt; ";" &lt;&lt; std::endl;
  
  
      Mat map11, map12, map21, map22;
      initUndistortRectifyMap(CameraMatrix1, disCoeffs1, R1, P1, imgLeft.size(), CV_32FC1, map11, map12);
      initUndistortRectifyMap(CameraMatrix2, disCoeffs2, R2, P2, imgRight.size(), CV_32FC1, map21, map22);
  
      Mat img1r, img2r;
      remap(imgLeft, img1r, map11, map12, INTER_LINEAR);
      remap(imgRight, img2r, map21, map22, INTER_LINEAR);
  
      imgLeft = img1r;
      imgRight = img2r;
  
      Mat disp, disp8;
      char* method = "BM";
      if (!(strcmp(method, "BM")))
      {
          StereoBM sbm;
          sbm.state-&gt;SADWindowSize = 25;
          sbm.state-&gt;numberOfDisparities = 112; //64; //112
          sbm.state-&gt;preFilterSize = 5;
          sbm.state-&gt;preFilterCap = 61;
          sbm.state-&gt;minDisparity = -39;
          sbm.state-&gt;textureThreshold = 150; //507
          sbm.state-&gt;uniquenessRatio = 0;
          sbm.state-&gt;speckleWindowSize = 0;
          sbm.state-&gt;speckleRange = 8;
          sbm.state-&gt;disp12MaxDiff = 1;
          sbm(imgLeft, imgRight, disp);
      }
      else if (!(strcmp(method, "SGBM")))
      {
          StereoSGBM sbm;
          sbm.SADWindowSize = 25;
          sbm.numberOfDisparities = 64; //144
          sbm.preFilterCap = 63;
          sbm.minDisparity = -10; //-39;
          sbm.uniquenessRatio = 10;
          sbm.speckleWindowSize = 100;
          sbm.speckleRange = 32;
          sbm.disp12MaxDiff = 1;
          sbm.fullDP = false;
          sbm.P1 = 216;
          sbm.P2 = 864;
          sbm(imgLeft, imgRight, disp);
      }
  
      //normalize(disp, disp8, 0, 255, CV_MINMAX, CV_8U);
      normalize(disp, disp8, 0, 255, CV_MINMAX, CV_8UC1);
      // Check its extreme values
      double minVal; double maxVal;
  
      minMaxLoc(disp, &amp;minVal, &amp;maxVal);
  
      printf("Min disp: %f Max value: %f \n", minVal, maxVal);
  
      // Display it as a CV_8UC1 image
      disp.convertTo(disp8, CV_8UC1, 255 / (maxVal - minVal));
  
      cv::Mat recons3D(disp8.size(), CV_32F);
  
      //Reproject image to 3D
      std::cout &lt;&lt; "Reprojecting image to 3D..." &lt;&lt; std::endl;
      cv::reprojectImageTo3D(disp8, recons3D, Q, false, CV_32F);
  
  
  
      waitKey(1);
  
  
  
      //Create point cloud and fill it
      std::cout &lt;&lt; "Creating Point Cloud..." &lt;&lt; std::endl;
      pcl::PointCloud&lt;pcl::PointXYZRGB&gt;::Ptr point_cloud_ptr(new pcl::PointCloud&lt;pcl::PointXYZRGB&gt;);
  
      double px, py, pz;
      uchar pr, pg, pb;
  
      for (int i = 0; i &lt; imgLeft.rows; i++)
      {
          uchar* rgb_ptr = imgLeft.ptr&lt;uchar&gt;(i);
  #ifdef CUSTOM_REPROJECT
          uchar* disp_ptr = disp8.ptr&lt;uchar&gt;(i);
  #else
          float* recons_ptr = recons3D.ptr&lt;float&gt;(i);
  #endif
          for (int j = 0; j &lt; imgRight.cols; j++)
          {
              //Get 3D coordinates
  #ifdef CUSTOM_REPROJECT
              uchar d = disp_ptr[j];
              if (d == 0) continue; //Discard bad pixels
              double pw = -1.0 * static_cast&lt;double&gt;(d)* Q32 + Q33;
              px = static_cast&lt;double&gt;(j)+Q03;
              py = static_cast&lt;double&gt;(i)+Q13;
              pz = Q23;
  
              px = px / pw;
              py = py / pw;
              pz = pz / pw;
  
      #else
              px = recons_ptr[3 * j];
              py = recons_ptr[3 * j + 1];
              pz = recons_ptr[3 * j + 2];
      #endif
  
              //Get RGB info
              pb = rgb_ptr[3 * j];
              pg = rgb_ptr[3 * j + 1];
              pr = rgb_ptr[3 * j + 2];
  
              //Insert info into point cloud structure
              pcl::PointXYZRGB point;
              point.x = px;
              point.y = py;
              point.z = pz;
              uint32_t rgb = (static_cast&lt;uint32_t&gt;(pr) &lt;&lt; 16 |
                  static_cast&lt;uint32_t&gt;(pg) &lt;&lt; 8 | static_cast&lt;uint32_t&gt;(pb)); //NULL
              point.rgb = *reinterpret_cast&lt;float*&gt;(&amp;rgb);
              point_cloud_ptr-&gt;points.push_back(point);
          }
      }
      point_cloud_ptr-&gt;width = (int)point_cloud_ptr-&gt;points.size();
      point_cloud_ptr-&gt;height = 20; //1
  
  
      //Create visualizer
      boost::shared_ptr&lt;pcl::visualization::PCLVisualizer&gt; viewer;
      viewer = createVisualizer(point_cloud_ptr);
  
      //Main loop
      while (!viewer-&gt;wasStopped())
      {
          viewer-&gt;spinOnce(10); //100
          boost::this_thread::sleep(boost::posix_time::microseconds(100000));
      }
  
  
  
  
      return 0;
  }
  </code></pre>