2013-03-15 00:10:54 +08:00
|
|
|
#include "opencv2/core/utility.hpp"
|
|
|
|
#include "opencv2/imgproc.hpp"
|
2014-07-04 22:48:15 +08:00
|
|
|
#include "opencv2/imgcodecs.hpp"
|
2013-03-15 00:10:54 +08:00
|
|
|
#include "opencv2/highgui.hpp"
|
2010-11-28 07:15:16 +08:00
|
|
|
|
2011-08-11 14:46:09 +08:00
|
|
|
#include <stdio.h>
|
2010-11-28 07:15:16 +08:00
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
int edgeThresh = 1;
|
|
|
|
Mat image, gray, edge, cedge;
|
|
|
|
|
|
|
|
// define a trackbar callback
|
2012-06-08 01:21:29 +08:00
|
|
|
static void onTrackbar(int, void*)
|
2010-11-28 07:15:16 +08:00
|
|
|
{
|
|
|
|
blur(gray, edge, Size(3,3));
|
|
|
|
|
|
|
|
// Run the edge detector on grayscale
|
|
|
|
Canny(edge, edge, edgeThresh, edgeThresh*3, 3);
|
|
|
|
cedge = Scalar::all(0);
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-11-28 07:15:16 +08:00
|
|
|
image.copyTo(cedge, edge);
|
|
|
|
imshow("Edge map", cedge);
|
|
|
|
}
|
|
|
|
|
2012-06-08 01:21:29 +08:00
|
|
|
static void help()
|
2011-08-11 14:46:09 +08:00
|
|
|
{
|
2012-06-08 01:21:29 +08:00
|
|
|
printf("\nThis sample demonstrates Canny edge detection\n"
|
|
|
|
"Call:\n"
|
2014-09-13 22:28:41 +08:00
|
|
|
" /.edge [image_name -- Default is ../data/fruits.jpg]\n\n");
|
2011-08-11 14:46:09 +08:00
|
|
|
}
|
|
|
|
|
2012-06-08 01:21:29 +08:00
|
|
|
const char* keys =
|
2010-11-28 07:15:16 +08:00
|
|
|
{
|
2015-08-01 23:24:23 +08:00
|
|
|
"{help h||}{@image |../data/fruits.jpg|input image name}"
|
2011-08-11 14:46:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
int main( int argc, const char** argv )
|
|
|
|
{
|
2012-06-08 01:21:29 +08:00
|
|
|
CommandLineParser parser(argc, argv, keys);
|
2015-08-01 23:24:23 +08:00
|
|
|
if (parser.has("help"))
|
|
|
|
{
|
|
|
|
help();
|
|
|
|
return 0;
|
|
|
|
}
|
2013-01-31 16:08:43 +08:00
|
|
|
string filename = parser.get<string>(0);
|
2010-11-28 07:15:16 +08:00
|
|
|
|
|
|
|
image = imread(filename, 1);
|
|
|
|
if(image.empty())
|
|
|
|
{
|
2012-06-08 01:21:29 +08:00
|
|
|
printf("Cannot read image file: %s\n", filename.c_str());
|
|
|
|
help();
|
2010-11-28 07:15:16 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cedge.create(image.size(), image.type());
|
2013-04-06 22:16:51 +08:00
|
|
|
cvtColor(image, gray, COLOR_BGR2GRAY);
|
2010-11-28 07:15:16 +08:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|