Last active
December 4, 2015 03:20
-
-
Save happymanx/402f521ff2dea5a72232 to your computer and use it in GitHub Desktop.
[OpenCV] Canny Detector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Theme: Canny Detector | |
IDE: Xcode 7, OSX 10.10 | |
Language: C++, OpenCV 2.4.12 | |
Date: 104/12/04 | |
Author: HappyMan | |
Blog: http://cg2010studio.wordpress.com/ | |
*/ | |
#include "opencv2/imgproc/imgproc.hpp" | |
#include "opencv2/highgui/highgui.hpp" | |
#include <stdlib.h> | |
#include <stdio.h> | |
using namespace cv; | |
/// Global variables | |
Mat src, src_gray; | |
Mat dst, detected_edges; | |
int edgeThresh = 1; | |
int lowThreshold; | |
int const max_lowThreshold = 100; | |
int ratio = 3; | |
int kernel_size = 3; | |
const char* window_name = "Happy Edge Map"; | |
/** | |
* @function CannyThreshold | |
* @brief Trackbar callback - Canny thresholds input with a ratio 1:3 | |
*/ | |
static void CannyThreshold(int, void*) | |
{ | |
/// Reduce noise with a kernel 3x3 | |
blur(src_gray, detected_edges, Size(3,3)); | |
/// Canny detector | |
Canny(detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size); | |
/// Using Canny's output as a mask, we display our result | |
dst = Scalar::all(0); | |
src.copyTo(dst, detected_edges); | |
imshow(window_name, dst); | |
} | |
/** | |
* @function main | |
*/ | |
int main(int, char** argv) | |
{ | |
/// Load an image | |
src = imread("fang.jpg", CV_LOAD_IMAGE_COLOR); | |
if(!src.data) | |
{ return -1; } | |
/// Create a matrix of the same type and size as src (for dst) | |
dst.create(src.size(), src.type()); | |
/// Convert the image to grayscale | |
cvtColor(src, src_gray, COLOR_BGR2GRAY); | |
/// Create a window | |
namedWindow(window_name, WINDOW_AUTOSIZE); | |
/// Create a Trackbar for user to enter threshold | |
createTrackbar("Min Threshold:", window_name, &lowThreshold, max_lowThreshold, CannyThreshold); | |
/// Show the image | |
CannyThreshold(0, 0); | |
/// Wait until user exit program by pressing a key | |
waitKey(0); | |
return 0; | |
} |
Author
happymanx
commented
Dec 3, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment