opencv4学习总结-图像梯度

robot算子计算梯度

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;


int main()
{
    Mat src = imread("E:/opencv_source/opencv_tutorial_data-master/images/home.jpg");
    imshow("src", src);
    
    Mat robotx = (Mat_<int>(2, 2) << 1, 0, 0, -1);
    Mat roboty = (Mat_<int>(2, 2) << 0, 1, -1, 0);
    Mat grad_x, grad_y;
    filter2D(src, grad_x, CV_32F, robotx, Point(-1, -1), 0, BORDER_DEFAULT);
    filter2D(src, grad_y, CV_32F, roboty, Point(-1, -1), 0, BORDER_DEFAULT);
    convertScaleAbs(grad_x, grad_x);
    convertScaleAbs(grad_y, grad_y);
    Mat result;
    add(grad_x, grad_y, result);
    imshow("grad_x", grad_x);
    imshow("grad_y", grad_y);
    imshow("result", result);


    waitKey(0);
    destroyAllWindows();

    return 0;
}

sobel算子

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;


int main()
{
    Mat src = imread("E:/opencv_source/opencv_tutorial_data-master/images/home.jpg");
    imshow("src", src);
    
    Mat grad_x, grad_y;

    Sobel(src, grad_x, CV_32F, 1, 0);
    Sobel(src, grad_y, CV_32F, 0, 1);
    convertScaleAbs(grad_x, grad_x);
    convertScaleAbs(grad_y, grad_y);

    Mat result;
    add(grad_x, grad_y, result);
    imshow("grad_x", grad_x);
    imshow("grad_y", grad_y);
    imshow("result", result);


    waitKey(0);
    destroyAllWindows();

    return 0;
}

scharr算子

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;


int main()
{
    Mat src = imread("E:/opencv_source/opencv_tutorial_data-master/images/home.jpg");
    imshow("src", src);
    
    Mat grad_x, grad_y;

    Scharr(src, grad_x, CV_32F, 1, 0);
    Scharr(src, grad_y, CV_32F, 0, 1);
    convertScaleAbs(grad_x, grad_x);
    convertScaleAbs(grad_y, grad_y);

    Mat result;
    add(grad_x, grad_y, result);
    imshow("grad_x", grad_x);
    imshow("grad_y", grad_y);
    imshow("result", result);


    waitKey(0);
    destroyAllWindows();

    return 0;
}