您的位置:首页 > 运维架构

opencv3学习之边缘检测(Canny/Sobel/Laplacian算子)

2017-08-19 10:09 447 查看
//30.canny边缘检测

#include <opencv2/opencv.hpp>

#include <opencv2/imgproc/imgproc.hpp>

#include <opencv2/highgui/highgui.hpp>

using namespace
cv;

int main(){

    Mat src=imread("/Users/oumoemoe/Downloads/girl.png");

    Mat dst,gray,edge;

    Mat src1=src.clone();

    dst.create(src1.size(), src1.type());

    //灰度处理

    cvtColor(src1, gray,
COLOR_BGR2GRAY);

   
//降噪,因为用于边缘检测的导数对于噪声很敏感

    blur(gray, edge,
Size(3,3));

    //canny算子

   
Canny(edge, edge,
3,
9,3);//第三个和第四个参数为滞后阈值,它们中的较小值用于边缘连接,较大值用于控制较强边缘的初始段。第五个参数为Soble算子孔径大小,默认值为3

    

    dst=Scalar::all(0);

    

    src1.copyTo(dst, edge);

    imshow("2", dst);

    waitKey(0);

    return
0;

}

//31.Sobel算子

#include <opencv2/opencv.hpp>

#include <opencv2/imgproc/imgproc.hpp>

#include <opencv2/highgui/highgui.hpp>

using namespace
cv;

int main(){

    Mat grad_x,grad_y;

    Mat abs_grad_x,abs_grad_y,dst;

    Mat src=imread("/Users/oumoemoe/Downloads/girl.png");

    imshow("1", src);

    //求x方向上的梯度

    Sobel(src, grad_x,
CV_16S,
1, 0,3,1,1,BORDER_DEFAULT);//x方向上的导数默认为【xorder=1,yorder=0,ksize=3】

    convertScaleAbs(grad_x, abs_grad_x);

    <
b4ba
span style="color:#3e1e81;">imshow("x", abs_grad_x);

    //求y方向上的梯度

    Sobel(src, grad_y,
CV_16S,
0, 1,3,1,1,BORDER_DEFAULT);//y方向上的导数默认为【xorder=0,yorder=1,ksize=3】,第七个参数是缩放因子,默认值是1

    convertScaleAbs(grad_y, abs_grad_y);

    imshow("y", abs_grad_y);

    //合并梯度

    addWeighted(abs_grad_x,
0.5, abs_grad_y,
0.5, 0, dst);

    imshow("final", dst);

    waitKey(0);

    return
0;

}

//32.拉普拉斯

#include <opencv2/opencv.hpp>

#include <opencv2/imgproc/imgproc.hpp>

#include <opencv2/highgui/highgui.hpp>

using namespace
cv;

int main(){

    Mat src,src_gray,dst,abs_dst;

    src=imread("/Users/oumoemoe/Downloads/girl.png");

    imshow("1", src);

    

    GaussianBlur(src, src,
Size(3,3),
0,0,BORDER_DEFAULT);

    cvtColor(src, src_gray,
COLOR_RGB2GRAY);

    Laplacian(src_gray, dst,
CV_16S,3,1,0,BORDER_DEFAULT);

    convertScaleAbs(dst, abs_dst);

    imshow("final", abs_dst);

    waitKey(0);

    return
0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: