+ int x1 = window[WindowDataLayer<Dtype>::X1];
+ int y1 = window[WindowDataLayer<Dtype>::Y1];
+ int x2 = window[WindowDataLayer<Dtype>::X2];
+ int y2 = window[WindowDataLayer<Dtype>::Y2];
+
+ int pad_w = 0;
+ int pad_h = 0;
+ if (context_pad > 0) {
+ // scale factor by which to expand the original region
+ // such that after warping the expanded region to cropsize x cropsize
+ // there's exactly context_pad amount of padding on each side
+ Dtype context_scale = static_cast<Dtype>(cropsize) /
+ static_cast<Dtype>(cropsize - 2*context_pad);
+
+ // compute the expanded region
+ Dtype half_height = static_cast<Dtype>(y2-y1+1)/2.0;
+ Dtype half_width = static_cast<Dtype>(x2-x1+1)/2.0;
+ Dtype center_x = static_cast<Dtype>(x1) + half_width;
+ Dtype center_y = static_cast<Dtype>(y1) + half_height;
+ x1 = static_cast<int>(round(center_x - half_width*context_scale));
+ x2 = static_cast<int>(round(center_x + half_width*context_scale));
+ y1 = static_cast<int>(round(center_y - half_height*context_scale));
+ y2 = static_cast<int>(round(center_y + half_height*context_scale));
+
+ // the expanded region may go outside of the image
+ // so we compute the clipped (expanded) region and keep track of
+ // the extent beyond the image
+ int unclipped_height = y2-y1+1;
+ int unclipped_width = x2-x1+1;
+ int pad_x1 = std::max(0, -x1);
+ int pad_y1 = std::max(0, -y1);
+ int pad_x2 = std::max(0, x2 - cv_img.cols + 1);
+ int pad_y2 = std::max(0, y2 - cv_img.rows + 1);
+ // clip bounds
+ x1 = x1 + pad_x1;
+ x2 = x2 - pad_x2;
+ y1 = y1 + pad_y1;
+ y2 = y2 - pad_y2;
+ CHECK_GT(x1, -1);
+ CHECK_GT(y1, -1);
+ CHECK_LT(x2, cv_img.cols);
+ CHECK_LT(y2, cv_img.rows);
+
+ int clipped_height = y2-y1+1;
+ int clipped_width = x2-x1+1;
+
+ // scale factors that would be used to warp the unclipped
+ // expanded region
+ Dtype scale_x =
+ static_cast<Dtype>(cropsize)/static_cast<Dtype>(unclipped_width);
+ Dtype scale_y =
+ static_cast<Dtype>(cropsize)/static_cast<Dtype>(unclipped_height);
+
+ // size to warp the clipped expanded region to
+ cv_crop_size.width =
+ static_cast<int>(round(static_cast<Dtype>(clipped_width)*scale_x));
+ cv_crop_size.height =
+ static_cast<int>(round(static_cast<Dtype>(clipped_height)*scale_y));
+ pad_x1 = static_cast<int>(round(static_cast<Dtype>(pad_x1)*scale_x));
+ pad_x2 = static_cast<int>(round(static_cast<Dtype>(pad_x2)*scale_x));
+ pad_y1 = static_cast<int>(round(static_cast<Dtype>(pad_y1)*scale_y));
+ pad_y2 = static_cast<int>(round(static_cast<Dtype>(pad_y2)*scale_y));
+
+ pad_h = pad_y1;
+ // if we're mirroring, we mirror the padding too (to be pedantic)
+ if (do_mirror) {
+ pad_w = pad_x2;
+ } else {
+ pad_w = pad_x1;
+ }
+
+ // ensure that the warped, clipped region plus the padding
+ // fits in the cropsize x cropsize image (it might not due to rounding)
+ if (pad_h + cv_crop_size.height > cropsize) {
+ cv_crop_size.height = cropsize - pad_h;
+ }
+ if (pad_w + cv_crop_size.width > cropsize) {
+ cv_crop_size.width = cropsize - pad_w;
+ }
+ }
+
+// CHECK_GT(x1, -1);
+// CHECK_GT(y1, -1);
+// CHECK_LT(x1, cv_img.cols);
+// CHECK_LT(y1, cv_img.rows);
+// CHECK_GT(x2, x1-1);
+// CHECK_GT(y2, y1-1);
+// CHECK_LT(x2, cv_img.cols);
+// CHECK_LT(y2, cv_img.rows);
+