[opencv] 三、canny边缘检测算法应用

版权声明:本文为博主原创文章,未经博主允许禁止转载! https://blog.csdn.net/az9996/article/details/89736115

1、载入图像
2、转为灰度图
3、用blur函数进行图像模糊,降噪
4、使用canny函数进行边缘检测

定位到的canny函数源码中的参数定义

/** @brief Finds edges in an image using the Canny algorithm @cite Canny86 .

The function finds edges in the input image and marks them in the output map edges using the
Canny algorithm. The smallest value between threshold1 and threshold2 is used for edge linking. The
largest value is used to find initial segments of strong edges. See
<http://en.wikipedia.org/wiki/Canny_edge_detector>

@param image 8-bit input image. 8位输入图像
@param edges output edge map; single channels 8-bit image, which has the same size as image .单通道8位图像,与图像大小相同。
@param threshold1 first threshold for the hysteresis procedure.滞后过程的第一个阈值。
@param threshold2 second threshold for the hysteresis procedure.滞后过程的第二个阈值。
@param apertureSize aperture size for the Sobel operator.调整Sobel算子的孔径
@param L2gradient a flag, indicating whether a more accurate \f$L_2\f$ norm
\f$=\sqrt{(dI/dx)^2 + (dI/dy)^2}\f$ should be used to calculate the image gradient magnitude (
L2gradient=true ), or whether the default \f$L_1\f$ norm \f$=|dI/dx|+|dI/dy|\f$ is enough (
L2gradient=false ).
 */
CV_EXPORTS_W void Canny( InputArray image, //输入阵列 
						 OutputArray edges,	//输出阵列
                         double threshold1, //阈值1
                         double threshold2,	//阈值2
                         int apertureSize = 3, 
                         bool L2gradient = false );

/** \overload

Finds edges in an image using the Canny algorithm with custom image gradient.

实例:

void image_canny() {
	//【0】载入原始图  
	Mat srcImage = imread("1.jpg");  //工程目录下应该有一张名为1.jpg的素材图
	imshow("【原始图】Canny边缘检测", srcImage); 	//显示原始图 
	Mat dstImage, edge, grayImage;	//参数定义,效果图、边缘检测图、灰度图

	//【1】创建与src同类型和大小的矩阵(dst)
	dstImage.create(srcImage.size(), srcImage.type());

	//【2】将原图像转换为灰度图像
	//此句代码的OpenCV2版为:
	//cvtColor( srcImage, grayImage, CV_BGR2GRAY );
	//此句代码的OpenCV3版为:
	cvtColor(srcImage, grayImage, COLOR_BGR2GRAY);

	//【3】先用使用 3x3内核来降噪
	blur(grayImage, edge, Size(3, 3));

	//【4】运行Canny算子
	Canny(edge, edge, 3, 9, 3);

	//【5】显示效果图 
	imshow("【效果图】Canny边缘检测", edge);

	waitKey(0);
}

猜你喜欢

转载自blog.csdn.net/az9996/article/details/89736115