自定义线性滤波。

Robert算子:

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

using namespace std;
using namespace cv;

int threshold_value = 100, threshold_max = 255;
int threshold_type = 0, threshold_type_max = 4;
string outwindow = "threshold img";
Mat src, dst;
void mythreshold(int, void*);
int main(){
    Mat src1;
    src1 = imread("/Users/ming/Documents/test.jpg");
    resize(src1, src, Size(src1.cols/2, src1.rows/2));
    if (!src.data){
        printf("cannot load image ...");
        return -1;
    }
    namedWindow("src img", CV_WINDOW_AUTOSIZE);
    imshow("src img", src);
    
    /* Robert算子 */
    Mat robert_1, robert_2;
    Mat kernel_x = (Mat_<int>(2,2) << 1,0,0,-1); //定义135度方向的Robert算子,哪个方向度数值不一样在哪个方向效果就明显(卷积核)
    filter2D(src, robert_1, -1, kernel_x); //滤波
    imshow("robert x direction", robert_1);
    
    Mat kernel_y = (Mat_<int>(2,2) << 0,1,-1,0); //定义45度方向的Robert算子(卷积核)
    filter2D(src, robert_2, -1, kernel_y);
    imshow("robert y direction", robert_2);
    
    waitKey(0);
    return 0;
}

学习c++版opencv3.4之16-自定义线性滤波

sobel算子:

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

using namespace std;
using namespace cv;

int threshold_value = 100, threshold_max = 255;
int threshold_type = 0, threshold_type_max = 4;
string outwindow = "threshold img";
Mat src, dst;
void mythreshold(int, void*);
int main(){
    Mat src1;
    src1 = imread("/Users/ming/Documents/test.jpg");
    resize(src1, src, Size(src1.cols/2, src1.rows/2));
    if (!src.data){
        printf("cannot load image ...");
        return -1;
    }
    namedWindow("src img", CV_WINDOW_AUTOSIZE);
    imshow("src img", src);
    
    /* sobel算子 */
    Mat dst1, dst2;
    Mat kernel1 = (Mat_<int>(3,3) << -1,0,1,-2,0,2,-1,0,1); //定义水平方向的sobel算子
    filter2D(src, dst1, -1, kernel1); //滤波
    imshow("sobel horizontal direction", dst1);
    
    Mat kernel2 = (Mat_<int>(3,3) << -1,-2,-1,0,0,0,1,2,1); //定义垂直方向的Robert算子
    filter2D(src, dst2, -1, kernel2);
    imshow("sobel vertical direction", dst2);
    
    waitKey(0);
    return 0;
}

学习c++版opencv3.4之16-自定义线性滤波

以上两种算子是在某个方向上提取边缘的,拉普拉斯算子是针对整个图像提取边缘的。

拉普拉斯算子。

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

using namespace std;
using namespace cv;

int threshold_value = 100, threshold_max = 255;
int threshold_type = 0, threshold_type_max = 4;
string outwindow = "threshold img";
Mat src, dst;
void mythreshold(int, void*);
int main(){
    Mat src1;
    src1 = imread("/Users/ming/Documents/test.jpg");
    resize(src1, src, Size(src1.cols/2, src1.rows/2));
    if (!src.data){
        printf("cannot load image ...");
        return -1;
    }
    namedWindow("src img", CV_WINDOW_AUTOSIZE);
    imshow("src img", src);
    
    /* 拉普拉斯算子 */
    Mat dst1, dst2;
    Mat kernel1 = (Mat_<int>(3,3) << 0,-1,0,-1,4,-1,0,-1,0);
    filter2D(src, dst1, -1, kernel1);
    imshow("lapulas kernel", dst1);
    
    waitKey(0);
    return 0;
}

学习c++版opencv3.4之16-自定义线性滤波

 

相关文章: