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

//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);

    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;

}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值