Canny算子
Canny 的目标是找到一个最优的边缘检测算法,最优边缘检测的含义是:
好的检测- 算法能够尽可能多地标识出图像中的实际边缘。
好的定位- 标识出的边缘要尽可能与实际图像中的实际边缘尽可能接近。
最小响应- 图像中的边缘只能标识一次,并且可能存在的图像噪声不应标识为边缘
实现步骤:
- 滤波(消除噪声)
- 梯度幅度及方向角计算(增强)
- 边缘非极大值抑制
- 用滞后阈值法求解图像边缘
代码实现
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
//极大值抑制
void nonMaximumSuppression(Mat &magnitudeImage, Mat &directionImage) {
Mat checkImage = Mat(magnitudeImage.rows, magnitudeImage.cols, CV_8U);
//迭代器初始化
MatIterator_<float>itMag = magnitudeImage.begin<float>();
MatIterator_<float>itDirection = directionImage.begin<float>();
MatIterator_<unsigned char>itRet = checkImage.begin<unsigned char>();
MatIterator_<float>itEnd = magnitudeImage.end<float>();
//遍历,计算对应的方向
for ( ; itMag != itEnd ; ++itDirection,++itRet,++itMag)
{
//将方向进行划分,对每个方向进行幅值判断
const Point pos = itRet.pos();
float currentDirection = atan(*itDirection)*(180 / 3.142);
while (currentDirection<0)
{
currentDirection += 180;
*itDirection = currentDirection;
//边界线定,对相应的方向进行判断
if (currentDirection>22.5 && currentDirection <=