- 操作系统:ubuntu22.04
- OpenCV版本:OpenCV4.9
- IDE:Visual Studio Code
- 编程语言:C++11
算法描述
stackBlur() 函数用于对图像进行模糊处理。该函数对图像应用了 stackBlur 技术。stackBlur 可以生成与高斯模糊相似的结果,而且随着模糊内核大小的增加,处理时间不会显著增长。它在扫描图像的过程中创建了一个移动的颜色堆栈。因此,在处理每一像素时只需要向堆栈的右侧添加一个新的颜色块,并移除最左侧的颜色。堆栈顶部的其他颜色则根据它们的位置(右侧或左侧)被增加或减少一个单位。唯一支持的边界类型是 BORDER_REPLICATE(边界复制)。原始论文由 Mario Klingemann 提出,可在此处找到:http://underdestruction.com/2004/02/25/stackblur-2004。
stackBlur 的主要优点在于它能够快速地对图像进行模糊处理,即使在较大的内核尺寸下也能保持高效的性能。这是因为它采用了特殊的算法来更新堆栈中的颜色值,而不是重新计算整个内核范围内的平均值。这种方法使得 stackBlur 成为了实时图像处理和需要大量模糊效果的应用的理想选择。
函数原型
void cv::stackBlur	
(
	InputArray 	src,
	OutputArray 	dst,
	Size 	ksize 
)		
参数
- 参数 src 输入图像。通道数可以是任意的,但是图像深度应该是 CV_8U(无符号8位整数),CV_16U(无符号16位整数),CV_16S(有符号16位整数),或者 CV_32F(32位浮点数)。
- 参数 dst 输出图像,与输入图像 src 具有相同的尺寸和类型。
- 参数 ksize stack-blur 内核的尺寸。ksize.width 和 ksize.height 可以不同,但都必须是正数且为奇数。
示例代码
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
    // Load an image
    Mat image = imread("/media/dingxin/data/study/OpenCV/sources/images/erik.jpg");
    // Check if the image is loaded successfully
    if (image.empty())
    {
        cout << "Error: Image not found." << endl;
        return -1;
    }
       
    cv::Size sz2Sh( 400, 600 );
    cv::resize( image, image, sz2Sh, 0, 0, cv::INTER_LINEAR_EXACT );
    // Set the blur kernel size
    int kernelSize = 13;  // You can adjust this value for different blurring effects
    // Perform box blur
    Mat blurredImage;
    stackBlur(image, blurredImage, Size(kernelSize, kernelSize));
    
    imshow("Original Image", image);
    imshow("Blurred Image", blurredImage);
    // Wait for a key press and then close all windows
    waitKey(0);
    destroyAllWindows();
    return 0;
}
运行结果








![[书生大模型实战营][L0][Task2] Python 开发前置知识](https://i-blog.csdnimg.cn/direct/1bde2408daaa41c888065d06d9cccc70.png#pic_center)











