#include <vector>
#include <queue>
#include <iostream>
#include <math.h>
#include <opencv/cv.h>
using namespace std;
#ifndef __CANNYEDGE_H__
#define __CANNYEDGE_H__
class AdaptiveCannyEdge {
typedef double Precision;
typedef vector<Precision> dtVect;
typedef vector<dtVect> dtMat;
typedef pair<int,int> Position;
typedef vector< vector<unsigned char> > lgMat;
private:
static const Precision _eps;
static const Precision PercentOfPixelsNotEdges; //Magic number
static const Precision ThresholdRatio; //Magic number
static const Precision gaussKernel[16]; //1-D Gaussian Kernel
static const Precision derivGaussKernel[16]; //1-D Derivative of Gaussian Kernel
public:
// If you do not specify THRESH, CANNYEDGE chooses low and high values automatically.
static bool CannyEdge( const IplImage* src, IplImage* & edgeImg );
static bool CannyGradient( const IplImage* src, IplImage* & gradImg );
private:
// Transform to a double precision intensity image if necessary
static bool im2single( const IplImage* src, dtMat& mat );
// Compute smoothed numerical gradient of image 'src' along x (horizontal) or y (vertical)
// direction.
static bool imfilter( const dtMat& src, const dtVect& filter, dtMat& _mat, bool isHori = true );
// Calculate gradients using a derivative of Gaussian filter
static bool smoothGradient( const dtMat& src, dtMat& dx, dtMat& dy );
// Calculate Magnitude of Gradient
static void hypot( const dtMat& dx, const dtMat& dy, dtMat& magGrad );
// Determine Hysteresis Thresholds
static void selectThresholds( const dtMat& src, double& lowThresh, double& highThresh );
// Perform Non-Maximum Suppression Thining and Hysteresis Thresholding of Edge Strength
static bool cannyFindLocalMaxima( const dtMat& src, const dtMat& dx, const dtMat& dy, const Position& pos );
// This function helps with the non-maximum suppression in the Canny edge detector.
static void thinAndThreshold( const dtMat& src, const dtMat& dx, const dtMat& dy,
const double& lowThresh, const double& highThresh, lgMat& dst );
// Return a binary image 'idxWeak' containing the objects that overlap the pixel (r,c), which
// (r,c) represents each location of 'idxStrongPts'.
static void bwselect( lgMat& idxWeak, lgMat& idxWeakNeg, const vector<Position>& idxStrongPts );
//Transform dtMat to an Image
static void dtMat2Img( const dtMat& mat, IplImage* & img );
// Transform lgMat to an Image
static void lgMat2Img( const lgMat& mat, IplImage* & img );
};
#endif
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include "cannyEdge.h"
const AdaptiveCannyEdge::Precision AdaptiveCannyEdge::_eps = 1.0e-10;
const AdaptiveCannyEdge::Precision AdaptiveCannyEdge::PercentOfPixelsNotEdges = 0.7;
const AdaptiveCannyEdge::Precision AdaptiveCannyEdge::ThresholdRatio = 0.4;
const AdaptiveCannyEdge::