Skip to content

Commit 4446ef5

Browse files
committed
Merge pull request #2132 from AHolliday:fix_2102_redux
2 parents 4df17e6 + 81b6165 commit 4446ef5

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

modules/ximgproc/src/selectivesearchsegmentation.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -558,9 +558,14 @@ namespace cv {
558558

559559
center = Point((int)(img_plane_rotated.cols / 2.0), (int)(img_plane_rotated.rows / 2.0));
560560
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());
562564

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));
564569

565570
threshold(tmp_gradiant, tmp_gradiant_pos, 0, 0, THRESH_TOZERO);
566571
threshold(tmp_gradiant, tmp_gradiant_neg, 0, 0, THRESH_TOZERO_INV);
@@ -573,9 +578,12 @@ namespace cv {
573578

574579
center = Point((int)(img_plane_rotated.cols / 2.0), (int)(img_plane_rotated.rows / 2.0));
575580
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());
577583

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));
579587

580588
threshold(tmp_gradiant, tmp_gradiant_pos, 0, 0, THRESH_TOZERO);
581589
threshold(tmp_gradiant, tmp_gradiant_neg, 0, 0, THRESH_TOZERO_INV);

0 commit comments

Comments
 (0)