@@ -106,10 +106,10 @@ class ROISelector
106106 bool isDrawing;
107107 Rect2d box;
108108 Mat image;
109+ Point2f startPos;
109110
110111 // parameters for drawing from the center
111112 bool drawFromCenter;
112- Point2f center;
113113
114114 // initializer list
115115 handlerT () : isDrawing(false ), drawFromCenter(true ){};
@@ -136,19 +136,31 @@ class ROISelector
136136 {
137137 if (selectorParams.drawFromCenter )
138138 {
139- selectorParams.box .width = 2 * (x - selectorParams.center .x );
140- selectorParams.box .height = 2 * (y - selectorParams.center .y );
141- selectorParams.box .x = std::min (
142- std::max (selectorParams.center .x - selectorParams.box .width / 2.0 , 0 .), (double )imageSize.width );
143- selectorParams.box .y = std::min (
144- std::max (selectorParams.center .y - selectorParams.box .height / 2.0 , 0 .), (double )imageSize.height );
139+ // limit half extends to imageSize
140+ float halfWidth = std::min (std::min (
141+ std::abs (x - selectorParams.startPos .x ),
142+ selectorParams.startPos .x ),
143+ imageSize.width - selectorParams.startPos .x );
144+ float halfHeight = std::min (std::min (
145+ std::abs (y - selectorParams.startPos .y ),
146+ selectorParams.startPos .y ),
147+ imageSize.height - selectorParams.startPos .y );
148+
149+ selectorParams.box .width = halfWidth * 2 ;
150+ selectorParams.box .height = halfHeight * 2 ;
151+ selectorParams.box .x = selectorParams.startPos .x - halfWidth;
152+ selectorParams.box .y = selectorParams.startPos .y - halfHeight;
153+
145154 }
146155 else
147156 {
148- selectorParams.box .width = std::max (
149- std::min (x - selectorParams.box .x , (double )imageSize.width - selectorParams.box .x ), - selectorParams.box .x );
150- selectorParams.box .height = std::max (
151- std::min (y - selectorParams.box .y , (double )imageSize.height - selectorParams.box .y ), - selectorParams.box .y );
157+ // limit x and y to imageSize
158+ int lx = std::min (std::max (x, 0 ), imageSize.width );
159+ int by = std::min (std::max (y, 0 ), imageSize.height );
160+ selectorParams.box .width = std::abs (lx - selectorParams.startPos .x );
161+ selectorParams.box .height = std::abs (by - selectorParams.startPos .y );
162+ selectorParams.box .x = std::min ((float )lx, selectorParams.startPos .x );
163+ selectorParams.box .y = std::min ((float )by, selectorParams.startPos .y );
152164 }
153165 }
154166 break ;
@@ -157,7 +169,7 @@ class ROISelector
157169 case EVENT_LBUTTONDOWN:
158170 selectorParams.isDrawing = true ;
159171 selectorParams.box = Rect2d (x, y, 0 , 0 );
160- selectorParams.center = Point2f ((float )x, (float )y);
172+ selectorParams.startPos = Point2f ((float )x, (float )y);
161173 break ;
162174
163175 // cleaning up the selected bounding box
0 commit comments