edge.cpp 1.48 KB
Newer Older
1 2
#include "opencv2/core/utility.hpp"
#include "opencv2/imgproc.hpp"
3
#include "opencv2/imgcodecs.hpp"
4
#include "opencv2/highgui.hpp"
5

6
#include <stdio.h>
7 8 9 10 11 12 13 14

using namespace cv;
using namespace std;

int edgeThresh = 1;
Mat image, gray, edge, cedge;

// define a trackbar callback
15
static void onTrackbar(int, void*)
16 17 18 19 20 21
{
    blur(gray, edge, Size(3,3));

    // Run the edge detector on grayscale
    Canny(edge, edge, edgeThresh, edgeThresh*3, 3);
    cedge = Scalar::all(0);
22

23 24 25 26
    image.copyTo(cedge, edge);
    imshow("Edge map", cedge);
}

27
static void help()
28
{
29 30
    printf("\nThis sample demonstrates Canny edge detection\n"
           "Call:\n"
Dmitriy Anisimov's avatar
Dmitriy Anisimov committed
31
           "    /.edge [image_name -- Default is ../data/fruits.jpg]\n\n");
32 33
}

34
const char* keys =
35
{
Dmitriy Anisimov's avatar
Dmitriy Anisimov committed
36
    "{@image |../data/fruits.jpg|input image name}"
37 38 39 40 41 42
};

int main( int argc, const char** argv )
{
    help();

43
    CommandLineParser parser(argc, argv, keys);
44
    string filename = parser.get<string>(0);
45 46 47 48

    image = imread(filename, 1);
    if(image.empty())
    {
49 50
        printf("Cannot read image file: %s\n", filename.c_str());
        help();
51 52 53
        return -1;
    }
    cedge.create(image.size(), image.type());
54
    cvtColor(image, gray, COLOR_BGR2GRAY);
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

    // Create a window
    namedWindow("Edge map", 1);

    // create a toolbar
    createTrackbar("Canny threshold", "Edge map", &edgeThresh, 100, onTrackbar);

    // Show the image
    onTrackbar(0, 0);

    // Wait for a key stroke; the same function arranges events processing
    waitKey(0);

    return 0;
}