在進行數學仿真或者誤差評估時,往往認為傳感器所引入的噪聲服從正態分布(高斯白噪聲),這個時候用高斯濾波器就可以很好地消除高斯噪聲。高斯濾波也是一種線性平滑濾波,通俗地講,高斯濾波就是對整幅圖像進行加權平均的過程,每一個像素點的值,都由其本身和鄰域內的其他像素值經過加權平均后得到。高斯濾波的具體操作:用一個模板(或稱卷積、掩模)掃描圖像中的每一個像素, 用模板確定的鄰域內像素的加權平均灰度值去替代模板中心像素點的值。
高斯濾波被用作為平滑濾波器的本質原因是因為它是一個低通濾波器,而且大部份基於卷積平滑濾波器都是低通濾波器。高斯模板名字的由來是二維高斯函數,即我們熟悉的二維正態分布密度函數,如下所示:
常用的 3x3 的高斯模板如下所示:
一、MATLAB實現
偷下懶,用函數法實現一下:
clc; clear all; close all; RGB= imread('flower.bmp'); %讀取圖片 gray = rgb2gray(RGB); %灰度圖 Gauss_3x3 = fspecial('gaussian',3,2); %sigma=2的3*3高斯模板 Gauss = imfilter(gray, Gauss_3x3); %高斯濾波 subplot(2,1,1); imshow(gray); title('灰度圖'); subplot(2,1,2); imshow(Gauss); title('高斯濾波');
點擊運行,得到如下結果:
結果和理論一致,圖像變得平滑了。
二、FPGA實現
1、形成3x3矩陣
這個在前面的博客花了3篇來解釋,就不多說了,我把3x3矩陣的代碼用一個專門的 .v 文件寫好,這里直接調用即可。輸入是灰度數據,即 YCbCr格式中的 8bit Y分量,輸出是矩陣數據。耗費 1 個時鍾周期。
//========================================================================== //== matrix_3x3_8bit,生成3x3矩陣,輸入和使能需對齊,耗費1clk //========================================================================== //--------------------------------------------------- 矩陣順序 // {matrix_11, matrix_12, matrix_13} // {matrix_21, matrix_22, matrix_23} // {matrix_31, matrix_32, matrix_33} //--------------------------------------------------- 模塊例化 matrix_3x3_8bit #( .COL (480 ), .ROW (272 ) ) u_matrix_3x3_8bit ( .clk (clk ), .rst_n (rst_n ), .din_vld (Y_de ), .din (Y_data ), .matrix_11 (matrix_11 ), .matrix_12 (matrix_12 ), .matrix_13 (matrix_13 ), .matrix_21 (matrix_21 ), .matrix_22 (matrix_22 ), .matrix_23 (matrix_23 ), .matrix_31 (matrix_31 ), .matrix_32 (matrix_32 ), .matrix_33 (matrix_33 ) );
2、高斯濾波
采用流水線思想,一級一級的往下運算,共耗費 3 個時鍾周期。
//gaussian ------------------------------------------ reg [11:0] gs_1 ; reg [11:0] gs_2 ; reg [11:0] gs_3 ; reg [11:0] gs ; //========================================================================== //== 高斯濾波,耗費3clk //========================================================================== //clk1,左中右的一列值 //--------------------------------------------------- always @ (posedge clk or negedge rst_n)begin if(!rst_n)begin gs_1 <= 'd0; gs_2 <= 'd0; gs_3 <= 'd0; end else begin gs_1 <= matrix_11 + matrix_12 * 2 + matrix_13; gs_2 <= matrix_21 * 2 + matrix_22 * 4 + matrix_23 * 2; gs_3 <= matrix_31 + matrix_32 * 2 + matrix_33; end end //clk2,相加 //--------------------------------------------------- always @(posedge clk or negedge rst_n)begin if(!rst_n)begin gs <= 'd0; end else begin gs <= gs_1 + gs_2 + gs_3; end end //clk3,除以16 -> 右移4位 -> 取高8位 //--------------------------------------------------- always @(posedge clk or negedge rst_n) begin if(!rst_n) begin gaussian_data <= 'd0; end else begin gaussian_data <= gs[11:4]; end end
3、信號同步
形成矩陣耗費 1clk,高斯濾波耗費 3clk,共 4clk,因此行場和使能信號都需要延遲 4 拍。
//========================================================================== //== 信號同步 //========================================================================== always @(posedge clk or negedge rst_n) begin if(!rst_n) begin Y_de_r <= 4'b0; Y_hsync_r <= 4'b0; Y_vsync_r <= 4'b0; end else begin Y_de_r <= {Y_de_r[2:0], Y_de}; Y_hsync_r <= {Y_hsync_r[2:0], Y_hsync}; Y_vsync_r <= {Y_vsync_r[2:0], Y_vsync}; end end assign gaussian_de = Y_de_r[3]; assign gaussian_hsync = Y_hsync_r[3]; assign gaussian_vsync = Y_vsync_r[3];
三、上板驗證
灰度圖:
高斯濾波:
從結果可以看出,圖像變得更加平滑,但力度不如上篇的均值濾波,整體細節保留得非常好。
[1] OpenS Lee:FPGA開源工作室(公眾號)
[2] CrazyBingo:基於VIP_Board Mini的FPGA視頻圖像算法(HDL-VIP)開發教程-V1.6
[3] NingHechuan:FPGA圖像處理教程