Fix bugs in blob detector

When passing grayscale images by value, they can't be changed inside a
function. Also, fixed an infinite loop when more than 0 but less than 3
blobs were getting detected.

Signed-off-by: Milind Upadhyay <milind.upadhyay@gmail.com>
Change-Id: I5403d02aa9e3df07800791312f0e18ee81824868
diff --git a/y2022/vision/blob_detector.cc b/y2022/vision/blob_detector.cc
index 8fc70f6..8ac96cd 100644
--- a/y2022/vision/blob_detector.cc
+++ b/y2022/vision/blob_detector.cc
@@ -200,8 +200,10 @@
 
   // If we see more than this number of blobs after filtering based on
   // color/size, the circle fit may detect noise so just return no blobs.
+  constexpr size_t kMinFilteredBlobs = 3;
   constexpr size_t kMaxFilteredBlobs = 50;
-  if (filtered_blobs.size() > 0 && filtered_blobs.size() <= kMaxFilteredBlobs) {
+  if (filtered_blobs.size() >= kMinFilteredBlobs &&
+      filtered_blobs.size() <= kMaxFilteredBlobs) {
     constexpr size_t kRansacIterations = 15;
     for (size_t i = 0; i < kRansacIterations; i++) {
       // Pick 3 random blobs and see how many fit on their circle
@@ -291,7 +293,7 @@
 }
 
 void BlobDetector::ExtractBlobs(
-    cv::Mat rgb_image, cv::Mat binarized_image, cv::Mat blob_image,
+    cv::Mat rgb_image, cv::Mat &binarized_image, cv::Mat blob_image,
     std::vector<std::vector<cv::Point>> &filtered_blobs,
     std::vector<std::vector<cv::Point>> &unfiltered_blobs,
     std::vector<BlobStats> &blob_stats, cv::Point &centroid) {
diff --git a/y2022/vision/blob_detector.h b/y2022/vision/blob_detector.h
index f95b217..a0663fd 100644
--- a/y2022/vision/blob_detector.h
+++ b/y2022/vision/blob_detector.h
@@ -45,7 +45,7 @@
       const std::vector<BlobStats> &blob_stats, cv::Point centroid);
 
   static void ExtractBlobs(
-      cv::Mat rgb_image, cv::Mat binarized_image, cv::Mat blob_image,
+      cv::Mat rgb_image, cv::Mat &binarized_image, cv::Mat blob_image,
       std::vector<std::vector<cv::Point>> &filtered_blobs,
       std::vector<std::vector<cv::Point>> &unfiltered_blobs,
       std::vector<BlobStats> &blob_stats, cv::Point &centroid);
diff --git a/y2022/vision/camera_reader.cc b/y2022/vision/camera_reader.cc
index 4ac6aad..732cf90 100644
--- a/y2022/vision/camera_reader.cc
+++ b/y2022/vision/camera_reader.cc
@@ -40,7 +40,7 @@
              << " on " << team_number;
 }
 
-void CameraReader::ProcessImage(const cv::Mat &image_mat) {
+void CameraReader::ProcessImage(cv::Mat image_mat) {
   // Remember, we're getting YUYV images, so we start by converting to RGB
 
   // TOOD: Need to code this up for blob detection
diff --git a/y2022/vision/camera_reader.h b/y2022/vision/camera_reader.h
index 78abaa5..7c2cc9e 100644
--- a/y2022/vision/camera_reader.h
+++ b/y2022/vision/camera_reader.h
@@ -45,7 +45,7 @@
   const sift::CameraCalibration *FindCameraCalibration() const;
 
   // Processes an image (including sending the results).
-  void ProcessImage(const cv::Mat &image);
+  void ProcessImage(cv::Mat image);
 
   // Reads an image, and then performs all of our processing on it.
   void ReadImage();
diff --git a/y2022/vision/viewer.cc b/y2022/vision/viewer.cc
index 202b6a0..1085aaf 100644
--- a/y2022/vision/viewer.cc
+++ b/y2022/vision/viewer.cc
@@ -122,6 +122,7 @@
     LOG(INFO) << ": # blobs: " << filtered_blobs.size() << " (# removed: "
               << unfiltered_blobs.size() - filtered_blobs.size() << ")";
     cv::imshow("image", rgb_image);
+    cv::imshow("mask", binarized_image);
     cv::imshow("blobs", ret_image);
 
     int keystroke = cv::waitKey(0);