Skip to content

Commit a516bf1

Browse files
committed
Rectifying both streams, scaling to rectified resolution
1 parent 3d8bd67 commit a516bf1

File tree

6 files changed

+96
-91
lines changed

6 files changed

+96
-91
lines changed

include/librealsense2/h/rs_types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ typedef enum rs2_extension
193193
RS2_EXTENSION_DEBUG_STREAM_SENSOR,
194194
RS2_EXTENSION_CALIBRATION_CHANGE_DEVICE,
195195
RS2_EXTENSION_ROTATION_FILTER,
196+
RS2_EXTENSION_RECTIFICATION_FILTER,
196197
RS2_EXTENSION_SAFETY_SENSOR,
197198
RS2_EXTENSION_DEPTH_MAPPING_SENSOR,
198199
RS2_EXTENSION_LABELED_POINTS,

src/ds/d500/d500-color.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "platform/platform-utils.h"
1414
#include <src/metadata-parser.h>
1515
#include <src/ds/ds-thermal-monitor.h>
16+
#include <src/proc/rectification-filter.h>
1617

1718
#include <src/ds/features/auto-exposure-roi-feature.h>
1819

@@ -248,6 +249,32 @@ namespace librealsense
248249

249250
processing_blocks d500_color_sensor::get_recommended_processing_blocks() const
250251
{
251-
return get_color_recommended_proccesing_blocks();
252+
auto res = get_color_recommended_proccesing_blocks();
253+
254+
std::string dev_name = _owner->get_info( RS2_CAMERA_INFO_NAME );
255+
if( dev_name.find( "D555" ) != std::string::npos )
256+
{
257+
std::vector< uint8_t > table_scope = _owner->get_calibration_table(); // Hold data, table returns as temporary
258+
auto table = ds::check_calib< ds::d500_coefficients_table >( table_scope );
259+
float3x3 base_intrinsics = {0};
260+
base_intrinsics( 0, 0 ) = table->right_coefficients_table.base_instrinsics.fx;
261+
base_intrinsics( 1, 1 ) = table->right_coefficients_table.base_instrinsics.fy;
262+
base_intrinsics( 2, 0 ) = table->right_coefficients_table.base_instrinsics.ppx;
263+
base_intrinsics( 2, 1 ) = table->right_coefficients_table.base_instrinsics.ppy;
264+
base_intrinsics( 2, 2 ) = 1;
265+
std::vector< float > coeffs{ table->right_coefficients_table.distortion_coeffs,
266+
table->right_coefficients_table.distortion_coeffs + 5 }; // Using just first 5 coefficients
267+
float3x3 rotation = table->right_coefficients_table.rotation_matrix; // Remove const
268+
float3x3 rectified_intrinsics = { 0 };
269+
rectified_intrinsics( 0, 0 ) = table->rectified_intrinsics.fx;
270+
rectified_intrinsics( 1, 1 ) = table->rectified_intrinsics.fy;
271+
rectified_intrinsics( 2, 0 ) = table->rectified_intrinsics.ppx;
272+
rectified_intrinsics( 2, 1 ) = table->rectified_intrinsics.ppy;
273+
rectified_intrinsics( 2, 2 ) = 1;
274+
res.push_back( std::make_shared< rectification_filter >( RS2_STREAM_COLOR, base_intrinsics, coeffs, rotation, rectified_intrinsics,
275+
table->rectified_intrinsics.image_width, table->rectified_intrinsics.image_height ) );
276+
}
277+
278+
return res;
252279
}
253280
}

