1. 程式人生 > >最快的高斯模糊(線性時間)Fastest Gaussian Blur (in linear time)

最快的高斯模糊(線性時間)Fastest Gaussian Blur (in linear time)

I needed really fast Gaussian blur for one of my projects. After hours of struggling and browsing the internet, I finally found the best solution.

Beginning

My solution is based on Fast image convolutions by Wojciech Jarosz. Presented ideas are very simple and I don't know who is the original author. I am going to describe it a little better and add some mathematics. To get motivated, take a glance at 

the results. I have implemented this code into Photopea under Filter - Blur - Gaussian Blur.

Definition

The convolution of two 2D functions ff and gg is defined as the volume of product of ff and "shifted" gg. The second function gg is sometimes called "weight", since it determines, how much of f

f will get into the result

The Gaussian blur of a 2D function can be defined as a convolution of that function with 2DGaussian function. Our gaussian function has an integral 1 (volume under surface) and is uniquely defined by one parameter σσ called standard deviation. We will also call it "radius" in the text below.

In our discrete finite case, we represent our 2D functions as matrices of values. We compute the volume (integral) as a sum. Gaussian function has near to zero values behind some radius, so we will use only the values rxr,ryr−r≤x≤r,−r≤y≤r. This "useful" part of weight is also called the kernel.The value of convolution at [i, j] is the weighted average, i. e. sum of function values around [i, j] multiplied by weight.

Algorithm 1

For a general discrete convolution of ff and weight function ww, we can compute the result bbas:

b[i,j]=y=iri+rx=jrj+rf[y,x]w[y,x]b[i,j]=∑y=i−ri+r∑x=j−rj+rf[y,x]∗w[y,x]

For gaussian weight, we can compute only weights around [i, j] (area of 4r24⋅r2). When our matrix has nn values, the time complexity is O(nr2)O(n⋅r2). For large radii, e. g. r=10r=10, we have to do n400n∗400 operations, which correspond to 400 loops over the whole matrix and that is ugly.

// source channel, target channel, width, height, radius
function gaussBlur_1 (scl, tcl, w, h, r) {
    var rs = Math.ceil(r * 2.57);     // significant radius
    for(var i=0; i<h; i++)
        for(var j=0; j<w; j++) {
            var val = 0, wsum = 0;
            for(var iy = i-rs; iy<i+rs+1; iy++)
                for(var ix = j-rs; ix<j+rs+1; ix++) {
                    var x = Math.min(w-1, Math.max(0, ix));
                    var y = Math.min(h-1, Math.max(0, iy));
                    var dsq = (ix-j)*(ix-j)+(iy-i)*(iy-i);
                    var wght = Math.exp( -dsq / (2*r*r) ) / (Math.PI*2*r*r);
                    val += scl[y*w+x] * wght;  wsum += wght;
                }
            tcl[i*w+j] = Math.round(val/wsum);            
        }
}

Algorithm 2

Let's introduce the box blur. It is the convolution of function ff and weight ww, but weight is constant and lies within a square (box). The nice feature of box blur is, that when you have some weight function having the same variance, it converges to gaussian blur after several passes.

In this algorithm, we will simulate the gaussian blur with 3 passes of box blur. Let's denote the half of size of square as brbr ("box radius"). The constant value of weight is 1/(2br)21/(2⋅br)2 (so the sum over the whole weight is 1). We can define box blur as:

bb[i,j]=y=ibri+brx=jbrj+brf[y,x]/(2br)2bb[i,j]=∑y=i−bri+br∑x=j−brj+brf[y,x]/(2⋅br)2

We have to convert the standard deviation of gaussian blur 

相關推薦

模糊線性時間Fastest Gaussian Blur (in linear time)

I needed really fast Gaussian blur for one of my projects. After hours of struggling and browsing the internet, I finally found the be

簡單css實現模糊毛玻璃效果

img標籤新增class  設定filter:blur (0-npx)模糊半徑 filter: url(blur.svg#blur); /* FireFox, Chrome, Opera */    

Android 實時濾鏡 模糊帶原始碼

最近在做一個這樣一個需求,一個控制元件可以實時預覽攝像頭的內容,並且對此影象進行高斯模糊處理,現在來記錄一下。  基本的實現思路 1,攝像頭實時預覽的資料會回撥給onPreviewFrame(byte[] data, Camera camera) ,通過這個獲取YU

快速模糊IIR遞迴模糊

本人是影象處理的初學者,在http://www.cnblogs.com/Imageshop/p/3145216.html博文中看到有關影象柔光的特效處理原理後自己也動手實現了一下,這裡包括一個快速高斯模糊的演算法,具體原理可以參考論文《Recursive Imp

UnityShader例項14:螢幕特效之模糊Gaussian Blur

Shader "PengLu/ImageEffect/Unlit/GaussianBlur" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} } CGINCLUDE #include "UnityCG.cginc" s

