opencv——边缘检测

来源:互联网 发布:js onscroll 编辑:程序博客网 时间:2024/06/06 07:06
#include<iostream>#include<opencv2/imgproc/imgproc.hpp>#include<opencv2/highgui/highgui.hpp>using namespace cv;int main(){    Mat originalimage;    originalimage = imread("1.jpg");    Mat grayimage;    cvtColor(originalimage, grayimage, CV_RGB2GRAY);    imshow("grayimage", grayimage);    Mat cannyimage;    Canny(grayimage, cannyimage, 3, 5, 3, false);//坎尼边缘检测    imshow("canny", cannyimage);    Mat sobelimage_x,sobelimage_y,abssobelimage_x,abssobelimage_y;    Sobel(grayimage, sobelimage_x, CV_16S, 1, 0, 3, 1, 0, BORDER_DEFAULT);//索贝尔边缘检测    convertScaleAbs(sobelimage_x,abssobelimage_x);    //imshow("sobelimage_x",abssobelimage_x);    Sobel(grayimage, sobelimage_y, CV_16S, 0, 1, 3, 1, 0, BORDER_DEFAULT);//索贝尔边缘检测    convertScaleAbs(sobelimage_y, abssobelimage_y);    //imshow("sobelimage_y", abssobelimage_y);    Mat sobelimage;    addWeighted(abssobelimage_x, 0.5,abssobelimage_y, 0.5, 0, sobelimage);    imshow("all sobelimage", sobelimage);    Mat laplacianimage,abslaplacianimage;    Laplacian(grayimage, laplacianimage,CV_16S, 3,1,0,BORDER_DEFAULT);//拉普拉斯算子检测    convertScaleAbs(laplacianimage, abslaplacianimage);    imshow("lalacian", abslaplacianimage);    waitKey(0);    return 0;}

1、canny函数

Canny(InputArray image,OutputArray edges, double threshold1, double threshold2, int apertureSize=3,bool L2gradient=false )

参数详解:
第一个参数,InputArray类型的image,输入图像,即源图像,填Mat类的对象即可,且需为单通道8位图像。
第二个参数,OutputArray类型的edges,输出的边缘图,需要和源图片有一样的尺寸和类型。
第三个参数,double类型的threshold1,第一个滞后性阈值。
第四个参数,double类型的threshold2,第二个滞后性阈值。
第五个参数,int类型的apertureSize,表示应用Sobel算子的孔径大小,其有默认值3。
第六个参数,bool类型的L2gradient,一个计算图像梯度幅值的标识,有默认值false。
注:这个函数阈值1和阈值2两者的小者用于边缘连接,而大者用来控制强边缘的初始段,推荐的高低阈值比在2:1到3:1之间。
2、sobel算子函数

Sobel (InputArray src, OutputArray dst,int depth,int dx,   int dy, int ksize=3,double scale=1,double delta=0,int borderType=BORDER_DEFAULT );  

参数详解:
第一个参数,InputArray 类型的src,为输入图像,填Mat类型即可。
第二个参数,OutputArray类型的dst,即目标图像,函数的输出参数,需要和源图片有一样的尺寸和类型。
第三个参数,int类型的ddepth,输出图像的深度,支持如下src.depth()和ddepth的组合:
若src.depth() = CV_8U, 取ddepth =-1/CV_16S/CV_32F/CV_64F
若src.depth() = CV_16U/CV_16S, 取ddepth =-1/CV_32F/CV_64F
若src.depth() = CV_32F, 取ddepth =-1/CV_32F/CV_64F
若src.depth() = CV_64F, 取ddepth = -1/CV_64F
第四个参数,int类型dx,x 方向上的差分阶数。
第五个参数,int类型dy,y方向上的差分阶数。
第六个参数,int类型ksize,有默认值3,表示Sobel核的大小;必须取1,3,5或7。
第七个参数,double类型的scale,计算导数值时可选的缩放因子,默认值是1,表示默认情况下是没有应用缩放的。我们可以在文档中查阅getDerivKernels的相关介绍,来得到这个参数的更多信息。
第八个参数,double类型的delta,表示在结果存入目标图(第二个参数dst)之前可选的delta值,有默认值0。
第九个参数, int类型的borderType,我们的老朋友了(万年是最后一个参数),边界模式,默认值为BORDER_DEFAULT。这个参数可以在官方文档中borderInterpolate处得到更详细的信息。
3、拉普拉斯函数

Laplacian(InputArray src,OutputArray dst, int ddepth, int ksize=1, double scale=1, double delta=0, intborderType=BORDER_DEFAULT );

参数详解:
第一个参数,InputArray类型的image,输入图像,即源图像,填Mat类的对象即可,且需为单通道8位图像。
第二个参数,OutputArray类型的edges,输出的边缘图,需要和源图片有一样的尺寸和通道数。
第三个参数,int类型的ddept,目标图像的深度。
第四个参数,int类型的ksize,用于计算二阶导数的滤波器的孔径尺寸,大小必须为正奇数,且有默认值1。
第五个参数,double类型的scale,计算拉普拉斯值的时候可选的比例因子,有默认值1。
第六个参数,double类型的delta,表示在结果存入目标图(第二个参数dst)之前可选的delta值,有默认值0。
第七个参数, int类型的borderType,边界模式,默认值为BORDER_DEFAULT。这个参数可以在官方文档中borderInterpolate()处得到更详细的信息。