00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 #include <pcl/pcl_config.h>
00039 #ifdef HAVE_OPENNI
00040 
00041 #include <pcl/io/openni_camera/openni_depth_image.h>
00042 #include <sstream>
00043 #include <limits>
00044 #include <iostream>
00045 
00046 using namespace std;
00047 
00048 namespace openni_wrapper
00049 {
00050 
00051   void
00052   DepthImage::fillDepthImageRaw (unsigned width, unsigned height, unsigned short* depth_buffer, unsigned line_step) const
00053   {
00054     if (width > depth_md_->XRes () || height > depth_md_->YRes ())
00055       THROW_OPENNI_EXCEPTION ("upsampling not supported: %d x %d -> %d x %d", depth_md_->XRes (), depth_md_->YRes (), width, height);
00056 
00057     if (depth_md_->XRes () % width != 0 || depth_md_->YRes () % height != 0)
00058       THROW_OPENNI_EXCEPTION ("downsampling only supported for integer scale: %d x %d -> %d x %d", depth_md_->XRes (), depth_md_->YRes (), width, height);
00059 
00060     if (line_step == 0)
00061       line_step = width * static_cast<unsigned> (sizeof (unsigned short));
00062 
00063     
00064     if (width == depth_md_->XRes () && height == depth_md_->YRes () && (line_step == width * sizeof (unsigned short)))
00065     {
00066       memcpy (depth_buffer, depth_md_->Data (), depth_md_->DataSize ());
00067       return;
00068     }
00069 
00070     
00071     unsigned bufferSkip = line_step - width * static_cast<unsigned> (sizeof (unsigned short));
00072 
00073     
00074     unsigned xStep = depth_md_->XRes () / width;
00075     unsigned ySkip = (depth_md_->YRes () / height - 1) * depth_md_->XRes ();
00076 
00077     
00078     short bad_point = numeric_limits<short>::quiet_NaN ();
00079     unsigned depthIdx = 0;
00080 
00081     for (unsigned yIdx = 0; yIdx < height; ++yIdx, depthIdx += ySkip)
00082     {
00083       for (unsigned xIdx = 0; xIdx < width; ++xIdx, depthIdx += xStep, ++depth_buffer)
00084       {
00086         if ((*depth_md_)[depthIdx] == 0 ||
00087             (*depth_md_)[depthIdx] == no_sample_value_ ||
00088             (*depth_md_)[depthIdx] == shadow_value_)
00089           *depth_buffer = bad_point;
00090         else
00091         {
00092           *depth_buffer = static_cast<unsigned short> ((*depth_md_)[depthIdx]);
00093         }
00094       }
00095       
00096       if (bufferSkip > 0)
00097       {
00098         char* cBuffer = reinterpret_cast<char*> (depth_buffer);
00099         depth_buffer = reinterpret_cast<unsigned short*> (cBuffer + bufferSkip);
00100       }
00101     }
00102   }
00103 
00104   void
00105   DepthImage::fillDepthImage (unsigned width, unsigned height, float* depth_buffer, unsigned line_step) const
00106   {
00107     if (width > depth_md_->XRes () || height > depth_md_->YRes ())
00108       THROW_OPENNI_EXCEPTION ("upsampling not supported: %d x %d -> %d x %d", depth_md_->XRes (), depth_md_->YRes (), width, height);
00109 
00110     if (depth_md_->XRes () % width != 0 || depth_md_->YRes () % height != 0)
00111       THROW_OPENNI_EXCEPTION ("downsampling only supported for integer scale: %d x %d -> %d x %d", depth_md_->XRes (), depth_md_->YRes (), width, height);
00112 
00113     if (line_step == 0)
00114       line_step = width * static_cast<unsigned> (sizeof (float));
00115 
00116     
00117     unsigned bufferSkip = line_step - width * static_cast<unsigned> (sizeof (float));
00118 
00119     
00120     unsigned xStep = depth_md_->XRes () / width;
00121     unsigned ySkip = (depth_md_->YRes () / height - 1) * depth_md_->XRes ();
00122 
00123     
00124     float bad_point = numeric_limits<float>::quiet_NaN ();
00125     unsigned depthIdx = 0;
00126 
00127     for (unsigned yIdx = 0; yIdx < height; ++yIdx, depthIdx += ySkip)
00128     {
00129       for (unsigned xIdx = 0; xIdx < width; ++xIdx, depthIdx += xStep, ++depth_buffer)
00130       {
00132         if ((*depth_md_)[depthIdx] == 0 ||
00133             (*depth_md_)[depthIdx] == no_sample_value_ ||
00134             (*depth_md_)[depthIdx] == shadow_value_)
00135           *depth_buffer = bad_point;
00136         else
00137         {
00138           *depth_buffer = static_cast<float> ((*depth_md_)[depthIdx]) * 0.001f;
00139         }
00140       }
00141       
00142       if (bufferSkip > 0)
00143       {
00144         char* cBuffer = reinterpret_cast<char*> (depth_buffer);
00145         depth_buffer = reinterpret_cast<float*> (cBuffer + bufferSkip);
00146       }
00147     }
00148   }
00149 
00150   void
00151   DepthImage::fillDisparityImage (unsigned width, unsigned height, float* disparity_buffer, unsigned line_step) const
00152   {
00153     if (width > depth_md_->XRes () || height > depth_md_->YRes ())
00154       THROW_OPENNI_EXCEPTION ("upsampling not supported: %d x %d -> %d x %d", depth_md_->XRes (), depth_md_->YRes (), width, height);
00155 
00156     if (depth_md_->XRes () % width != 0 || depth_md_->YRes () % height != 0)
00157       THROW_OPENNI_EXCEPTION ("downsampling only supported for integer scale: %d x %d -> %d x %d", depth_md_->XRes (), depth_md_->YRes (), width, height);
00158 
00159     if (line_step == 0)
00160       line_step = width * static_cast<unsigned> (sizeof (float));
00161 
00162     unsigned xStep = depth_md_->XRes () / width;
00163     unsigned ySkip = (depth_md_->YRes () / height - 1) * depth_md_->XRes ();
00164 
00165     unsigned bufferSkip = line_step - width * static_cast<unsigned> (sizeof (float));
00166 
00167     
00168     
00169     
00170     float constant = focal_length_ * baseline_ * 1000.0f / static_cast<float> (xStep);
00171 
00172     for (unsigned yIdx = 0, depthIdx = 0; yIdx < height; ++yIdx, depthIdx += ySkip)
00173     {
00174       for (unsigned xIdx = 0; xIdx < width; ++xIdx, depthIdx += xStep, ++disparity_buffer)
00175       {
00176         if ((*depth_md_)[depthIdx] == 0 ||
00177             (*depth_md_)[depthIdx] == no_sample_value_ ||
00178             (*depth_md_)[depthIdx] == shadow_value_)
00179           *disparity_buffer = 0.0;
00180         else
00181           *disparity_buffer = constant / static_cast<float> ((*depth_md_)[depthIdx]);
00182       }
00183 
00184       
00185       if (bufferSkip > 0)
00186       {
00187         char* cBuffer = reinterpret_cast<char*> (disparity_buffer);
00188         disparity_buffer = reinterpret_cast<float*> (cBuffer + bufferSkip);
00189       }
00190     }
00191   }
00192 } 
00193 #endif //HAVE_OPENNI