知识点
图像卷积操作
图像卷积可以看成是一个窗口区域在另外一个大的图像上移动,对每个窗口覆盖的区域都进行点乘得到的值作为中心像素点的输出值。窗口的移动是从左到右,从上到下。窗口可以理解成一个指定大小的二维矩阵,里面有预先指定的值。
相关API(C++)
- blur(
InputArray src, // 输入
OutputArray dst, 输出
Size ksize, // 窗口大小
Point anchor = Point(-1,-1), // 默认值
int borderType = BORDER_DEFAULT // 默认值
)
Python版本
dst = cv.blur( src, ksize[, dst[, anchor[, borderType]]] )
python代码
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("C:/Users/qqxd/Desktop/opencvcode/images/lena.png")
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
cv.imshow("input", src)
dst = cv.blur(src, (9,9))
cv.imshow("blur", dst)
custom_blur(src)
cv.waitKey(0)
cv.destroyAllWindows()
c++代码
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int artc, char** argv) {
Mat src = imread("C:/Users/qqxd/Desktop/opencvcode/images/lena.png");
if (src.empty()) {
printf("could not load image...\n");
return -1;
}
namedWindow("input", WINDOW_AUTOSIZE);
imshow("input", src);
int h = src.rows;
int w = src.cols;
// 3x3 均值模糊,自定义版本实现
Mat dst = src.clone();
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);
}
}
imshow("blur", dst);
//imwrite("D:/result.png", dst);
// OpenCV 均值模糊
Mat result;
blur(src, result, Size(9, 9), Point(-1, -1), 4);
imshow("result", result);
waitKey(0);
return 0;
}
运行结果如下: