]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - apps/barcode-roi-detection.git/commitdiff
Add multiple options for static image input, camera input and live drawing
authorDjordje Senicic <d-senicic1@ti.com>
Tue, 20 Sep 2016 16:55:44 +0000 (12:55 -0400)
committerHongmei Gou <h-gou@ti.com>
Mon, 26 Sep 2016 19:53:54 +0000 (15:53 -0400)
Signed-off-by: Djordje Senicic <d-senicic1@ti.com>
detect_barcode.cpp

index 534f7f41b323f5bec2aacd930af7f4975be591ad..3ea35ffb5d4c4683ecc3e5cf9765bb08c6d88b4c 100644 (file)
@@ -57,21 +57,59 @@ int main ( int argc, char **argv )
     vector<vector<Point> > contours;
     float maxContArea;
     int   maxContIdx;
+    VideoCapture cap;
+    bool live_camera_input = false;
+    bool live_display      = false;
+    bool ocl_acc_flag      = false;
+    int  frames_to_process = 1;
+    char *outputFilename   = NULL;
+    
+    if(argc < 2) {
+      printf ("\nUsage %s: input [1|static_image_filename] opencl_acc [0|1] live_display [1|fileOutput]\n", argv[0]);
+      printf ("Live camera input, NO opencl, live display output: %s 1 0 1\n", argv[0]);
+      printf ("Live camera input, opencl ON, live display output: %s 1 1 1\n", argv[0]);
+      printf ("Static image input, opencl ON, file output: %s sample_barcode.jpg 1 image_det.png\n", argv[0]);
+      exit (0);
+    }
+
+    if(atoi(argv[1]) == 1) {
+      live_camera_input = true;
+      if(!cap.open(1)) // open the default camera
+        return -1;
+      double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video
+      cout << "Frame per seconds : " << fps << endl;
+      frames_to_process = 30000; // Maximum number of frames to process
+    } else {
+      live_camera_input = false;
+    }
+
+    ocl_acc_flag = atoi(argv[2]);
+    if(argc > 3) {
+      if(atoi(argv[3]) == 1) live_display = true;
+      else outputFilename = argv[3];
+    }
 
-    if(argc < 3) {
-       cout << endl << "Two arguments are neeeded: filename [jpg|png] and OCL acc flag [0|1]" << endl;
-       return -1;
+    if(live_display) {
+      namedWindow( "BarcodeROI", WINDOW_AUTOSIZE );// Create a window for display.
     }
-    im_rgb = imread(argv[1]);
-    bool ocl_acc_flag = atoi(argv[2]);
-    for (int jj = 0; jj < 1; jj ++)
+
+    for (int jj = 0; jj < frames_to_process; jj ++)
     {
+      if(live_camera_input) {
+        cap >> im_rgb;
+      } else {
+        im_rgb = imread(argv[1]);
+      }
+      if(im_rgb.empty()) {
+         cout << "Wrong input (" << argv[1] << ")" << endl;
+         break;
+      }
+
       ocl::setUseOpenCL(ocl_acc_flag);
       cvtColor(im_rgb,im_gray,CV_RGB2GRAY);
 
       im_gray.copyTo(img_gray);
 
-      //Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );
       Sobel( img_gray, gradX, CV_16S, 1, 0, -1, 1, 0, BORDER_DEFAULT );
       Sobel( img_gray, gradY, CV_16S, 0, 1, -1, 1, 0, BORDER_DEFAULT );
       subtract(gradX, gradY, gradient);
@@ -82,13 +120,13 @@ int main ( int argc, char **argv )
       threshold(blurredImg, threshImg, 225, 255, THRESH_BINARY);
 
       Mat elementKernel = getStructuringElement( MORPH_RECT, Size( 2*10+1, 2*3+1 ), Point(10, 3));
-      //ocl::setUseOpenCL(false);
+      ocl::setUseOpenCL(false);
       morphologyEx( threshImg, closedImg, MORPH_CLOSE, elementKernel );
       ocl::setUseOpenCL(ocl_acc_flag);
 
       erode(closedImg, img_final, UMat(), Point(-1, -1), 4);
       dilate(img_final, img_ocl, UMat(), Point(-1, -1), 4);
-      /*************************************************************************/
+      /***********************************************************************************/
       ocl::setUseOpenCL(false);
       findContours(img_ocl, contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);
 
@@ -105,11 +143,26 @@ int main ( int argc, char **argv )
       //compute the rotated bounding box of the largest contour
       RotatedRect rect = minAreaRect( Mat(contours[maxContIdx]) );
       Point2f rvertices[4];
+
       rect.points(rvertices);
-      //Draw bounding rectangle
       for (int i = 0; i < 4; i++)
         line(im_rgb, rvertices[i], rvertices[(i+1)%4], Scalar(0,255,0), 3);
-      imwrite("image_det.png", im_rgb);
+
+      if(live_display) {
+        imshow( "BarcodeROI", im_rgb );   // Show our image inside it.
+        if(!live_camera_input) {
+          cout << "Wait for input camera stroke" << endl;
+          waitKey(0);                     // Wait for keystroke
+        } else waitKey(10);
+      } else {
+        if(outputFilename) {
+          imwrite(outputFilename, im_rgb);
+        }
+        break;
+      }
+   }
+   if(live_camera_input) {
+     cap.release();
    }
    return 0;
 }