- 操作系统:ubuntu22.04
- OpenCV版本:OpenCV4.9
- IDE:Visual Studio Code
- 编程语言:C++11
算法描述
使用归一化的方框滤波器模糊图像。
该函数使用以下核来平滑图像:
K = 1 k s i z e . w i d t h × k s i z e . h e i g h t [ 1 1 ⋯ 1 1 1 ⋯ 1 ⋮ ⋮ ⋱ ⋮ 1 1 ⋯ 1 ] K = \frac{1}{
{ksize.width} \times {ksize.height}} \begin{bmatrix} 1 & 1 & \cdots & 1 \\ 1 & 1 & \cdots & 1 \\ \vdots & \vdots & \ddots & \vdots \\ 1 & 1 & \cdots & 1 \end{bmatrix} K=ksize.width×ksize.height1
11⋮111⋮1⋯⋯⋱⋯11⋮1
调用 blur(src, ksize, anchor, borderType) 等价于 boxFilter(src, src.type(), ksize, anchor, true, borderType)。
支持的输入矩阵数据类型为 CV_8UC1、CV_8UC3、CV_16UC1、CV_16SC1、CV_32FC1。输出图像必须与输入图像具有相同的类型、大小和通道数。
注意:
如果硬件支持,将进行最近偶数舍入;如果不支持,则进行最近值舍入。
函数文本ID是 “org.opencv.imgproc.filters.blur”
函数原型
GMat cv::gapi::blur
(
const GMat & src,
const Size & ksize,
const Point & anchor = Point(-1,-1),
int borderType = BORDER_DEFAULT,
const Scalar & borderValue = Scalar(0)
)
参数
- 参数 src: 源图像。
- 参数 ksize: 模糊内核大小。
- 参数 anchor: 锚点位置;默认值 Point(-1,-1) 表示锚点位于内核中心。
- 参数 borderType: 边界模式用于外推图像外部的像素,请参阅 cv::BorderTypes。
- 参数 borderValue: 在使用常量边界模式时使用的值。
代码示例
#include <opencv2/gapi/core.hpp> // 包含G-API核心功能
#include <opencv2/gapi/imgproc.hpp> // 包含imgproc模块,可能需要的其他G-API模块
#include <opencv2/opencv.hpp>
int main()
{
// 加载输入图像
cv::Mat src = cv::imread( "/media/dingxin/data/study/OpenCV/sources/images/Lenna.png" );
if ( src.empty() )
{
std::cerr << "Error: Image cannot be loaded!" << std::endl;
return -1;
}
// 定义均值滤波参数
cv::Size ksize( 15, 15 ); // 模糊内核大小
cv::Point anchor( -1, -1 ); // 默认锚点位置
int borderType = cv::BORDER_DEFAULT; // 边界填充模式
cv::Scalar borderValue = cv::Scalar( 0 ); // 常量边界值
// 定义G-API计算图来应用均值滤波
cv::GComputation comp( [ ksize, anchor, borderType, borderValue ]() {
cv::GMat in; // 输入:源图像
cv::GMat out = cv::gapi::blur( in, ksize, anchor, borderType, borderValue );
return cv::GComputation( cv::GIn( in ), cv::GOut( out ) );
} );
// 输出结果
cv::Mat dst;
try
{
// 执行计算图并传入实际的cv::Mat数据
comp.apply( cv::gin( src ), cv::gout( dst ) );
// 显示结果
cv::imshow( "Original Image", src );
cv::imshow( "Blurred Image", dst );
cv::waitKey( 0 ); // 等待按键事件
}
catch ( const std::exception& e )
{
std::cerr << "Exception: " << e.what() << std::endl;
return -1;
}
return 0;
}