本帖最后由 FPGA课程 于 2024-10-18 17:31 编辑
软件版本:VIVADO2021.1
操作系统:WIN10 64bit
硬件平台:适用 XILINX A7/K7/Z7/ZU/KU 系列 FPGA
实验平台:米联客-MLK-H3-CZ08-7100开发板
板卡获取平台:https://milianke.tmall.com/
登录“米联客”FPGA社区 http://www.uisrc.com 视频课程、答疑解惑!
1 图像均值滤波算法简介
均值滤波也称为线性滤波,其采用的主要方法为邻域平均法。线性滤波的基本原理是用均值代替原图像中的各 个像素值,即对待处理的当前像素点(x ,y),选择一个模板,该模板由其近邻的若干像素组成,求模板中所有像 素的均值,再把该均值赋予当前像素点(x ,y),作为处理后图像在该点上的灰度 g(x ,y),即 g(x, y) = Σ f (x, y) / m m 为该模板中包含当前像素在内的像素总个数。图像处理的模板在上一讲介绍过,3x3 或 5x5 的模板都可以用 于均值滤波。 2 设计分析2.1Matlab代码分析 源代码如下:- 源代码如下:
- clear;clear all;clc;
- image_in = imread('lena_1280x720.jpg'); % [row,col,n] = size(image_in);
- image_gray = rgb2gray(image_in);
- image_gray = im2double(image_gray); %变为双精度类型
- average_image = zeros(row,col);
- for i = 2:1:row-1
- for j = 2:1:col-1
- average_image(i,j) = (...
- image_gray(i-1,j-1)+image_gray(i-1,j)+image_gray(i-1,j+1)+... image_gray(i,j-1) +image_gray(i,j) +image_gray(i,j+1) +...
- image_gray(i+1,j-1)+image_gray(i+1,j)+image_gray(i+1,j+1))/9;%取中值 end
- end
- image_gray1 = imnoise 加入椒盐噪声
- image_gray1 = im2double 变为双精度
- average_image1 = zeros(row,col);
- for i = 2:1:row-1
- for j = 2:1:col-1
- average_image1(i,j) = (...
- image_gray1(i-1,j-1)+image_gray1(i-1,j)+image_gray1(i-1,j+1)+... image_gray1(i,j-1) +image_gray1(i,j) +image_gray1(i,j+1) +...
- image_gray1(i+1,j-1)+image_gray1(i+1,j)+image_gray1(i+1,j+1))/9;%取中值 end
- end
- image_gray2 = imnoise(image_gray,'gaussian',0.05); %加入高斯噪声 image_gray2 = im2double(image_gray2);
- average_image2 = zeros(row,col);
- for i = 2:1:row-1
- for j = 2:1:col-1
- average_image2(i,j) = (...
- image_gray2(i-1,j-1)+image_gray2(i-1,j)+image_gray2(i-1,j+1)+... image_gray2(i,j-1) +image_gray2(i,j) +image_gray2(i,j+1) +...
- image_gray2(i+1,j-1)+image_gray2(i+1,j)+image_gray2(i+1,j+1))/9;%取中值 end
- end
- figure
- subplot(321);
- imshow(image_gray ), title('the original gray image');
- subplot(322);
- imshow(average_image), title('the average image'); subplot(323);
- imshow(image_gray1), title('the salt & pepper image'); subplot(324);
- imshow(average_image1), title('the average1 image'); subplot(325);
- imshow(image_gray2), title('the gaussian image'); subplot(326);
- imshow(average_image2), title('the average2 image');
复制代码2.2Verilog代码分析 - image_template u_r_template
- (
- .i_clk (i_clk ),
- .i_rst_n (i_rst_n ),
- .i_en (i_en ),
- .i_data (i_r ),
- .o_en ( ),
- .o_temp_11 (r_temp_11 ),
- .o_temp_12 (r_temp_12 ),
- .o_temp_13 (r_temp_13 ),
- .o_temp_21 (r_temp_21 ),
- .o_temp_22 (r_temp_22 ),
- .o_temp_23 (r_temp_23 ),
- .o_temp_31 (r_temp_31 ),
- .o_temp_32 (r_temp_32 ),
- .o_temp_33 (r_temp_33 )
- );
- image_template u_g_template
- (
- .i_clk (i_clk ),
- .i_rst_n (i_rst_n ),
- .i_en (i_en ),
- .i_data (i_g ),
- .o_en ( ),
- .o_temp_11 (g_temp_11 ),
- .o_temp_12 (g_temp_12 ),
- .o_temp_13 (g_temp_13 ),
- .o_temp_21 (g_temp_21 ),
- .o_temp_22 (g_temp_22 ),
- .o_temp_23 (g_temp_23 ),
- .o_temp_31 (g_temp_31 ),
- .o_temp_32 (g_temp_32 ),
- .o_temp_33 (g_temp_33 )
- );
- image_template u_b_template
- (
- .i_clk (i_clk ),
- .i_rst_n (i_rst_n ),
- .i_en (i_en ),
- .i_data (i_b ),
- .o_en ( ),
- .o_temp_11 (b_temp_11 ),
- .o_temp_12 (b_temp_12 ),
- .o_temp_13 (b_temp_13 ),
- .o_temp_21 (b_temp_21 ),
- .o_temp_22 (b_temp_22 ),
- .o_temp_23 (b_temp_23 ),
- .o_temp_31 (b_temp_31 ),
- .o_temp_32 (b_temp_32 ),
- .o_temp_33 (b_temp_33 )
- );
- always@(posedge i_clk or negedge i_rst_n)
- begin
- if(!i_rst_n)
- begin
- sum_r <= 12'd0;
- sum_g <= 12'd0;
- sum_b <= 12'd0;
- end
- else
- begin
- sum_r <= r_temp_11 +
- r_temp_12 +
- r_temp_13 +
- r_temp_21 +
- r_temp_22 +
- r_temp_23 +
- r_temp_31 +
- r_temp_32 +
- r_temp_33;
- sum_g <= g_temp_11 +
- g_temp_12 +
- g_temp_13 +
- g_temp_21 +
- g_temp_22 +
- g_temp_23 +
- g_temp_31 +
- g_temp_32 +
- g_temp_33 ;
- sum_b <= b_temp_11 +
- b_temp_12 +
- b_temp_13 +
- b_temp_21 +
- b_temp_22 +
- b_temp_23 +
- b_temp_31 +
- b_temp_32 +
- b_temp_33 ;
- end
- end
- div_gen_0 u_r_div(
- .aclk (i_clk ),
- .s_axis_divisor_tvalid (i_en_d[1] ),
- .s_axis_divisor_tdata (8'd9 ),
- .s_axis_dividend_tvalid (i_en_d[1] ),
- .s_axis_dividend_tdata ({4'd0,sum_r[11:0]} ),
- .m_axis_dout_tvalid (avg_r_valid ),
- .m_axis_dout_tdata (avg_r )
- );
- div_gen_0 u_g_div(
- .aclk (i_clk ),
- .s_axis_divisor_tvalid (i_en_d[1] ),
- .s_axis_divisor_tdata (8'd9 ),
- .s_axis_dividend_tvalid (i_en_d[1] ),
- .s_axis_dividend_tdata ({4'd0,sum_g[11:0]} ),
- .m_axis_dout_tvalid (avg_g_valid ),
- .m_axis_dout_tdata (avg_g )
- );
- div_gen_0 u_b_div(
- .aclk (i_clk ),
- .s_axis_divisor_tvalid (i_en_d[1] ),
- .s_axis_divisor_tdata (8'd9 ),
- .s_axis_dividend_tvalid (i_en_d[1] ),
- .s_axis_dividend_tdata ({4'd0,sum_b[11:0]} ),
- .m_axis_dout_tvalid (avg_b_valid ),
- .m_axis_dout_tdata (avg_b )
- );
- endmodule
复制代码
2.3 工程结构分析 我们将图像算法的模块做成 IP 后,在vivado 中进行工程的搭建,工程结构如图所示:
3 仿真及结果3.1Matlab实验结果
3.2Modelsim实验结果
4 搭建 Vitis-sdk 工程
创建 soc_base sdk platform 和 APP 工程的过程不再重复,可以阅读 3-3-01_sdk_base_app。以下给出创建好 soc_base sdk platform 的截图和对应工程 APP 的截图。
4.1 创建 SDKPlatform工程
4.2SDKAPP工程
5 硬件连接硬件连接如图所示:
6 上板实验结果
实验结果如图所示:
|