opencv-021-图像卷积及均值模糊

知识点

图像卷积可以看成是一个窗口区域在另外一个大的图像上移动,对每个窗口覆盖的区域都进行点乘得到的值作为中心像素点的输出值。窗口的移动是从左到右,从上到下。窗口可以理解成一个指定大小的二维矩阵,里面有预先指定的值。(注意与深度学习卷积的区别)

API

代码(c++,python)

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
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

/*
* 图像卷积及均值模糊
*/
int main() {
Mat src = imread("../images/test.png");
if (src.empty()) {
cout << "could not load image.." << endl;
}
imshow("input", src);

// 均值模糊
Mat dst;
blur(src, dst, Size(15, 15), Point(-1, -1), 4);
imshow("dst", dst);

// 3x3 均值模糊,自定义版本实现
for (int row = 1; row < h-1; row++) {
for (int col = 1; col < w-1; col++) {
Vec3b p1 = src.at<Vec3b>(row-1, col-1);
Vec3b p2 = src.at<Vec3b>(row-1, col);
Vec3b p3 = src.at<Vec3b>(row-1, col+1);
Vec3b p4 = src.at<Vec3b>(row, col-1);
Vec3b p5 = src.at<Vec3b>(row, col);
Vec3b p6 = src.at<Vec3b>(row, col+1);
Vec3b p7 = src.at<Vec3b>(row+1, col-1);
Vec3b p8 = src.at<Vec3b>(row+1, col);
Vec3b p9 = src.at<Vec3b>(row+1, col+1);

int b = p1[0] + p2[0] + p3[0] + p4[0] + p5[0] + p6[0] + p7[0] + p8[0] + p9[0];
int g = p1[1] + p2[1] + p3[1] + p4[1] + p5[1] + p6[1] + p7[1] + p8[1] + p9[1];
int r = p1[2] + p2[2] + p3[2] + p4[2] + p5[2] + p6[2] + p7[2] + p8[2] + p9[2];

dst.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(b / 9);
dst.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(g / 9);
dst.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(r / 9);
}
}

waitKey(0);
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
import cv2 as cv
import numpy as np


def custom_blur(src):
h, w, ch = src.shape
print("h , w, ch", h, w, ch)
result = np.copy(src)
for row in range(1, h-1, 1):
for col in range(1, w-1, 1):
v1 = np.int32(src[row-1, col-1])
v2 = np.int32(src[row-1, col])
v3 = np.int32(src[row-1, col+1])
v4 = np.int32(src[row, col-1])
v5 = np.int32(src[row, col])
v6 = np.int32(src[row, col+1])
v7 = np.int32(src[row+1, col-1])
v8 = np.int32(src[row+1, col])
v9 = np.int32(src[row+1, col+1])

b = v1[0] + v2[0] + v3[0] + v4[0] + v5[0] + v6[0] + v7[0] + v8[0] + v9[0];
g = v1[1] + v2[1] + v3[1] + v4[1] + v5[1] + v6[1] + v7[1] + v8[1] + v9[1];
r = v1[2] + v2[2] + v3[2] + v4[2] + v5[2] + v6[2] + v7[2] + v8[2] + v9[2];
result[row, col] = [b//9, g//9, r//9]
cv.imshow("result", result)


src = cv.imread("D:/vcprojects/images/lena.png")
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
cv.imshow("input", src)
dst = cv.blur(src, (15, 15))
cv.imshow("blur", dst)
custom_blur(src)
cv.waitKey(0)
cv.destroyAllWindows()

结果

代码地址

github