From 42b763e6b79961df551f16cc32ee1a966289cabe Mon Sep 17 00:00:00 2001 From: LaurentBerger Date: Tue, 16 Aug 2016 09:51:09 +0200 Subject: [PATCH] Canny with own gradient (#6664) * example using the Canny algorithm with custom image gradient. * Modified example to integrate new function canny with custom gradient --- samples/cpp/edge.cpp | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/samples/cpp/edge.cpp b/samples/cpp/edge.cpp index 88abd8b257..4fbef937a1 100644 --- a/samples/cpp/edge.cpp +++ b/samples/cpp/edge.cpp @@ -9,19 +9,34 @@ using namespace cv; using namespace std; int edgeThresh = 1; -Mat image, gray, edge, cedge; +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)"; // define a trackbar callback static void onTrackbar(int, void*) { - blur(gray, edge, Size(3,3)); + blur(gray, blurImage, Size(3,3)); // Run the edge detector on grayscale - Canny(edge, edge, edgeThresh, edgeThresh*3, 3); + Canny(blurImage, edge1, edgeThresh, edgeThresh*3, 3); cedge = Scalar::all(0); - image.copyTo(cedge, edge); - imshow("Edge map", cedge); + 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); } static void help() @@ -57,10 +72,12 @@ int main( int argc, const char** argv ) cvtColor(image, gray, COLOR_BGR2GRAY); // Create a window - namedWindow("Edge map", 1); + namedWindow(window_name1, 1); + namedWindow(window_name2, 1); // create a toolbar - createTrackbar("Canny threshold", "Edge map", &edgeThresh, 100, onTrackbar); + createTrackbar("Canny threshold default", window_name1, &edgeThresh, 100, onTrackbar); + createTrackbar("Canny threshold Scharr", window_name2, &edgeThreshScharr, 400, onTrackbar); // Show the image onTrackbar(0, 0);