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;
|
2016-08-16 15:51:09 +08:00
|
|
|
int edgeThreshScharr=1;
|
|
|
|
|
|
|
|
Mat image, gray, blurImage, edge1, edge2, cedge;
|
|
|
|
|
|
|
|
const char* window_name1 = "Edge map : Canny default (Sobel gradient)";
|
|
|
|
const char* window_name2 = "Edge map : Canny with custom gradient (Scharr)";
|
2010-11-28 07:15:16 +08:00
|
|
|
|
|
|
|
// 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
|
|
|
{
|
2016-08-16 15:51:09 +08:00
|
|
|
blur(gray, blurImage, Size(3,3));
|
2010-11-28 07:15:16 +08:00
|
|
|
|
|
|
|
// Run the edge detector on grayscale
|
2016-08-16 15:51:09 +08:00
|
|
|
Canny(blurImage, edge1, edgeThresh, edgeThresh*3, 3);
|
2010-11-28 07:15:16 +08:00
|
|
|
cedge = Scalar::all(0);
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2016-08-16 15:51:09 +08:00
|
|
|
image.copyTo(cedge, edge1);
|
|
|
|
imshow(window_name1, cedge);
|
|
|
|
|
|
|
|
/// Canny detector with scharr
|
|
|
|
Mat dx,dy;
|
|
|
|
Scharr(blurImage,dx,CV_16S,1,0);
|
|
|
|
Scharr(blurImage,dy,CV_16S,0,1);
|
|
|
|
Canny( dx,dy, edge2, edgeThreshScharr, edgeThreshScharr*3 );
|
|
|
|
/// Using Canny's output as a mask, we display our result
|
|
|
|
cedge = Scalar::all(0);
|
|
|
|
image.copyTo(cedge, edge2);
|
|
|
|
imshow(window_name2, cedge);
|
2010-11-28 07:15:16 +08:00
|
|
|
}
|
|
|
|
|
2020-02-23 21:38:04 +08:00
|
|
|
static void help(const char** argv)
|
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"
|
2020-02-23 21:38:04 +08:00
|
|
|
" %s [image_name -- Default is fruits.jpg]\n\n", argv[0]);
|
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
|
|
|
{
|
2018-10-31 20:48:56 +08:00
|
|
|
"{help h||}{@image |fruits.jpg|input image name}"
|
2011-08-11 14:46:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
int main( int argc, const char** argv )
|
|
|
|
{
|
2020-02-23 21:38:04 +08:00
|
|
|
help(argv);
|
2012-06-08 01:21:29 +08:00
|
|
|
CommandLineParser parser(argc, argv, keys);
|
2013-01-31 16:08:43 +08:00
|
|
|
string filename = parser.get<string>(0);
|
2010-11-28 07:15:16 +08:00
|
|
|
|
2018-10-31 20:48:56 +08:00
|
|
|
image = imread(samples::findFile(filename), IMREAD_COLOR);
|
2010-11-28 07:15:16 +08:00
|
|
|
if(image.empty())
|
|
|
|
{
|
2012-06-08 01:21:29 +08:00
|
|
|
printf("Cannot read image file: %s\n", filename.c_str());
|
2020-02-23 21:38:04 +08:00
|
|
|
help(argv);
|
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
|
2016-08-16 15:51:09 +08:00
|
|
|
namedWindow(window_name1, 1);
|
|
|
|
namedWindow(window_name2, 1);
|
2010-11-28 07:15:16 +08:00
|
|
|
|
|
|
|
// create a toolbar
|
2016-08-16 15:51:09 +08:00
|
|
|
createTrackbar("Canny threshold default", window_name1, &edgeThresh, 100, onTrackbar);
|
|
|
|
createTrackbar("Canny threshold Scharr", window_name2, &edgeThreshScharr, 400, onTrackbar);
|
2010-11-28 07:15:16 +08:00
|
|
|
|
|
|
|
// Show the image
|
|
|
|
onTrackbar(0, 0);
|
|
|
|
|
|
|
|
// Wait for a key stroke; the same function arranges events processing
|
|
|
|
waitKey(0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|