均质模糊
均匀模糊是使图像平滑的最简单方法。也称为均质平滑(Homogeneous Smoothing)、均质滤波(Homogeneous Filtering)和框模糊(Box Blurring)。在该技术中,将每个像素值计算为核心(Kernel)定义的像素邻域的平均值。均匀模糊中使用的核心称为归一化框过滤器。可以根据需要为此内核定义任何大小。但是最好定义大小和宽度都为奇数的正方形内核。在以下图像中,我显示了3 x 3和5 x 5归一化框式过滤器。
3 x 3归一化框式过滤器
你必须选择合适的内核大小来定义每个像素的邻域。如果太大,图像的细微特征可能会消失,图像看起来会模糊。如果太小,则无法消除图像中的噪点。
5 x 5归一化框式过滤器
使用OpenCV对图像进行均质模糊
这就是使用OpenCV模糊/平滑图像的方式。你可以根据需要选择内核的大小。在本示例程序中,我使用了3 x 3和5 x 5内核。#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
// 读取图像
Mat image = imread("Lady with a Guitar.jpg");
// 检测失败
if (image.empty())
{
cout << "Could not open or find the image" << endl;
cin.get(); //等待键盘
return -1;
}
//使用3x3核心模糊图像
Mat image_blurred_with_3x3_kernel;
blur(image, image_blurred_with_3x3_kernel, Size(3, 3));
//使用5x5核心模糊图像
Mat image_blurred_with_5x5_kernel;
blur(image, image_blurred_with_5x5_kernel, Size(5, 5));
// 创建窗口
namedWindow("原图");
namedWindow("3x3核心模糊图像");
namedWindow("5x5核心模糊图像");
// 显示图像
imshow("原图", image);
imshow("3x3核心模糊图像", image_blurred_with_3x3_kernel);
imshow("5x5核心模糊图像", image_blurred_with_5x5_kernel);
waitKey(0); // 等待键盘
destroyAllWindows(); //毁灭窗口~
return 0;
}
复制粘贴以上代码,注意把以上的"Lady with a Guitar.jpg"换成有效的图像路径。编译运行后,效果如下图:
原图
3 x 3归一化框式过滤器
5 x 5归一化框式过滤器
解说
Mat image_blurred_with_3x3_kernel; blur(image, image_blurred_with_3x3_kernel, Size(3, 3));上面的函数使用3 x 3归一化框滤镜对原始图像执行均质平滑/模糊操作,并将平滑后的图像存储在image_blurred_with_3x3_kernel Mat对象中。 原始图像中的每个通道都是独立处理的。
Mat image_blurred_with_5x5_kernel; blur(image, image_blurred_with_5x5_kernel, Size(5, 5));上面的函数使用5 x 5归一化框滤镜对原始图像执行均质平滑/模糊操作,并将平滑后的图像存储在image_blurred_with_5x5_kernel Mat对象中。 原始图像中的每个通道都是独立处理的。


.png)

