@@ -558,9 +558,14 @@ namespace cv {
558
558
559
559
center = Point ((int )(img_plane_rotated.cols / 2.0 ), (int )(img_plane_rotated.rows / 2.0 ));
560
560
rot = cv::getRotationMatrix2D (center, -45.0 , 1.0 );
561
- warpAffine (tmp_gradiant, tmp_rot, rot, bbox.size ());
561
+ // Using this bigger box avoids clipping the ends of narrow images
562
+ Rect bbox2 = cv::RotatedRect (center, img_plane_rotated.size (), -45.0 ).boundingRect ();\
563
+ warpAffine (tmp_gradiant, tmp_rot, rot, bbox2.size ());
562
564
563
- tmp_gradiant = tmp_rot (Rect ((bbox.width - img.cols ) / 2 , (bbox.height - img.rows ) / 2 , img.cols , img.rows ));
565
+ // for narrow images, bbox might be less tall or wide than img
566
+ int start_x = std::max (0 , (bbox.width - img.cols ) / 2 );
567
+ int start_y = std::max (0 , (bbox.height - img.rows ) / 2 );
568
+ tmp_gradiant = tmp_rot (Rect (start_x, start_y, img.cols , img.rows ));
564
569
565
570
threshold (tmp_gradiant, tmp_gradiant_pos, 0 , 0 , THRESH_TOZERO);
566
571
threshold (tmp_gradiant, tmp_gradiant_neg, 0 , 0 , THRESH_TOZERO_INV);
@@ -573,9 +578,12 @@ namespace cv {
573
578
574
579
center = Point ((int )(img_plane_rotated.cols / 2.0 ), (int )(img_plane_rotated.rows / 2.0 ));
575
580
rot = cv::getRotationMatrix2D (center, -45.0 , 1.0 );
576
- warpAffine (tmp_gradiant, tmp_rot, rot, bbox.size ());
581
+ bbox2 = cv::RotatedRect (center, img_plane_rotated.size (), -45.0 ).boundingRect ();\
582
+ warpAffine (tmp_gradiant, tmp_rot, rot, bbox2.size ());
577
583
578
- tmp_gradiant = tmp_rot (Rect ((bbox.width - img.cols ) / 2 , (bbox.height - img.rows ) / 2 , img.cols , img.rows ));
584
+ start_x = std::max (0 , (bbox.width - img.cols ) / 2 );
585
+ start_y = std::max (0 , (bbox.height - img.rows ) / 2 );
586
+ tmp_gradiant = tmp_rot (Rect (start_x, start_y, img.cols , img.rows ));
579
587
580
588
threshold (tmp_gradiant, tmp_gradiant_pos, 0 , 0 , THRESH_TOZERO);
581
589
threshold (tmp_gradiant, tmp_gradiant_neg, 0 , 0 , THRESH_TOZERO_INV);
0 commit comments