自适应Canny算法的C++实现[与Matlab的效果非常相近]

#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::

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值