当前位置 主页 > 网站技术 > 代码类 >

    opencv3/C++图像边缘提取方式

    栏目:代码类 时间:2019-12-10 18:09

    canny算子实现

    使用track bar 调整canny算子参数,提取到合适的图像边缘。

    #include<iostream>
    #include<opencv2/opencv.hpp>
    using namespace cv;
    
    void trackBar(int, void*);
    int s1=0,s2=0;
    Mat src, dst;
    int main()
    {
     src = imread("E:/image/image/daibola.jpg");
     if(src.empty())
     {
      printf("can not load image \n");
      return -1;
     }
     cvNamedWindow("input", CV_WINDOW_AUTOSIZE);
     imshow("input", src);
     dst = src.clone();
     cvNamedWindow("output", CV_WINDOW_AUTOSIZE);
     createTrackbar("canny1", "output", &s1, 255, trackBar);
     createTrackbar("canny2", "output", &s2, 255, trackBar);
     GaussianBlur(src,src,Size(3,3),0);
    
     waitKey();
     return 0;
    }
    
    void trackBar(int, void*)
    {
    
     Canny(src,dst,s1,s2,3);
     imshow("output", dst);
    }

    Sobel算子实现

    #include<iostream>
    #include<opencv2/opencv.hpp>
    using namespace cv;
    
    int main()
    {
     Mat src, dst;
     src = imread("E:/image/image/daibola.jpg");
    
     if(src.empty())
     {
      printf("can not load image \n");
      return -1;
     }
     dst = Mat::zeros(src.size(), src.type());
     cvNamedWindow("input", CV_WINDOW_AUTOSIZE);
     imshow("input", src); 
    
     Mat kernel = (Mat_<int>(2,2)<<0,1,-1,0);
     filter2D(src,dst,-1,kernel,Point(-1,-1),0.0);
     Mat xgrad, ygrad;
     Sobel(src,xgrad,CV_16S,1,0,3);
     Sobel(src,ygrad,CV_16S,0,1,3);
     convertScaleAbs(xgrad,xgrad);
     convertScaleAbs(ygrad,ygrad);
    
     addWeighted(xgrad,0.5,ygrad,0.5,0,dst);
     //addWeighted(dst,0.5,src,1,0,dst);
     cvNamedWindow("output", CV_WINDOW_AUTOSIZE);
     imshow("output", dst);
     waitKey();
     return 0;
    }

    以上这篇opencv3/C++图像边缘提取方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持IIS7站长之家。