Skip to content

Commit 70e9dff

Browse files
committed
L515: downscale depth image if it has been upscaled during registration, fixed depth not correctly scaled in IR mode
1 parent 814a243 commit 70e9dff

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

corelib/src/camera/CameraRealSense2.cpp

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2525
*/
2626

2727
#include <rtabmap/core/camera/CameraRealSense2.h>
28+
#include <rtabmap/core/util2d.h>
2829
#include <rtabmap/utilite/UTimer.h>
2930
#include <rtabmap/utilite/UThreadC.h>
3031
#include <rtabmap/utilite/UConversion.h>
@@ -103,14 +104,17 @@ CameraRealSense2::~CameraRealSense2()
103104
UDEBUG("Closing %d sensor(s) from device %d...", (int)dev_[i]->query_sensors().size(), (int)i);
104105
for(rs2::sensor _sensor : dev_[i]->query_sensors())
105106
{
106-
try
107+
if(!_sensor.get_active_streams().empty())
107108
{
108-
_sensor.stop();
109-
_sensor.close();
110-
}
111-
catch(const rs2::error & error)
112-
{
113-
UWARN("%s", error.what());
109+
try
110+
{
111+
_sensor.stop();
112+
_sensor.close();
113+
}
114+
catch(const rs2::error & error)
115+
{
116+
UWARN("%s", error.what());
117+
}
114118
}
115119
}
116120
#ifdef WIN32
@@ -1298,7 +1302,7 @@ SensorData CameraRealSense2::captureImage(CameraInfo * info)
12981302
if(is_rgb_arrived && is_depth_arrived)
12991303
{
13001304
cv::Mat depth;
1301-
if(ir_)
1305+
if(ir_ && !irDepth_)
13021306
{
13031307
depth = cv::Mat(depthBuffer_.size(), depthBuffer_.type(), (void*)depth_frame.get_data()).clone();
13041308
}
@@ -1307,18 +1311,30 @@ SensorData CameraRealSense2::captureImage(CameraInfo * info)
13071311
rs2::align align(rgb_frame.get_profile().stream_type());
13081312
rs2::frameset processed = frameset.apply_filter(align);
13091313
rs2::depth_frame aligned_depth_frame = processed.get_depth_frame();
1310-
depth = cv::Mat(depthBuffer_.size(), depthBuffer_.type(), (void*)aligned_depth_frame.get_data()).clone();
1314+
if(frameset.get_depth_frame().get_width() < aligned_depth_frame.get_width() &&
1315+
frameset.get_depth_frame().get_height() < aligned_depth_frame.get_height())
1316+
{
1317+
int decimationWidth = int(float(aligned_depth_frame.get_width())/float(frameset.get_depth_frame().get_width())+0.5f);
1318+
int decimationHeight = int(float(aligned_depth_frame.get_height())/float(frameset.get_depth_frame().get_height())+0.5f);
1319+
if(decimationWidth>1 || decimationHeight>1)
1320+
{
1321+
depth = util2d::decimate(cv::Mat(depthBuffer_.size(), depthBuffer_.type(), (void*)aligned_depth_frame.get_data()), decimationWidth>decimationHeight?decimationWidth:decimationHeight);
1322+
}
1323+
else
1324+
{
1325+
depth = cv::Mat(depthBuffer_.size(), depthBuffer_.type(), (void*)aligned_depth_frame.get_data()).clone();
1326+
}
1327+
}
1328+
else
1329+
{
1330+
depth = cv::Mat(depthBuffer_.size(), depthBuffer_.type(), (void*)aligned_depth_frame.get_data()).clone();
1331+
}
13111332
if(depth_scale_meters_ != 0.001f)
13121333
{ // convert to mm
13131334
if(depth.type() == CV_16UC1)
13141335
{
1315-
float scale = depth_scale_meters_ / 0.001f;
1316-
uint16_t *p = depth.ptr<uint16_t>();
1317-
int buffSize = depth.rows * depth.cols;
1318-
#pragma omp parallel for
1319-
for(int i = 0; i < buffSize; ++i) {
1320-
p[i] *= scale;
1321-
}
1336+
float scaleMM = depth_scale_meters_ / 0.001f;
1337+
depth = scaleMM * depth;
13221338
}
13231339
}
13241340
}

0 commit comments

Comments
 (0)