You are on page 1of 1

Canny edge detector with live feed:

#include<iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
using namespace std;
int main()
{
VideoCapture cap(0);
if (!cap.isOpened())
{
cout << "error opening file" << endl;
return -1;
}
namedWindow("Video", CV_WINDOW_AUTOSIZE);
Mat frame, frame2;
while (1)
{
cap >> frame;
if (frame.empty())
{
cout << "no frame in video" << endl;
break;
}
cvtColor(frame, frame2, CV_BGR2GRAY);
Canny(frame2, frame2, 100, 200, 3, true);
imshow("Video", frame2);
if (waitKey(1) == 27)
{
cout << "esc key pressed" << endl;
break;
}
}
destroyAllWindows();
return 0;
}
First argument is our input image. Second and third arguments are our minVal an
d maxVal respectively. Third argument is aperture_size. It is the size of Sobel
kernel used for find image gradients. By default it is 3. Last argument is L2gra
dient which specifies the equation for finding gradient magnitude. If it is True
, it uses the equation mentioned above which is more accurate, otherwise it uses
this function: Edge_Gradient; (G) = |G_x| + |G_y|. By default, it is False.

You might also like