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 #include <openni_camera/openni_depth_image.h>
00038 #include <sstream>
00039 #include <limits>
00040 #include <iostream>
00041
00042 using namespace std;
00043
00044 namespace openni_wrapper
00045 {
00046
00047 void DepthImage::fillDepthImageRaw (unsigned width, unsigned height, short* depth_buffer, unsigned line_step) const throw (OpenNIException)
00048 {
00049 if (width > depth_md_->XRes () || height > depth_md_->YRes ())
00050 THROW_OPENNI_EXCEPTION ("upsampling not supported: %d x %d -> %d x %d", depth_md_->XRes (), depth_md_->YRes (), width, height);
00051
00052 if (depth_md_->XRes () % width != 0 || depth_md_->YRes () % height != 0)
00053 THROW_OPENNI_EXCEPTION ("downsampling only supported for integer scale: %d x %d -> %d x %d", depth_md_->XRes (), depth_md_->YRes (), width, height);
00054
00055 if (line_step == 0)
00056 line_step = width * sizeof (short);
00057
00058
00059 unsigned bufferSkip = line_step - width * sizeof (short);
00060
00061
00062 unsigned xStep = depth_md_->XRes () / width;
00063 unsigned ySkip = (depth_md_->YRes () / height - 1) * depth_md_->XRes ();
00064
00065
00066 short bad_point = numeric_limits<short>::quiet_NaN ();
00067 unsigned depthIdx = 0;
00068
00069 for (unsigned yIdx = 0; yIdx < height; ++yIdx, depthIdx += ySkip)
00070 {
00071 for (unsigned xIdx = 0; xIdx < width; ++xIdx, depthIdx += xStep, ++depth_buffer)
00072 {
00074 if ((*depth_md_)[depthIdx] == 0 ||
00075 (*depth_md_)[depthIdx] == no_sample_value_ ||
00076 (*depth_md_)[depthIdx] == shadow_value_)
00077 *depth_buffer = bad_point;
00078 else
00079 {
00080 *depth_buffer = (short)(*depth_md_)[depthIdx];
00081 }
00082 }
00083
00084 if (bufferSkip > 0)
00085 {
00086 char* cBuffer = reinterpret_cast<char*>(depth_buffer);
00087 depth_buffer = reinterpret_cast<short*>(cBuffer + bufferSkip);
00088 }
00089 }
00090 }
00091
00092 void DepthImage::fillDepthImage (unsigned width, unsigned height, float* depth_buffer, unsigned line_step) const throw (OpenNIException)
00093 {
00094 if (width > depth_md_->XRes () || height > depth_md_->YRes ())
00095 THROW_OPENNI_EXCEPTION ("upsampling not supported: %d x %d -> %d x %d", depth_md_->XRes (), depth_md_->YRes (), width, height);
00096
00097 if (depth_md_->XRes () % width != 0 || depth_md_->YRes () % height != 0)
00098 THROW_OPENNI_EXCEPTION ("downsampling only supported for integer scale: %d x %d -> %d x %d", depth_md_->XRes (), depth_md_->YRes (), width, height);
00099
00100 if (line_step == 0)
00101 line_step = width * sizeof (float);
00102
00103
00104 unsigned bufferSkip = line_step - width * sizeof (float);
00105
00106
00107 unsigned xStep = depth_md_->XRes () / width;
00108 unsigned ySkip = (depth_md_->YRes () / height - 1) * depth_md_->XRes ();
00109
00110
00111 float bad_point = numeric_limits<float>::quiet_NaN ();
00112 unsigned depthIdx = 0;
00113
00114 for (unsigned yIdx = 0; yIdx < height; ++yIdx, depthIdx += ySkip)
00115 {
00116 for (unsigned xIdx = 0; xIdx < width; ++xIdx, depthIdx += xStep, ++depth_buffer)
00117 {
00119 if ((*depth_md_)[depthIdx] == 0 ||
00120 (*depth_md_)[depthIdx] == no_sample_value_ ||
00121 (*depth_md_)[depthIdx] == shadow_value_)
00122 *depth_buffer = bad_point;
00123 else
00124 {
00125 *depth_buffer = (float)(*depth_md_)[depthIdx] * 0.001f;
00126 }
00127 }
00128
00129 if (bufferSkip > 0)
00130 {
00131 char* cBuffer = reinterpret_cast<char*>(depth_buffer);
00132 depth_buffer = reinterpret_cast<float*>(cBuffer + bufferSkip);
00133 }
00134 }
00135 }
00136
00137 void DepthImage::fillDisparityImage (unsigned width, unsigned height, float* disparity_buffer, unsigned line_step) const throw (OpenNIException)
00138 {
00139 if (width > depth_md_->XRes () || height > depth_md_->YRes ())
00140 THROW_OPENNI_EXCEPTION ("upsampling not supported: %d x %d -> %d x %d", depth_md_->XRes (), depth_md_->YRes (), width, height);
00141
00142 if (depth_md_->XRes () % width != 0 || depth_md_->YRes () % height != 0)
00143 THROW_OPENNI_EXCEPTION ("downsampling only supported for integer scale: %d x %d -> %d x %d", depth_md_->XRes (), depth_md_->YRes (), width, height);
00144
00145 if (line_step == 0)
00146 line_step = width * sizeof (float);
00147
00148 unsigned xStep = depth_md_->XRes () / width;
00149 unsigned ySkip = (depth_md_->YRes () / height - 1) * depth_md_->XRes ();
00150
00151 unsigned bufferSkip = line_step - width * sizeof (float);
00152
00153
00154
00155
00156 float constant = focal_length_ * baseline_ * 1000.0 / (float)xStep;
00157
00158 for (unsigned yIdx = 0, depthIdx = 0; yIdx < height; ++yIdx, depthIdx += ySkip)
00159 {
00160 for (unsigned xIdx = 0; xIdx < width; ++xIdx, depthIdx += xStep, ++disparity_buffer)
00161 {
00162 if ((*depth_md_)[depthIdx] == 0 ||
00163 (*depth_md_)[depthIdx] == no_sample_value_ ||
00164 (*depth_md_)[depthIdx] == shadow_value_)
00165 *disparity_buffer = 0.0;
00166 else
00167 *disparity_buffer = constant / (double)(*depth_md_)[depthIdx];
00168 }
00169
00170
00171 if (bufferSkip > 0)
00172 {
00173 char* cBuffer = reinterpret_cast<char*>(disparity_buffer);
00174 disparity_buffer = reinterpret_cast<float*>(cBuffer + bufferSkip);
00175 }
00176 }
00177 }
00178 }