Android實現模糊也叫毛玻璃效果

/*** 高斯模糊* @param bkg* @param view*/private void blur(Bitmap bkg, View view) {float radius = 25;Bitmap overlay = Bitmap.createBitmap((int) (view.getMeasure

模糊濾波的原理與演算法

通常,影象處理軟體會提供”模糊”(blur)濾鏡,使圖片產生模糊的效果。

cocos2dx 高效能模糊包含lua介面

根據官方的帖子實現的高斯模糊當前螢幕內容  點選開啟連結 1.截圖縮小壓縮,減小畫素取樣的優化演算法。預設截圖後縮小到原來的1/4。 2.C++程式碼進行一次性高斯模糊。避免使用shader造成的渲染掉幀 以下是C++部分程式碼: /* * 高斯模糊介面 縮放因

Android簡單、高效能的模糊毛玻璃效果(附原始碼)

毛玻璃效果相信很多朋友都眼紅很久了, 隔壁ios系統對高斯模糊早就大範圍使用了, 咱們Android卻絲毫不為所動, 於是就只能靠廣大開發者咯。 這是目前市面上效能最高的方案, 也不知道最初是

洛谷P4151 [WC2011]大XOR和路徑線性

std 回來 int 沒有 moto ext pri 線性 inf 傳送門 首先看到異或就想到線性基 我們考慮有一條路徑,那麽從這條路徑走到圖中的任意一個環再走回這條路徑上,對答案的貢獻是這個環的異或和,走到這個環上的路徑對答案是沒有影響的 以這張(偷來的)圖為

20.方差/標準差/數學期望/正態分佈/函式數學篇--- OpenCV從零開始到影象人臉 + 物體識別系列

本文作者:小嗷 微信公眾號:aoxiaoji 吹比QQ群:736854977 本文你會找到以下問題的答案: 方差 標準差 數學期望 正態分佈 高斯函式 2.1 方差 方差描述隨機變數對於數學期望的偏離程度。(隨機變數可以

牛頓法求解小二乘問題線性迴歸

用牛頓法求解代價函式的最小值,這裡是n維向量,是實數。 解  牛頓法(詳細點此)迭代公式為, 這裡,是關於的偏導數向量;是一個n x n被稱作Hessian的矩陣,其元素為。 先求關於的偏導數 上式

CUDA實現影象的濾波opencv實現

高斯濾波簡介:       高斯濾波是通過對輸入陣列的每個點與輸入的高斯濾波模板執行卷積計算然後將這些結果一塊組成了濾波後的輸出陣列,通俗的講就是高斯濾波是對整幅影象進行加權平均的過程,每一個畫素點的值都由其本身和鄰域內的其他畫素值經過加權平均後得到。        高斯

機器學習-多元分佈異常檢測

的系列文章進行學習。 不過博主的部落格只寫到“第十講 資料降維” http://blog.csdn.net/abcjennifer/article/details/8002329,後面還有三講,內容比較偏應用,分別是異常檢測、大資料機器學習、photo OCR。為了學習的完整性,我將把後續三講的內容補充

快速的“模糊演算法附Android原始碼

1:高斯模糊演算法(參考:http://www.ruanyifeng.com/blog/2012/11/gaussian_blur.html) 所謂的模糊演算法就是當前畫素點和周圍畫素點進行加權均值之後的結果替換當前畫素值。因此均值模糊是最簡單的,只要將周圍的畫素點

opencv學習十一模糊

程式碼如下: # 匯入cv模組 import cv2 as cv import numpy as np # 確保在0-255之間 def clamp(pv): if pv > 255: return 255 if pv < 0: r

opencv學習模糊理論知識

理論知識: 參考連結: 對Photoshop高斯模糊濾鏡的演算法總結:http://www.cnblogs.com/hoodlum1980/archive/2008/03/03/1088567.html Python計算機視覺3:模糊,平滑,去噪:https://www.cnblogs.

SIFT2-----模糊

       SIFT演算法是在不同的尺度空間上查詢關鍵點,而尺度空間的獲取需要使用高斯模糊來實現 。 一維正態分佈公式:        (x-μ)在高斯影象模糊中對應模糊半徑,指的是模板元素到模板中心的距離。            eg: 二維模板大小為m*n,則

Glide中MultiTransformation使用,實現多種變換效果組合圓形,圓角,模糊,黑白...

Glide中MultiTransformation使用 MultiTransformation可以實現多個Transformation效果結合實現一些需求 1、例如Glide載入一張圖片,我們需要把這張

2018.12.07【LOJ114】k 大異或和線性消元

傳送門 解析: 先求一個線性基,然後高斯消元解線性空間,然後基本上就是亂搞把第 k k k