图像的卷积

卷积核像一个老奶奶,眼疾手快又很有耐心的把每个像素按照周围的像素要么拔高,要么抹平。拔高就是找边缘、锐化,抹平就是模糊、中值滤波

filter2d

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main(){
    Mat img = imread("../assets/lena.jpg",IMREAD_COLOR);
    imshow("src",img);

    // 定义卷积核
    Mat kernel = (Mat_<char>(3,3)<<-1,-1,-1,
                                        -1,9,-1,
                                        -1,-1,-1);

    // 进行图像的卷积操作
    Mat dst;
    filter2D(img,dst,-1,kernel);

    imshow("dst",dst);

    waitKey(0);
    destroyAllWindows();
    return 0;
}

Sobel算子

示例代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;



int main(){
    Mat src_img = imread("../assets/zqbb.jpg",IMREAD_COLOR);
    namedWindow("src",CV_WINDOW_AUTOSIZE);
    imshow("src",src_img);


    Mat gray;
    cvtColor(src_img,gray,COLOR_BGR2GRAY);

    GaussianBlur(gray,gray,Size(3,3),0);

    // sobel
    Mat xgray,ygray;

//    Sobel(gray,xgray,CV_16S,1,0);
//    Sobel(gray,ygray,CV_16S,0,1);

    Scharr(gray,xgray,CV_16S,1,0);
    Scharr(gray,ygray,CV_16S,0,1);

    convertScaleAbs(xgray,xgray);
    convertScaleAbs(ygray,ygray);

    imshow("xgray",xgray);
    imshow("ygray",ygray);

    //将两张图片融合在一起
    const Size size = gray.size();
    int height = size.height;
    int width = size.width;

    Mat result = Mat::zeros(gray.size(),gray.type());
//
//    for(int row=0;row<height;row++){
//        for(int col=0;col<width;col++){
//            int xv = xgray.at<uchar>(row,col);
//            int yv = ygray.at<uchar>(row,col);
//            result.at<uchar>(row,col) = xv + yv;
//        }
//    }
    add(xgray,ygray,result);

    imshow("result",result);

    waitKey(0);
    destroyAllWindows();
    return 0;
}

拉普拉斯算子

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main(){
    Mat img = imread("../assets/zqbb.jpg",IMREAD_GRAYSCALE);

    Mat dst;
    Laplacian(img,dst,-1,5);

    imshow("src",img);
    imshow("dst",dst);

    waitKey(0);
    destroyAllWindows();
    return 0;
}

均值滤波

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main(){
    Mat img = imread("../assets/itheima.jpg",IMREAD_COLOR);
    imshow("src",img);

    Mat dst;
    blur(img,dst,Size(5,5));
    imshow("dst",dst);

    waitKey(0);
    destroyAllWindows();
    return 0;
}

中值滤波

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
using namespace std;
using namespace cv;

int main(){
    Mat img = imread("../assets/itheima_salt.jpg",IMREAD_COLOR);
    imshow("src salt",img);

    Mat dst;
    medianBlur(img,dst,5);

    imshow("dst",dst);

    waitKey(0);
    destroyAllWindows();
    return 0;
}

高斯滤波

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main(){
    Mat img = imread("../assets/itheima.jpg",IMREAD_COLOR);

    Mat dst;
    GaussianBlur(img,dst,Size(5,5),0);

    imshow("src",img);
    imshow("dst",dst);

    waitKey(0);
    destroyAllWindows();
    return 0;
}

Canny算法

示例代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main(){
    Mat img = imread("../assets/itheima.jpg",IMREAD_COLOR);

    Mat dst;
    Canny(img,dst,50,120);

    imshow("src",img);
    imshow("dst",dst);

    waitKey(0);
    destroyAllWindows();
    return 0;
}

双边滤波

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int sigmaColor = 0;
int sigmaSpace = 0;
// 回调函数
void changeSigma(int val,void* userdata,int index){

    switch (index){
        case 0:
            sigmaColor = val;
            break;
        case 1:
            sigmaSpace = val;
            break;
    }

    Mat src = *(Mat *)userdata;
    Mat dst;
    bilateralFilter(src,dst,10,sigmaColor,sigmaSpace);
    imshow("dst",dst);
}


int main(){
    Mat img = imread("../assets/shuaige.jpg",IMREAD_COLOR);

    imshow("src",img);
    // 创建窗口
    namedWindow("dst");
    // 创建滑动条
    createTrackbar("sigmaColor","dst",0,255,[](int val,void* userdata){ changeSigma(val,userdata,0); },&img);
    createTrackbar("sigmaSpace","dst",0,255,[](int val,void* userdata){ changeSigma(val,userdata,1); },&img);
    // 初始化图像
    changeSigma(0,&img,0);

    waitKey(0);
    destroyAllWindows();
    return 0;
}