基于Verilog HDL的FPGA图像滤波处理仿真实现

时间:2025-06-15  作者:Diven  阅读:0

今天给大侠带来FPGA设计中用Verilog HDL实现基本的图像滤波处理仿真,话不多说,上货。   1、用matlab代码,准备好把图片转化成Vivado Simulator识别的格式,即每行一个数据:

基于Verilog HDL的FPGA图像滤波处理仿真实现

  代码:

 

img = imread('E:matlabImages2016-09-05-211710.jpg'); if size(img,3)==3 img = rgb2gray(img); end height = size(img, 1); width = size(img, 2); s = fopen('image2mem.txt','wb'); %opens the output file cnt = 0; for r=1:height for c=1:width cnt = cnt + 1; grey=img(r,c); greyb = dec2bin(grey,8); Outbyte =greyb(1:8); if (Outbyte(1:4) == '0000')fprintf(s,'0%X',bin2dec(Outbyte)); else fprintf(s,'%X',bin2dec(Outbyte)); end if (mod(cnt,1) == 0)fprintf(s,''); end end end figure,imshow(img); fclose(s);
    2、EdgeSobel的Verilog源代码:

 

 

代码:

 

`timescale 1ns/1psModule EdgeSobel( input clk, input [7:0] inData, input [11:0]x, input [11:0]y, output [7:0] outData ); parameter pixel_depth=8; parameter frame_width=640; parameter block_width=3; parameter block_height=3; parameter shiftRegSize=pixel_depth*((block_height-1)*frame_width+block_width); reg[shiftRegSize-1:0] shiftReg; wire [block_width*block_height*pixel_depth-1:0] Window; initial begin shiftReg=10264'b0;end always@(posedge clk)if((x<640)&&(y<480))shiftReg<={shiftReg,inData}; genvar i,j; generate for(i = 0; i < block_height; i = i + 1) begin : array for(j = 0; j < block_width; j = j + 1)     begin : vector      assign Window[pixel_depth*(i * block_width + j)+:pixel_depth] =shiftReg[pixel_depth*(i*frame_width+j)+:pixel_depth];    end  end  endgenerate  wire [7:0] average;  assign average =      (Window[7:0]+Window[15:8]+Window[23:16]+    //Window[31:24]+Window[39:32]+Window[47:40]+    Window[31:24]+Window[39:32]+Window[47:40]+    Window[55:48]+Window[63:56]+Window[71:64])/9 ;  wire signed [pixel_depth+1:0] Gx;  wire signed [pixel_depth+1:0] Gy;  wire [pixel_depth+1:0] Gxabs;  wire [pixel_depth+1:0] Gyabs;  wire [pixel_depth+1:0] G;  assign Gx =   shiftReg[pixel_depth*(0*frame_width+2)+:pixel_depth]        +2*shiftReg[pixel_depth*(1*frame_width+2)+:pixel_depth]        +  shiftReg[pixel_depth*(2*frame_width+2)+:pixel_depth]        -  shiftReg[pixel_depth*(0*frame_width+0)+:pixel_depth]        -2*shiftReg[pixel_depth*(1*frame_width+0)+:pixel_depth]        -  shiftReg[pixel_depth*(2*frame_width+0)+:pixel_depth];  assign Gy =   shiftReg[pixel_depth*(2*frame_width+0)+:pixel_depth]        +2*shiftReg[pixel_depth*(2*frame_width+1)+:pixel_depth]        +  shiftReg[pixel_depth*(2*frame_width+2)+:pixel_depth]        -  shiftReg[pixel_depth*(0*frame_width+0)+:pixel_depth]        -2*shiftReg[pixel_depth*(0*frame_width+1)+:pixel_depth]        -  shiftReg[pixel_depth*(0*frame_width+2)+:pixel_depth];     assign Gxabs = (Gx>0)?Gx-Gx); assign Gyabs = (Gy>0)?Gy-Gy); assign G = GxABS+GyABS; //assign outData = average; //平滑 assign outData = G[9:2]; //边缘检测endModule

3、仿真文件:EdgeSobel_tb.v

代码:

`timescale 1ns / 1ps

module edgesobel_tb; reg clk; reg [7:0] inData; reg [19:0] cnt; reg [9:0] row; wire [7:0] outData; reg [7:0] image [307199:0]; integer file_id; reg [4:0] frame_cnt; initial begin $readmemh("E:/matlab/Vivado/image2mem.txt", image); file_id = $fopen("E:/matlab/Vivado/mem2image.txt","w"); clk = 0; cnt = 0; row = 0; frame_cnt = 0; end EdgeSobel u_2 ( .clk(clk), .x(1), .y(1), .inData(inData), .outData(outData) ); always #1 clk = ~clk; always@(posedge clk) begin if(cnt == 307200) begin cnt = 0; row = 0; frame_cnt = frame_cnt + 1; end else inData = image[cnt]; cnt = cnt+1; if(frame_cnt==1) begin $fwrite(file_id, "%d ", outData); if(((cnt % 640)==0) &&(cnt>0)) begin $fwrite(file_id,""); row = row + 1; end; end endendmodule

4、把输出的txt文件转化成图片Matlab程序:

A=importdata('E:matlabVivadomem2image.txt');A=A./255;imshow(A);

注意这里的A是double类型的,直接进行imshow会全白,要转化到0-1:A=A./255,或者把double类型转化为整形。

审核编辑:黄飞

 

猜您喜欢

现代电子产品中,贴片电阻小巧的封装尺寸和优异的性能,应用于各种电路设计中。其中,0402封装的贴片电阻仅有1.0mm x 0.5mm的尺寸,成为了许多高密度电路...
2025-03-19 04:00:35


贴片电阻2012封装,指的是长度为2.0mm,宽度为1.2mm的矩形表面贴装电阻。它体积小巧,却在电子电路中扮演着至关重要的角色。由于其紧凑的尺寸,2012封装...
2024-11-26 11:29:12

现代工业和技术领域中,PM_19.6X10MM_TM作为重要的组件,受到了广泛的关注。它的独特尺寸和特性使其在多个行业中都有着不可替代的作用。本文将对PM_19...
2025-02-21 13:41:16

贴片电阻R020,指的是封装尺寸为0201的贴片电阻,是电子电路中很重要的基础元件。体积微小,却是重要的配件,主要用于限制电流、分压和提供特定阻值。R020的0...
2024-11-29 10:26:07

贴片电阻体积小巧,焊接牢固,拆卸时需要一定的技巧,才能避免损坏电路板或元件。以下是一些常用的贴片电阻拆卸技巧:使用热风枪:这是最常用的方法,热风枪可以均匀加热贴...
2024-11-29 10:26:10


在选择清洁用品时,了解其参数非常重要。清洁剂的成分是关键,通常分为生物酶型、酸性和碱性等,选择时需根据污渍类型和材质来决定。PH值也是一个重要参数,适合的PH值...
2010-03-28 00:00:00

固态电解电容是重要电子元件。在电路中起到存储电能作用。随着科技进步,固态电解电容的需求增加。市场上厂家众多,选择困难。本文将介绍固态电解电容厂家排名,帮助大家了...
2025-03-27 11:31:40

光敏电阻作为重要的光电元件,在各种光电检测和自动控制系统中得到了应用。作为全球知名的电子产品制造商,Samsung(三星)推出的光敏电阻因其优良的性能和稳定的品...
2024-10-22 07:15:58