Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/cpp/laplace.cpp
16337 views
1
#include "opencv2/videoio.hpp"
2
#include "opencv2/highgui.hpp"
3
#include "opencv2/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 -c=<camera #, default 0> -p=<index of the frame to be decoded/captured next>\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
cv::CommandLineParser parser(argc, argv, "{ c | 0 | }{ p | | }");
30
help();
31
32
if( parser.get<string>("c").size() == 1 && isdigit(parser.get<string>("c")[0]) )
33
cap.open(parser.get<int>("c"));
34
else
35
cap.open(parser.get<string>("c"));
36
if( cap.isOpened() )
37
cout << "Video " << parser.get<string>("c") <<
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( parser.has("p") )
42
{
43
int pos = parser.get<int>("p");
44
if (!parser.check())
45
{
46
parser.printErrors();
47
return -1;
48
}
49
cout << "seeking to frame #" << pos << endl;
50
cap.set(CAP_PROP_POS_FRAMES, pos);
51
}
52
53
if( !cap.isOpened() )
54
{
55
cout << "Could not initialize capturing...\n";
56
return -1;
57
}
58
59
namedWindow( "Laplacian", 0 );
60
createTrackbar( "Sigma", "Laplacian", &sigma, 15, 0 );
61
62
Mat smoothed, laplace, result;
63
64
for(;;)
65
{
66
Mat frame;
67
cap >> frame;
68
if( frame.empty() )
69
break;
70
71
int ksize = (sigma*5)|1;
72
if(smoothType == GAUSSIAN)
73
GaussianBlur(frame, smoothed, Size(ksize, ksize), sigma, sigma);
74
else if(smoothType == BLUR)
75
blur(frame, smoothed, Size(ksize, ksize));
76
else
77
medianBlur(frame, smoothed, ksize);
78
79
Laplacian(smoothed, laplace, CV_16S, 5);
80
convertScaleAbs(laplace, result, (sigma+1)*0.25);
81
imshow("Laplacian", result);
82
83
char c = (char)waitKey(30);
84
if( c == ' ' )
85
smoothType = smoothType == GAUSSIAN ? BLUR : smoothType == BLUR ? MEDIAN : GAUSSIAN;
86
if( c == 'q' || c == 'Q' || c == 27 )
87
break;
88
}
89
90
return 0;
91
}
92
93