opencv/samples/cpp/laplace.cpp

99 lines
2.6 KiB
C++
Raw Normal View History

2016-02-15 21:37:29 +08:00
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
2010-11-28 07:15:16 +08:00
#include <ctype.h>
#include <stdio.h>
2010-11-28 07:15:16 +08:00
#include <iostream>
using namespace cv;
using namespace std;
2012-06-08 01:21:29 +08:00
static void help()
2010-12-04 16:24:25 +08:00
{
2012-06-08 01:21:29 +08:00
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"
2015-08-01 23:24:23 +08:00
"./laplace -c=<camera #, default 0> -p=<index of the frame to be decoded/captured next>\n" << endl;
2010-12-04 16:24:25 +08:00
}
enum {GAUSSIAN, BLUR, MEDIAN};
2010-11-28 07:15:16 +08:00
int sigma = 3;
int smoothType = GAUSSIAN;
2010-11-28 07:15:16 +08:00
int main( int argc, char** argv )
{
2017-07-26 13:39:53 +08:00
cv::CommandLineParser parser(argc, argv, "{ c | 0 | }{ p | | }");
help();
2018-10-31 20:48:56 +08:00
VideoCapture cap;
string camera = parser.get<string>("c");
if (camera.size() == 1 && isdigit(camera[0]))
2015-08-01 23:24:23 +08:00
cap.open(parser.get<int>("c"));
else
2018-10-31 20:48:56 +08:00
cap.open(samples::findFileOrKeep(camera));
if (!cap.isOpened())
{
2018-10-31 20:48:56 +08:00
cerr << "Can't open camera/video stream: " << camera << endl;
return 1;
}
2018-10-31 20:48:56 +08:00
cout << "Video " << parser.get<string>("c") <<
": width=" << cap.get(CAP_PROP_FRAME_WIDTH) <<
", height=" << cap.get(CAP_PROP_FRAME_HEIGHT) <<
", nframes=" << cap.get(CAP_PROP_FRAME_COUNT) << endl;
int pos = 0;
if (parser.has("p"))
{
pos = parser.get<int>("p");
}
if (!parser.check())
2010-11-28 07:15:16 +08:00
{
2018-10-31 20:48:56 +08:00
parser.printErrors();
2010-11-28 07:15:16 +08:00
return -1;
}
2018-10-31 20:48:56 +08:00
if (pos != 0)
{
cout << "seeking to frame #" << pos << endl;
if (!cap.set(CAP_PROP_POS_FRAMES, pos))
{
cerr << "ERROR: seekeing is not supported" << endl;
}
}
namedWindow("Laplacian", WINDOW_AUTOSIZE);
createTrackbar("Sigma", "Laplacian", &sigma, 15, 0);
2010-11-28 07:15:16 +08:00
Mat smoothed, laplace, result;
2012-06-08 01:21:29 +08:00
2010-11-28 07:15:16 +08:00
for(;;)
{
Mat frame;
cap >> frame;
if( frame.empty() )
break;
int ksize = (sigma*5)|1;
if(smoothType == GAUSSIAN)
2010-11-28 07:15:16 +08:00
GaussianBlur(frame, smoothed, Size(ksize, ksize), sigma, sigma);
else if(smoothType == BLUR)
2010-11-28 07:15:16 +08:00
blur(frame, smoothed, Size(ksize, ksize));
else
medianBlur(frame, smoothed, ksize);
2012-06-08 01:21:29 +08:00
2010-11-28 07:15:16 +08:00
Laplacian(smoothed, laplace, CV_16S, 5);
convertScaleAbs(laplace, result, (sigma+1)*0.25);
imshow("Laplacian", result);
char c = (char)waitKey(30);
2010-11-28 07:15:16 +08:00
if( c == ' ' )
smoothType = smoothType == GAUSSIAN ? BLUR : smoothType == BLUR ? MEDIAN : GAUSSIAN;
if( c == 'q' || c == 'Q' || c == 27 )
2010-11-28 07:15:16 +08:00
break;
}
return 0;
}