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;
}

opencv4学习总结-自定义滤波

#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);
    //均值卷积
    int k = 15;
    Mat mkernel = Mat::ones(k, k, CV_32F) / (float)(k * k);
    Mat dst;
    filter2D(src, dst, -1, mkernel, Point(-1, -1), 0, BORDER_DEFAULT);
    imshow("mean filter", dst);

    //非均值滤波
    Mat robot = (Mat_<int>(2, 2) << 1, 0, 0, -1);

    Mat result;
    filter2D(src, result, -1, robot, Point(-1, -1), 0, BORDER_DEFAULT);
    imshow("robot filter", result);

    waitKey(0);
    destroyAllWindows();

    return 0;
}