opencv3/C++图像边缘提取

canny算子实现

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

#include
#include
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);
}

opencv3/C++图像边缘提取_第1张图片
opencv3/C++图像边缘提取_第2张图片

Sobel算子实现

#include
#include
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++图像边缘提取_第3张图片
opencv3/C++图像边缘提取_第4张图片

你可能感兴趣的:(OpenCV,opencv,sobel,canny)