Home | History | Annotate | Download | only in cpp
      1 #include "opencv2/videoio/videoio.hpp"
      2 #include "opencv2/highgui/highgui.hpp"
      3 #include "opencv2/imgproc/imgproc.hpp"
      4 
      5 #include <ctype.h>
      6 #include <stdio.h>
      7 #include <iostream>
      8 
      9 using namespace cv;
     10 using namespace std;
     11 
     12 static void help()
     13 {
     14     cout <<
     15             "\nThis program demonstrates Laplace point/edge detection using OpenCV function Laplacian()\n"
     16             "It captures from the camera of your choice: 0, 1, ... default 0\n"
     17             "Call:\n"
     18             "./laplace [camera #, default 0]\n" << endl;
     19 }
     20 
     21 enum {GAUSSIAN, BLUR, MEDIAN};
     22 
     23 int sigma = 3;
     24 int smoothType = GAUSSIAN;
     25 
     26 int main( int argc, char** argv )
     27 {
     28     VideoCapture cap;
     29     help();
     30 
     31     if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
     32         cap.open(argc == 2 ? argv[1][0] - '0' : 0);
     33     else if( argc >= 2 )
     34     {
     35         cap.open(argv[1]);
     36         if( cap.isOpened() )
     37             cout << "Video " << argv[1] <<
     38                 ": width=" << cap.get(CAP_PROP_FRAME_WIDTH) <<
     39                 ", height=" << cap.get(CAP_PROP_FRAME_HEIGHT) <<
     40                 ", nframes=" << cap.get(CAP_PROP_FRAME_COUNT) << endl;
     41         if( argc > 2 && isdigit(argv[2][0]) )
     42         {
     43             int pos;
     44             sscanf(argv[2], "%d", &pos);
     45             cout << "seeking to frame #" << pos << endl;
     46             cap.set(CAP_PROP_POS_FRAMES, pos);
     47         }
     48     }
     49 
     50     if( !cap.isOpened() )
     51     {
     52         cout << "Could not initialize capturing...\n";
     53         return -1;
     54     }
     55 
     56     namedWindow( "Laplacian", 0 );
     57     createTrackbar( "Sigma", "Laplacian", &sigma, 15, 0 );
     58 
     59     Mat smoothed, laplace, result;
     60 
     61     for(;;)
     62     {
     63         Mat frame;
     64         cap >> frame;
     65         if( frame.empty() )
     66             break;
     67 
     68         int ksize = (sigma*5)|1;
     69         if(smoothType == GAUSSIAN)
     70             GaussianBlur(frame, smoothed, Size(ksize, ksize), sigma, sigma);
     71         else if(smoothType == BLUR)
     72             blur(frame, smoothed, Size(ksize, ksize));
     73         else
     74             medianBlur(frame, smoothed, ksize);
     75 
     76         Laplacian(smoothed, laplace, CV_16S, 5);
     77         convertScaleAbs(laplace, result, (sigma+1)*0.25);
     78         imshow("Laplacian", result);
     79 
     80         int c = waitKey(30);
     81         if( c == ' ' )
     82             smoothType = smoothType == GAUSSIAN ? BLUR : smoothType == BLUR ? MEDIAN : GAUSSIAN;
     83         if( c == 'q' || c == 'Q' || (c & 255) == 27 )
     84             break;
     85     }
     86 
     87     return 0;
     88 }
     89