77
88namespace 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 }
0 commit comments