From 81b61652cc4acf41a07806a9ddb14388ec375433 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 19 Apr 2019 18:33:06 -0400 Subject: [PATCH] Added a fix for issue 2102. --- .../ximgproc/src/selectivesearchsegmentation.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/modules/ximgproc/src/selectivesearchsegmentation.cpp b/modules/ximgproc/src/selectivesearchsegmentation.cpp index 05e80e063..99d35a07e 100644 --- a/modules/ximgproc/src/selectivesearchsegmentation.cpp +++ b/modules/ximgproc/src/selectivesearchsegmentation.cpp @@ -558,9 +558,14 @@ namespace cv { center = Point((int)(img_plane_rotated.cols / 2.0), (int)(img_plane_rotated.rows / 2.0)); rot = cv::getRotationMatrix2D(center, -45.0, 1.0); - warpAffine(tmp_gradiant, tmp_rot, rot, bbox.size()); + // Using this bigger box avoids clipping the ends of narrow images + Rect bbox2 = cv::RotatedRect(center, img_plane_rotated.size(), -45.0).boundingRect();\ + warpAffine(tmp_gradiant, tmp_rot, rot, bbox2.size()); - tmp_gradiant = tmp_rot(Rect((bbox.width - img.cols) / 2, (bbox.height - img.rows) / 2, img.cols, img.rows)); + // for narrow images, bbox might be less tall or wide than img + int start_x = std::max(0, (bbox.width - img.cols) / 2); + int start_y = std::max(0, (bbox.height - img.rows) / 2); + tmp_gradiant = tmp_rot(Rect(start_x, start_y, img.cols, img.rows)); threshold(tmp_gradiant, tmp_gradiant_pos, 0, 0, THRESH_TOZERO); threshold(tmp_gradiant, tmp_gradiant_neg, 0, 0, THRESH_TOZERO_INV); @@ -573,9 +578,12 @@ namespace cv { center = Point((int)(img_plane_rotated.cols / 2.0), (int)(img_plane_rotated.rows / 2.0)); rot = cv::getRotationMatrix2D(center, -45.0, 1.0); - warpAffine(tmp_gradiant, tmp_rot, rot, bbox.size()); + bbox2 = cv::RotatedRect(center, img_plane_rotated.size(), -45.0).boundingRect();\ + warpAffine(tmp_gradiant, tmp_rot, rot, bbox2.size()); - tmp_gradiant = tmp_rot(Rect((bbox.width - img.cols) / 2, (bbox.height - img.rows) / 2, img.cols, img.rows)); + start_x = std::max(0, (bbox.width - img.cols) / 2); + start_y = std::max(0, (bbox.height - img.rows) / 2); + tmp_gradiant = tmp_rot(Rect(start_x, start_y, img.cols, img.rows)); threshold(tmp_gradiant, tmp_gradiant_pos, 0, 0, THRESH_TOZERO); threshold(tmp_gradiant, tmp_gradiant_neg, 0, 0, THRESH_TOZERO_INV);