Adding check that pixels lie within padded border of image left/right

Change-Id: I0711c373a20edcea894c033e0c71af60c8f66190
Signed-off-by: Jim Ostrowski <yimmy13@gmail.com>
diff --git a/y2023/vision/aprilrobotics.cc b/y2023/vision/aprilrobotics.cc
index 7ccde65..3ad6671 100644
--- a/y2023/vision/aprilrobotics.cc
+++ b/y2023/vision/aprilrobotics.cc
@@ -8,6 +8,8 @@
 
 DEFINE_double(min_decision_margin, 75.0,
               "Minimum decision margin (confidence) for an apriltag detection");
+DEFINE_int32(pixel_border, 3,
+             "Size of image border within which to reject detected corners");
 
 namespace y2023 {
 namespace vision {
@@ -18,13 +20,12 @@
                                              std::string_view channel_name)
     : calibration_data_(event_loop),
       ftrace_(),
-      image_callback_(
-          event_loop, channel_name,
-          [&](cv::Mat image_color_mat,
-              const aos::monotonic_clock::time_point eof) {
-            HandleImage(image_color_mat, eof);
-          },
-          chrono::milliseconds(5)),
+      image_callback_(event_loop, channel_name,
+                      [&](cv::Mat image_color_mat,
+                          const aos::monotonic_clock::time_point eof) {
+                        HandleImage(image_color_mat, eof);
+                      },
+                      chrono::milliseconds(5)),
       target_map_sender_(
           event_loop->MakeSender<frc971::vision::TargetMap>("/camera")),
       image_annotations_sender_(
@@ -147,6 +148,8 @@
       .stride = image.cols,
       .buf = image.data,
   };
+  const uint32_t min_x = FLAGS_pixel_border;
+  const uint32_t max_x = image.cols - FLAGS_pixel_border;
 
   ftrace_.FormatMessage("Starting detect\n");
   zarray_t *detections = apriltag_detector_detect(tag_detector_, &im);
@@ -163,6 +166,13 @@
     zarray_get(detections, i, &det);
 
     if (det->decision_margin > FLAGS_min_decision_margin) {
+      if (det->p[0][0] < min_x || det->p[0][0] > max_x ||
+          det->p[1][0] < min_x || det->p[1][0] > max_x ||
+          det->p[2][0] < min_x || det->p[2][0] > max_x ||
+          det->p[3][0] < min_x || det->p[3][0] > max_x) {
+        VLOG(1) << "Rejecting detection because corner is outside pixel border";
+        continue;
+      }
       VLOG(1) << "Found tag number " << det->id << " hamming: " << det->hamming
               << " margin: " << det->decision_margin;