divide image into columns and give min distance for each
[201903hackathon.git] / cpp / sampleOpenCV / sampleOpenCV.cpp
index cd81a3d..0968319 100644 (file)
@@ -39,9 +39,9 @@ using namespace cv;
 class MyListener : public IDepthDataListener
 {
 
-    static uint32_t const num_dist_columns_=5;
-    uint32_t latest_distance_[num_dist_columns_];
-    uint32_t latest_distance_diff_[num_dist_columns_];
+    static uint32_t const num_dist_columns_=4;
+    double latest_min_distance_[num_dist_columns_];
+    double latest_min_distance_diff_[num_dist_columns_];
 
     const int DELAY_BLUR = 100;
     // const int MAX_KERNEL_LENGTH = 31;
@@ -53,6 +53,7 @@ class MyListener : public IDepthDataListener
 
 public :
 
+
     MyListener() :
         undistortImage (false)
     {
@@ -116,6 +117,32 @@ public :
             undistort (temp, zImage8, cameraMatrix, distortionCoefficients);
         }
 
+        if (normblurImage)
+        {
+            auto temp = zImage8.clone();
+            for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
+            {
+                blur( temp, zImage8, Size( i, i ), Point(-1,-1) );
+            }
+        }
+
+        //// Debug: show column part of image
+        // Mat subimg = zImage8(Rect((zImage.cols/num_dist_columns_)*3,0, zImage.cols/num_dist_columns_ , zImage8.rows));
+        // imshow ("column", subimg);
+        //detect column distance
+        auto col_width = zImage.cols/num_dist_columns_;
+        for (uint32_t col=0; col<num_dist_columns_; col++)
+        {
+            auto col_y_start = col*col_width;
+            Mat subimg = zImage8(Rect(col_y_start, 0, col_width , zImage8.rows));
+            double min, max;
+            minMaxLoc(subimg,&min,&max);
+            latest_min_distance_diff_[col]=min-latest_min_distance_[col];
+            latest_min_distance_[col]=min;
+            std::cout << "col" << col << "   min:" << min << "("<<latest_min_distance_diff_[col]<<")"<< "   max:" << max << std::endl;
+        }
+
         // scale and display the depth image
         scaledZImage.create (Size (data->width * 4, data->height * 4), CV_8UC1);
         resize (zImage8, scaledZImage, scaledZImage.size());
@@ -161,6 +188,12 @@ public :
         undistortImage = !undistortImage;
     }
 
+    void toggleNormBlur()
+    {
+        std::lock_guard<std::mutex> lock (flagMutex);
+        normblurImage = !normblurImage;
+    }
+
 private:
 
     // adjust z value to fit fixed scaling, here max dist is 2.5m
@@ -287,6 +320,7 @@ int main (int argc, char *argv[])
     // create two windows
     namedWindow ("Depth", WINDOW_AUTOSIZE);
     namedWindow ("Gray", WINDOW_AUTOSIZE);
+    // namedWindow ("column", WINDOW_AUTOSIZE);
 
     // start capture mode
     if (cameraDevice->startCapture() != CameraStatus::SUCCESS)
@@ -307,6 +341,12 @@ int main (int argc, char *argv[])
             // toggle the undistortion of the image
             listener.toggleUndistort();
         }
+
+        if (currentKey == 'b')
+        {
+            // toggle the undistortion of the image
+            listener.toggleNormBlur();
+        }
     }
 
     // stop capture mode