src/ds/d500/d500-device.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,26 @@ namespace librealsense
141141
std::string dev_name = _owner->get_info( RS2_CAMERA_INFO_NAME );
142142
if( dev_name.find( "D555" ) != std::string::npos )
143143
{
144-
auto * table = reinterpret_cast< librealsense::ds::d500_coefficients_table * >( _owner->get_calibration_table().data() );
145-
146-
res.push_back( std::make_shared< rectification_filter >( table->left_coefficients_table.base_instrinsics,
147-
table->left_coefficients_table.distortion_coeffs,
148-
table->left_coefficients_table.rotation_matrix,
149-
table->rectified_intrinsics ) );
144+
std::vector< uint8_t > table_scope = _owner->get_calibration_table(); // Hold data, table returns as temporary
145+
auto table = ds::check_calib< ds::d500_coefficients_table >( table_scope );
146+
//auto * table = reinterpret_cast< ds::d500_coefficients_table * >( _owner->get_calibration_table().data() );
147+
float3x3 base_intrinsics = {0};
148+
base_intrinsics( 1, 1 ) = table->left_coefficients_table.base_instrinsics.fy;
149+
base_intrinsics( 2, 0 ) = table->left_coefficients_table.base_instrinsics.ppx;
150+
base_intrinsics( 2, 1 ) = table->left_coefficients_table.base_instrinsics.ppy;
151+
base_intrinsics( 0, 0 ) = table->left_coefficients_table.base_instrinsics.fx;
152+
base_intrinsics( 2, 2 ) = 1;
153+
std::vector< float > coeffs{ table->left_coefficients_table.distortion_coeffs,
154+
table->left_coefficients_table.distortion_coeffs + 5 }; // Using just first 5 coefficients
155+
float3x3 rotation = table->left_coefficients_table.rotation_matrix; // Remove const
156+
float3x3 rectified_intrinsics = { 0 };
157+
rectified_intrinsics( 0, 0 ) = table->rectified_intrinsics.fx;
158+
rectified_intrinsics( 1, 1 ) = table->rectified_intrinsics.fy;
159+
rectified_intrinsics( 2, 0 ) = table->rectified_intrinsics.ppx;
160+
rectified_intrinsics( 2, 1 ) = table->rectified_intrinsics.ppy;
161+
rectified_intrinsics( 2, 2 ) = 1;
162+
res.push_back( std::make_shared< rectification_filter >( RS2_STREAM_INFRARED, base_intrinsics, coeffs, rotation, rectified_intrinsics,
163+
table->rectified_intrinsics.image_width, table->rectified_intrinsics.image_height ) );
150164
}
151165

152166
return res;

src/proc/color-formats-converter.cpp

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -785,55 +785,6 @@ namespace librealsense
785785
break;
786786
}
787787
}
788-
// --- Rectification parameters for one camera ---
789-
struct RectificationParams
790-
{
791-
cv::Mat cameraMatrix;
792-
cv::Mat distCoeffs;
793-
cv::Mat R;
794-
cv::Mat newCamMat;
795-
cv::Size imageSize;
796-
};
797-
798-
void rectify( uint8_t * const d[], const uint8_t * s, int width, int height, int actual_size )
799-
{
800-
static bool first_time = true;
801-
static cv::Mat map1, map2;
802-
static cv::Mat rectified;
803-
if( first_time )
804-
{
805-
RectificationParams params;
806-
params.cameraMatrix = ( cv::Mat_< double >( 3, 3 ) << 649.6231, 0, 636.1855, 0, 648.288, 402.3958, 0, 0, 1 );
807-
params.distCoeffs = ( cv::Mat_< double >( 1, 5 ) << -0.052214, 0.060396, 0.00053593, -0.0014044, -0.018184 );
808-
params.R = ( cv::Mat_< double >( 3, 3 ) <<
809-
0.999949436295546, 0.002088371842696, -0.009836846815234,
810-
-0.002097318108441, 0.999997396311540, -8.992368395849524e-04,
811-
0.009834943262254, 9.198223677947176e-04, 0.999951212718821 );
812-
params.newCamMat = ( cv::Mat_< double >( 3, 3 ) << 650.6063, 0, 638.5314, 0, 650.6063, 363.5143, 0, 0, 1 );
813-
params.imageSize = cv::Size( 1280, 800 );
814-
815-
// Preallocate buffer
816-
rectified.create( cv::Size( 1280, 800 ), CV_8UC3 );
817-
818-
// Precompute undistort/rectify maps
819-
cv::initUndistortRectifyMap( params.cameraMatrix, params.distCoeffs, params.R,
820-
params.newCamMat, params.imageSize, CV_16SC2, map1, map2 );
821-
822-
first_time = false;
823-
}
824-
825-
if( true )
826-
{
827-
// Remap to rectified image
828-
cv::Mat rgbBuffer( height, width, CV_8UC3, const_cast< uint8_t * >( s ) ); // Uses the data without copying it
829-
cv::remap( rgbBuffer, rectified, map1, map2, cv::INTER_LINEAR );
830-
memcpy( d[0], rectified.data, actual_size );
831-
}
832-
else
833-
{
834-
memcpy( d[0], s, actual_size );
835-
}
836-
}
837788

