3.1 identity矩阵
根据线性代数,行列式计算方法,很容易看出该3x3矩阵就是将周边像素影响降为权值为0。
C++:
// Apply identity filter using kernel
Mat kernel1 = (Mat_<double>(3,3) << 0, 0, 0, 0, 1, 0, 0, 0, 0);
Mat identity;
filter2D(image, identity, -1 , kernel1, Point(-1, -1), 0, 4);
Python:
# Apply identity kernel
kernel1 = np.array([[0, 0, 0],
[0, 1, 0],
[0, 0, 0]])
identity = cv2.filter2D(src=image, ddepth=-1, kernel=kernel1)
3.2 all ones 5x5矩阵
5行x5列,全1矩阵,并做一个归一化。
根据线性代数,行列式计算方法,很容易看出该3x3矩阵就是将周边像素影响权值为1,做算术平均。
C++:
// Blurred using kernel
// Initialize matrix with all ones
Mat kernel2 = Mat::ones(5,5, CV_64F);
// Normalize the elements
kernel2 = kernel2 / 25;
Mat img;
filter2D(image, img, -1 , kernel2, Point(-1, -1), 0, 4);
Python:
# Apply blurring kernel
kernel2 = np.ones((5, 5), np.float32) / 25
img = cv2.filter2D(src=image, ddepth=-1, kernel=kernel2)
3.3 blur 5x5矩阵
5x5的OpenCV内置blur矩阵模糊化过滤。
C++:
// Blurred using OpenCV C++ blur() function
Mat img_blur;
blur(image, img_blur, Size(5,5));
Python:
# Apply blur using `blur()` function
img_blur = cv2.blur(src=image, ksize=(5,5))
3.4 GaussianBlur 5x5矩阵
5x5的OpenCV内置Gaussian Blur矩阵模糊化过滤。
C++:
// Performing Gaussian Blur
Mat gaussian_blur;
GaussianBlur(image, gaussian_blur, Size(5,5), 0);
Python:
# Apply Gaussian blur
gaussian_blur = cv2.GaussianBlur(src=image, ksize=(5,5), sigmaX=0)
3.5 medianBlur 5x5矩阵
5x5的OpenCV内置median Blur矩阵模糊化过滤。
C++:
// Apply Median Blur
Mat median_blurred;
medianBlur(image, median_blurred, (5,5));
Python:
# Apply Median blur
median_blurred = cv2.medianBlur(src=image, ksize=5)
3.6 Sharpening 3x3矩阵
5x5的定制了一个Kernel3矩阵进行凸显(锐化)中间像素。
C++:
// Apply Sharpening using kernel
Mat sharp_img;
Mat kernel3 = (Mat_<double>(3,3) << 0, -1, 0, -1, 5, -1, 0, -1 ,0);
filter2D(image, sharp_img, -1 , kernel3, Point(-1, -1), 0, BORDER_DEFAULT);
Python:
# Apply sharpening using kernel
kernel3 = np.array([[0, -1, 0],
[-1, 5, -1],
[0, -1, 0]])
sharp_img = cv2.filter2D(src=image, ddepth=-1, kernel=kernel3)
3.7 bilateralFilter矩阵
对区域内的像素进行数据过滤。
C++:
// Apply bilateral filtering
Mat bilateral_filter;
bilateralFilter(image, bilateral_filter, 9, 75, 75);
Python:
# Apply Bilateral Filtering
bilateral_filter = cv2.bilateralFilter(src=image, d=9, sigmaColor=75, sigmaSpace=75)