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

时间:2025-08-02  作者: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类型转化为整形。

审核编辑:黄飞

 

猜您喜欢


现代电子设备中,集成电路和各种电子元件的使用越来越普遍。其中,TSOP(ThinSmallOutlinePackage)封装形式因其体积小、性能优越而受到广泛青...
2025-02-24 16:10:05

在选择安防设备时,了解其参数非常重要。分辨率是关键指标,通常以像素(如720P、1080P、4K)表示,分辨率越高,画面越清晰。视场角(FOV)决定了摄像头能覆...
2014-05-16 00:00:00

在FPGA处理完数字信号之后,我们有些情况下是需要将数字信号转变为模拟信号再输出的。比如音频信号在滤波后,需要转换为声音信号进行输出。此次,我们要讲数字信号转换...
2023-08-04 09:19:00

贴片电阻,作为电子电路中很重要的元件,其阻值并非绝对精确,存在一定的误差。这个误差值,通常用百分比表示,标注在电阻本体上,例如±1%,±5%等。这意味着实际阻值...
2024-11-29 10:26:14

贴片电阻上的「E02」并不是直接表示电阻值,而是采用了一种简化的代码表示法。E代表10的幂次方,后面的数字02表示10的2次方,也就是100。因此,E02代表的...
2024-11-26 11:29:48

声光报警器是应用于安全防范领域的设备,其规格和尺寸因型号和用途而异。声光报警器的尺寸通常在15厘米到30厘米之间,具体取决于产品的设计和功能需求。大多数家用型报...
2008-08-15 00:00:00

接线排是应用于电气工程和电子设备中的连接元件,主要用于实现电路中的电气连接和分配。通常由多个端子组成,能够方便地连接不同的电缆或导线,从而确保电流的有效传输。接...
2011-05-12 00:00:00

硬盘连接器是现代计算机存储系统中不可少的一部分,主要包括SAS、SATA和M.2等类型。每种连接器都有其独特的参数和应用场景。SAS(串行附加SCSI)连接器通...
2012-05-30 00:00:00