laplace.cpp 2.35 KB
Newer Older
1
#include "opencv2/videoio/videoio.hpp"
2 3 4 5
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

#include <ctype.h>
6
#include <stdio.h>
7 8 9 10 11
#include <iostream>

using namespace cv;
using namespace std;

12
static void help()
Gary Bradski's avatar
Gary Bradski committed
13
{
14 15 16 17 18
    cout <<
            "\nThis program demonstrates Laplace point/edge detection using OpenCV function Laplacian()\n"
            "It captures from the camera of your choice: 0, 1, ... default 0\n"
            "Call:\n"
            "./laplace [camera #, default 0]\n" << endl;
Gary Bradski's avatar
Gary Bradski committed
19 20
}

21 22
enum {GAUSSIAN, BLUR, MEDIAN};

23
int sigma = 3;
24
int smoothType = GAUSSIAN;
25 26 27 28

int main( int argc, char** argv )
{
    VideoCapture cap;
Gary Bradski's avatar
Gary Bradski committed
29
    help();
30 31 32

    if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
        cap.open(argc == 2 ? argv[1][0] - '0' : 0);
33 34
    else if( argc >= 2 )
    {
35
        cap.open(argv[1]);
36 37
        if( cap.isOpened() )
            cout << "Video " << argv[1] <<
38 39 40
                ": width=" << cap.get(CAP_PROP_FRAME_WIDTH) <<
                ", height=" << cap.get(CAP_PROP_FRAME_HEIGHT) <<
                ", nframes=" << cap.get(CAP_PROP_FRAME_COUNT) << endl;
41 42 43 44 45
        if( argc > 2 && isdigit(argv[2][0]) )
        {
            int pos;
            sscanf(argv[2], "%d", &pos);
            cout << "seeking to frame #" << pos << endl;
46
            cap.set(CAP_PROP_POS_FRAMES, pos);
47 48
        }
    }
49 50 51 52 53 54 55 56 57 58 59

    if( !cap.isOpened() )
    {
        cout << "Could not initialize capturing...\n";
        return -1;
    }

    namedWindow( "Laplacian", 0 );
    createTrackbar( "Sigma", "Laplacian", &sigma, 15, 0 );

    Mat smoothed, laplace, result;
60

61 62 63 64 65 66 67 68
    for(;;)
    {
        Mat frame;
        cap >> frame;
        if( frame.empty() )
            break;

        int ksize = (sigma*5)|1;
69
        if(smoothType == GAUSSIAN)
70
            GaussianBlur(frame, smoothed, Size(ksize, ksize), sigma, sigma);
71
        else if(smoothType == BLUR)
72 73 74
            blur(frame, smoothed, Size(ksize, ksize));
        else
            medianBlur(frame, smoothed, ksize);
75

76 77 78 79 80 81
        Laplacian(smoothed, laplace, CV_16S, 5);
        convertScaleAbs(laplace, result, (sigma+1)*0.25);
        imshow("Laplacian", result);

        int c = waitKey(30);
        if( c == ' ' )
82
            smoothType = smoothType == GAUSSIAN ? BLUR : smoothType == BLUR ? MEDIAN : GAUSSIAN;
83 84 85 86 87 88
        if( c == 'q' || c == 'Q' || (c & 255) == 27 )
            break;
    }

    return 0;
}