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

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

审核编辑:黄飞

 

猜您喜欢

电阻器作为基础元件,承担着限制电流、分压、偏置等重要功能。随着电子设备对性能和稳定性的要求不断提升,金属膜电阻因其优异的温度系数、低噪声和高稳定性,成为众多设计...
2013-02-23 07:05:59

当今互联网时代,网站的流量和排名是企业在线成功的关键。而排阻com作为一个新兴的SEO工具,正逐渐受到众多网站管理员和数字营销人员的青睐。本文将深入探讨排阻co...
2025-03-17 04:01:09

我们在ASIC或FPGA系统设计中,常常会遇到需要在多个时钟域下交互传输的问题,时序问题也随着系统越复杂而变得更为严重。跨时钟域处理技术是IC设计中非常重要的...
2022-08-01 18:07:00

贴片电阻上的2021并不是的阻值,而是表示的尺寸。 2021 指的是 0201 封装尺寸,表示该电阻的长和宽分别为 0.2 英寸和 0.01 英寸,换算成公制单...
2024-11-29 10:26:07

喷壶是常见的园艺工具,其作用多种多样。喷壶可以用于浇水,特别是在室内植物的护理中,细喷雾能有效避免水流过快导致土壤冲刷,保持土壤湿润而不积水。喷壶适合用于喷洒肥...
2018-07-16 00:00:00

电阻作为基础元件,其性能直接影响产品的稳定性和寿命。特别是在高湿、高温等恶劣环境下,防硫化电阻成为保障电路安全的重要选择。禾伸堂(HEC)作为知名的电子元件制造...
2021-05-17 10:01:04

电容器聚丙烯薄膜是电气行业重要材料。应用在电容器中,特别是高频和高压电源。市场上有许多厂家生产聚丙烯薄膜。本文将对这些厂家进行排名,并介绍的特点。厂家A厂家A是...
2025-03-22 01:30:34

信号继电器是重要的电气元件,应用于各个领域。在工业自动化中,信号继电器用于控制和监测设备的运行状态,确保生产流程的顺畅。在交通信号系统中,继电器负责管理信号灯的...
2013-01-21 00:00:00

功率电感是电力电子领域中不可少的元件,其主要功能是储存能量并平滑电流。不同类型的功率电感在性能和应用上存在显著区别。功率电感可分为铁氧体电感和空心电感。铁氧体电...
2010-03-25 00:00:00

测量贴片电阻时,选择合适的万用表档位很重要,这关系到测量精度和仪表安全。一般情况下,贴片电阻的阻值范围较大,从几欧姆到几兆欧姆不等。因此,没有一个固定的档位适用...
2024-11-29 10:26:08