838789
void unpack_m420(rs2_format dst_format, rs2_stream dst_stream, uint8_t * const d[], const uint8_t * s, int w, int h, int actual_size)
839790
{
@@ -847,11 +798,7 @@ namespace librealsense
847798
unpack_m420<RS2_FORMAT_Y16>(d, s, w, h, actual_size);
848799
break;
849800
case RS2_FORMAT_RGB8:
850-
uint8_t * unrectified_rgb[1];
851-
unrectified_rgb[0] = new uint8_t[w * h * 3];
852-
unpack_m420< RS2_FORMAT_RGB8 >( unrectified_rgb, s, w, h, actual_size );
853-
rectify( d, unrectified_rgb[0], w, h, actual_size );
854-
delete[] unrectified_rgb[0];
801+
unpack_m420< RS2_FORMAT_RGB8 >( d, s, w, h, actual_size );
855802
break;
856803
case RS2_FORMAT_RGBA8:
857804
unpack_m420<RS2_FORMAT_RGBA8>(d, s, w, h, actual_size);

src/proc/rectification-filter.cpp

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,49 @@
77

88
namespace librealsense
99
{
10-
rectification_filter::rectification_filter( rsutils::number::float3x3 & k_distorted,
10+
rectification_filter::rectification_filter() : stream_filter_processing_block("Rectification Filter")
11+
{
12+
}
13+
14+
rectification_filter::rectification_filter( rs2_stream stream_to_rectify,
15+
rsutils::number::float3x3 & k_distorted,
1116
std::vector< float > & dist_coeffs,
1217
rsutils::number::float3x3 & rotation_mat,
13-
rsutils::number::float3x3 & k_rect )
18+
rsutils::number::float3x3 & k_rect,
19+
uint16_t rect_width, uint16_t rect_height )
1420
: stream_filter_processing_block("Rectification Filter")
21+
, _stream_to_rectify( stream_to_rectify )
1522
{
16-
_stream_filter.stream = RS2_STREAM_COLOR;
17-
_stream_filter.format = RS2_FORMAT_RGB8;
18-
19-
// Initialize rectification parameters with the values from color-formats-converter.cpp
20-
2123
// cv::Mat is raw major, float3x3 is coloumn major
2224
cv::Mat camera_matrix = ( cv::Mat_< double >( 3, 3 ) << k_distorted( 0, 0 ), k_distorted( 1, 0 ), k_distorted( 2, 0 ),
2325
k_distorted( 0, 1 ), k_distorted( 1, 1 ), k_distorted( 2, 1 ),
2426
k_distorted( 0, 2 ), k_distorted( 1, 2 ), k_distorted( 2, 2 ) );
2527

2628
cv::Mat coeffs = ( cv::Mat_< double >( 1, 5 ) << dist_coeffs[0], dist_coeffs[1], dist_coeffs[2], dist_coeffs[3], dist_coeffs[4] );
27-
cv::Mat R = (cv::Mat_<double>(3, 3) << rotation_mat( 0, 0 ), rotation_mat( 1, 0 ), rotation_mat( 2, 0 ),
28-
rotation_mat( 0, 1 ), rotation_mat( 1, 1 ), rotation_mat( 2, 1 ),
29-
rotation_mat( 0, 2 ), rotation_mat( 1, 2 ), rotation_mat( 2, 2 ) );
30-
cv::Mat new_camera_matrix = (cv::Mat_<double>(3, 3) << k_rect( 0, 0 ), k_rect( 1, 0 ), k_rect( 2, 0 ),
31-
k_rect( 0, 1 ), k_rect( 1, 1 ), k_rect( 2, 1 ),
32-
k_rect( 0, 2 ), k_rect( 1, 2 ), k_rect( 2, 2 ) );
29+
30+
cv::Mat R = (cv::Mat_<double>(3, 3) << rotation_mat( 0, 0 ), rotation_mat( 0, 1 ), rotation_mat( 0, 2 ),
31+
rotation_mat( 1, 0 ), rotation_mat( 1, 1 ), rotation_mat( 1, 2 ),
32+
rotation_mat( 2, 0 ), rotation_mat( 2, 1 ), rotation_mat( 2, 2 ) );
33+
34+
// k_rect should be adjusted to new resolution.
35+
rsutils::number::float3x3 scaled_k_rect = k_rect;
36+
37+
auto scale_ratio_x = 1280.0f / static_cast < float >( rect_width );
38+
auto scale_ratio_y = 720.0f / static_cast< float >( rect_height );
39+
auto scale_ratio = std::max< float >( scale_ratio_x, scale_ratio_y );
40+
41+
auto crop_x = ( static_cast< float >( rect_width ) * scale_ratio - 1280.0f ) * 0.5f;
42+
auto crop_y = ( static_cast< float >( rect_height ) * scale_ratio - 720.0f ) * 0.5f;
43+
44+
scaled_k_rect( 2, 0 ) = ( scaled_k_rect( 2, 0 ) + 0.5f ) * scale_ratio - crop_x - 0.5f;
45+
scaled_k_rect( 2, 1 ) = ( scaled_k_rect( 2, 1 ) + 0.5f ) * scale_ratio - crop_y - 0.5f;
46+
47+
scaled_k_rect( 0, 0 ) = scaled_k_rect( 0, 0 ) * scale_ratio;
48+
scaled_k_rect( 1, 1 ) = scaled_k_rect( 1, 1 ) * scale_ratio;
49+
50+
cv::Mat new_camera_matrix = (cv::Mat_<double>(3, 3) << scaled_k_rect( 0, 0 ), scaled_k_rect( 1, 0 ), scaled_k_rect( 2, 0 ),
51+
scaled_k_rect( 0, 1 ), scaled_k_rect( 1, 1 ), scaled_k_rect( 2, 1 ),
52+
scaled_k_rect( 0, 2 ), scaled_k_rect( 1, 2 ), scaled_k_rect( 2, 2 ) );
3353
cv::Size image_size = cv::Size( 1280, 720 );
3454

3555
// Precompute undistort/rectify maps
@@ -41,11 +61,13 @@ namespace librealsense
4161

4262
bool rectification_filter::should_process( const rs2::frame & frame )
4363
{
64+
if( _rectified_buffer.empty() )
65+
return false; // TODO - throw?
66+
4467
if( ! frame || frame.is< rs2::frameset >() )
4568
return false;
4669

47-
auto profile = frame.get_profile();
48-
return profile.stream_type() == RS2_STREAM_COLOR && profile.format() == RS2_FORMAT_RGB8;
70+
return frame.get_profile().format() == RS2_FORMAT_RGB8 && frame.get_profile().stream_type() == _stream_to_rectify;
4971
}
5072

5173
rs2::frame rectification_filter::process_frame( const rs2::frame_source & source, const rs2::frame & f )
@@ -56,11 +78,7 @@ namespace librealsense
5678
int width = vf.get_width();
5779
int height = vf.get_height();
5880
int bpp = vf.get_bytes_per_pixel();
59-
int actual_size = width * height * bpp;
60-
61-
// Allocate output frame
62-
auto target_profile = profile.clone( profile.stream_type(), profile.stream_index(), profile.format() );
63-
auto ret = source.allocate_video_frame( target_profile, f, bpp, width, height, width * bpp, RS2_EXTENSION_VIDEO_FRAME );
81+
auto ret = source.allocate_video_frame( profile, f, bpp, 1280, 720, 1280 * bpp, RS2_EXTENSION_VIDEO_FRAME );
6482

6583
// Perform rectification
6684
uint8_t * src = const_cast< uint8_t * >( reinterpret_cast< const uint8_t * >( vf.get_data() ) );
@@ -69,7 +87,7 @@ namespace librealsense
6987
// Remap to rectified image
7088
cv::Mat rgb_buffer( height, width, CV_8UC3, src ); // Uses the data without copying it
7189
cv::remap( rgb_buffer, _rectified_buffer, _map1, _map2, cv::INTER_LINEAR );
72-
memcpy( dst, _rectified_buffer.data, actual_size );
90+
memcpy( dst, _rectified_buffer.data, 1280 * 720 * bpp );
7391

7492
return ret;
7593
}

src/proc/rectification-filter.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,24 @@ namespace librealsense
1313
class rectification_filter : public stream_filter_processing_block
1414
{
1515
public:
16-
rectification_filter( rsutils::number::float3x3 & k_distorted,
16+
rectification_filter();
17+
rectification_filter( rs2_stream stream_to_rectify,
18+
rsutils::number::float3x3 & k_distorted,
1719
std::vector< float > & dist_coeffs,
1820
rsutils::number::float3x3 & rotation_mat,
19-
rsutils::number::float3x3 & k_rect );
21+
rsutils::number::float3x3 & k_rect,
22+
uint16_t rect_width, uint16_t rect_height);
2023

2124
protected:
2225
rs2::frame process_frame(const rs2::frame_source& source, const rs2::frame& f) override;
2326
bool should_process(const rs2::frame& frame) override;
2427

2528
private:
29+
rs2_stream _stream_to_rectify;
30+
2631
cv::Mat _map1;
2732
cv::Mat _map2;
2833
cv::Mat _rectified_buffer;
29-
30-
// Rectification parameters
31-
cv::Mat _camera_matrix;
32-
cv::Mat _dist_coeffs;
33-
cv::Mat _R;
34-
cv::Mat _new_camera_matrix;
35-
cv::Size _image_size;
3634
};
3735

3836
MAP_EXTENSION(RS2_EXTENSION_RECTIFICATION_FILTER, librealsense::rectification_filter);

0 commit comments

Comments
 (0)