Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/cpp/edge.cpp
16337 views
1
#include "opencv2/core/utility.hpp"
2
#include "opencv2/imgproc.hpp"
3
#include "opencv2/imgcodecs.hpp"
4
#include "opencv2/highgui.hpp"
5
6
#include <stdio.h>
7
8
using namespace cv;
9
using namespace std;
10
11
int edgeThresh = 1;
12
int edgeThreshScharr=1;
13
14
Mat image, gray, blurImage, edge1, edge2, cedge;
15
16
const char* window_name1 = "Edge map : Canny default (Sobel gradient)";
17
const char* window_name2 = "Edge map : Canny with custom gradient (Scharr)";
18
19
// define a trackbar callback
20
static void onTrackbar(int, void*)
21
{
22
blur(gray, blurImage, Size(3,3));
23
24
// Run the edge detector on grayscale
25
Canny(blurImage, edge1, edgeThresh, edgeThresh*3, 3);
26
cedge = Scalar::all(0);
27
28
image.copyTo(cedge, edge1);
29
imshow(window_name1, cedge);
30
31
/// Canny detector with scharr
32
Mat dx,dy;
33
Scharr(blurImage,dx,CV_16S,1,0);
34
Scharr(blurImage,dy,CV_16S,0,1);
35
Canny( dx,dy, edge2, edgeThreshScharr, edgeThreshScharr*3 );
36
/// Using Canny's output as a mask, we display our result
37
cedge = Scalar::all(0);
38
image.copyTo(cedge, edge2);
39
imshow(window_name2, cedge);
40
}
41
42
static void help()
43
{
44
printf("\nThis sample demonstrates Canny edge detection\n"
45
"Call:\n"
46
" /.edge [image_name -- Default is ../data/fruits.jpg]\n\n");
47
}
48
49
const char* keys =
50
{
51
"{help h||}{@image |../data/fruits.jpg|input image name}"
52
};
53
54
int main( int argc, const char** argv )
55
{
56
help();
57
CommandLineParser parser(argc, argv, keys);
58
string filename = parser.get<string>(0);
59
60
image = imread(filename, IMREAD_COLOR);
61
if(image.empty())
62
{
63
printf("Cannot read image file: %s\n", filename.c_str());
64
help();
65
return -1;
66
}
67
cedge.create(image.size(), image.type());
68
cvtColor(image, gray, COLOR_BGR2GRAY);
69
70
// Create a window
71
namedWindow(window_name1, 1);
72
namedWindow(window_name2, 1);
73
74
// create a toolbar
75
createTrackbar("Canny threshold default", window_name1, &edgeThresh, 100, onTrackbar);
76
createTrackbar("Canny threshold Scharr", window_name2, &edgeThreshScharr, 400, onTrackbar);
77
78
// Show the image
79
onTrackbar(0, 0);
80
81
// Wait for a key stroke; the same function arranges events processing
82
waitKey(0);
83
84
return 0;
85
}
86
87