OpenCV  3.2.0
Open Source Computer Vision
laplace.cpp

An example using Laplace transformations for edge detection

#include <ctype.h>
#include <stdio.h>
#include <iostream>
using namespace cv;
using namespace std;
static void help()
{
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 -c=<camera #, default 0> -p=<index of the frame to be decoded/captured next>\n" << endl;
}
enum {GAUSSIAN, BLUR, MEDIAN};
int sigma = 3;
int smoothType = GAUSSIAN;
int main( int argc, char** argv )
{
cv::CommandLineParser parser(argc, argv, "{help h | | }{ c | 0 | }{ p | | }");
if ( parser.has("help") )
{
help();
return 0;
}
if( parser.get<string>("c").size() == 1 && isdigit(parser.get<string>("c")[0]) )
cap.open(parser.get<int>("c"));
else
cap.open(parser.get<string>("c"));
if( cap.isOpened() )
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;
if( parser.has("p") )
{
int pos = parser.get<int>("p");
if (!parser.check())
{
parser.printErrors();
return -1;
}
cout << "seeking to frame #" << pos << endl;
}
if( !cap.isOpened() )
{
cout << "Could not initialize capturing...\n";
return -1;
}
namedWindow( "Laplacian", 0 );
createTrackbar( "Sigma", "Laplacian", &sigma, 15, 0 );
Mat smoothed, laplace, result;
for(;;)
{
Mat frame;
cap >> frame;
if( frame.empty() )
break;
int ksize = (sigma*5)|1;
if(smoothType == GAUSSIAN)
GaussianBlur(frame, smoothed, Size(ksize, ksize), sigma, sigma);
else if(smoothType == BLUR)
blur(frame, smoothed, Size(ksize, ksize));
else
medianBlur(frame, smoothed, ksize);
Laplacian(smoothed, laplace, CV_16S, 5);
convertScaleAbs(laplace, result, (sigma+1)*0.25);
imshow("Laplacian", result);
char c = (char)waitKey(30);
if( c == ' ' )
smoothType = smoothType == GAUSSIAN ? BLUR : smoothType == BLUR ? MEDIAN : GAUSSIAN;
if( c == 'q' || c == 'Q' || c == 27 )
break;
}
return 0;
}
cv::CAP_PROP_FRAME_WIDTH
@ CAP_PROP_FRAME_WIDTH
Width of the frames in the video stream.
Definition: videoio.hpp:127
cv::CAP_PROP_FRAME_COUNT
@ CAP_PROP_FRAME_COUNT
Number of frames in the video file.
Definition: videoio.hpp:131
imgproc.hpp
cv::VideoCapture::get
virtual double get(int propId) const
Returns the specified VideoCapture property.
cv::VideoCapture
Class for video capturing from video files, image sequences or cameras.
Definition: videoio.hpp:597
cv::waitKey
int waitKey(int delay=0)
Waits for a pressed key.
cv::VideoCapture::set
virtual bool set(int propId, double value)
Sets a property in the VideoCapture.
cv::CAP_PROP_FRAME_HEIGHT
@ CAP_PROP_FRAME_HEIGHT
Height of the frames in the video stream.
Definition: videoio.hpp:128
cv::CAP_PROP_POS_FRAMES
@ CAP_PROP_POS_FRAMES
0-based index of the frame to be decoded/captured next.
Definition: videoio.hpp:125
highgui.hpp
cv::namedWindow
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
cv::convertScaleAbs
void convertScaleAbs(InputArray src, OutputArray dst, double alpha=1, double beta=0)
Scales, calculates absolute values, and converts the result to 8-bit.
cv::Size
Size2i Size
Definition: types.hpp:315
cv::medianBlur
void medianBlur(InputArray src, OutputArray dst, int ksize)
Blurs an image using the median filter.
cv::VideoCapture::isOpened
virtual bool isOpened() const
Returns true if video capturing has been initialized already.
cv::VideoCapture::open
virtual bool open(const String &filename)
Open video file or a capturing device or a IP video stream for video capturing.
cv::imshow
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
cv::GaussianBlur
void GaussianBlur(InputArray src, OutputArray dst, Size ksize, double sigmaX, double sigmaY=0, int borderType=BORDER_DEFAULT)
Blurs an image using a Gaussian filter.
cv::blur
void blur(InputArray src, OutputArray dst, Size ksize, Point anchor=Point(-1,-1), int borderType=BORDER_DEFAULT)
Blurs an image using the normalized box filter.
cv::Mat
n-dimensional dense array class
Definition: mat.hpp:741
cv::Laplacian
void Laplacian(InputArray src, OutputArray dst, int ddepth, int ksize=1, double scale=1, double delta=0, int borderType=BORDER_DEFAULT)
Calculates the Laplacian of an image.
CV_16S
#define CV_16S
Definition: interface.h:70
cv::CommandLineParser
Designed for command line parsing.
Definition: utility.hpp:735
cv::createTrackbar
int createTrackbar(const String &trackbarname, const String &winname, int *value, int count, TrackbarCallback onChange=0, void *userdata=0)
Creates a trackbar and attaches it to the specified window.
cv
Definition: affine.hpp:52
videoio.hpp