EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
大侠好,欢迎来到FPGA技术江湖,江湖偌大,相见即是缘分。大侠可以关注FPGA技术江湖,在“闯荡江湖”、"行侠仗义"栏里获取其他感兴趣的资源,或者一起煮酒言欢。
5 l; x& s/ |, q# |/ o$ |: \1 v: X9 Y7 V/ d1 m/ r
今天给大侠带来FPGA设计中用Verilog HDL实现基本的图像滤波处理仿真,话不多说,上货。
* Z) d, ?3 J* q1 V8 }3 ]
, @ C$ w f/ \7 ?6 c1、用matlab代码,准备好把图片转化成Vivado Simulator识别的格式,即每行一个数据:
) k" f( P( T+ j7 X* Y) _4 [2 e! Y# y0 O! w
9 I* n% Y: U6 y1 X z" G U; a
) p+ S ^$ ~. c; E. V
' x6 M+ l2 F( `3 s8 ~1 ?源码:; U9 X9 l. r5 s; d
/ P+ m/ S6 |; p- k' y
img = imread('E:\matlab\Images\2016-09-05-211710.jpg');
# M& H2 @8 B4 v/ z) L1 J1 @: N if size(img,3)==3
1 u7 Y7 Z8 b6 h3 Q" b9 }% N& x img = rgb2gray(img);
$ s# @6 N* }: i% J end 2 x/ H9 y$ S) ]8 f: P/ W
height = size(img, 1); 0 f6 E$ G% {. t3 |
width = size(img, 2); 3 G, M7 h+ |- s
s = fopen('image2mem.txt','wb'); %opens the output file ) ?( U6 M1 o+ g5 h" b6 j
cnt = 0; $ R% b( }/ \6 C9 g0 U, E) ^1 B
for r=1:height
, U/ |3 a- @3 S. I+ r for c=1:width 8 S+ S4 F# @' R0 F8 Z1 p
cnt = cnt + 1;
6 @: p- \/ ?* a& ^8 a" R grey=img(r,c);
7 C/ [; H& e( m' ` greyb = dec2bin(grey,8); : B5 \/ C. F, }, S7 b
Outbyte =greyb(1:8);% k5 ?# x' U6 g- i" a/ K
: T0 T2 e G, T8 u* E4 N% n
& m: \2 \, g5 M$ d. a, X
7 y0 U4 N2 O: d' j if (Outbyte(1:4) == '0000')fprintf(s,'0%X',bin2dec(Outbyte)); " Y! p9 i6 E, v2 V/ ]
else fprintf(s,'%X',bin2dec(Outbyte)); end ! t @9 F" L; @) q; R* A$ J1 i) j
if (mod(cnt,1) == 0)fprintf(s,'\r\n'); end 7 Y$ w& o( g u) z. @, X6 |# T
end , y7 W8 I R2 |/ m3 N
end % U7 o% F* r! H9 G" ?4 u
figure,imshow(img); $ M7 y, L" m$ u+ ?1 z8 _3 o, X
fclose(s);
8 G: z" s4 K: W7 B- h; X( n! U0 c% s% h9 I0 [
3 g) i- [8 X0 \' q3 v
2、EdgeSobel的Verilog源代码:
' ~& r5 C1 Y! i/ [3 E; v* N6 {( W8 H* x3 t2 I
& B1 ]& H }( B9 e. |
5 P3 l3 Q4 K' y( \ M9 l3 R
源码:& W% {4 q/ D- V$ x5 u5 \& L
`timescale 1ns/1ps
- C* x2 n; X3 `2 N* X! x* c
module 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; ( g/ e, W7 _6 |; R3 U4 s
parameter shiftRegSize=pixel_depth*((block_height-1)*frame_width+block_width);
! ?6 v6 L% L( z+ U
reg[shiftRegSize-1:0] shiftReg; wire [block_width*block_height*pixel_depth-1:0] Window; * J; s+ D6 b' L" r1 H
initial begin shiftReg=10264'b0;end always@(posedge clk)if((x<640)&&(y<480))shiftReg<={shiftReg,inData}; 7 W$ B. m3 ^ K+ t8 d
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 & s8 q3 y0 V. m+ W5 X% p! E& p# N
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 ;
+ ^7 N8 A( Y4 {. E
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; 0 B8 k, e4 J% 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; , h' S, ^, L2 {/ U! _& ~1 M
//assign outData = average; //平滑 assign outData = G[9:2]; //边缘检测 endmodule
/ E/ F! X9 ]8 V2 w {) E; F3、仿真文件:EdgeSobel_tb.v3 V, l3 [/ V+ e: J2 S
/ n# w0 v% o0 `: r
$ {7 W# @8 `* @( u- k Q1 X2 E
. ]! H1 m9 A. H* B
& u& W% K% z) E, s% [
源码:
P. c& w4 J4 I4 {% s, {`timescale 1ns / 1ps
9 V0 s% r7 U# P' t0 | j
module edgesobel_tb;
4 W( h' J% A9 v* b" n0 V; x
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;
- H; A r2 T( h4 B# e( J# c
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 , I9 Q3 u4 @5 g' m3 Q2 L) x1 e
EdgeSobel u_2 ( .clk(clk), .x(1), .y(1), .inData(inData), .outData(outData) );
+ l5 y$ D/ F9 H. _
always #1 clk = ~clk; 7 X4 ^! l) N% H1 Z
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,"\r\n"); row = row + 1; end; end end endmodule $ c/ E' g. R. E2 V5 ~9 m9 T: |
4、把输出的txt文件转化成图片Matlab程序:" I9 Y& i/ l2 G
A=importdata('E:\matlab\Vivado\mem2image.txt');
: Z* d% }6 K s- q: L A=A./255;% s4 I8 r- Z5 C- P" V8 X- {% ]
imshow(A); 1 e) b; G6 v& G- w# ?
注意这里的A是double类型的,直接进行imshow会全白,要转化到0-1:A=A./255,或者把double类型转化为整形。 " h$ V% m1 o' P+ u% D* U# V
后续会持续更新,带来Vivado、 ISE、Quartus II 、candence等安装相关设计教程,学习资源、项目资源、好文推荐等,希望大侠持续关注。
^3 t+ S% Q' ^9 {# S& S大侠们,江湖偌大,继续闯荡,愿一切安好,有缘再见!
% ?2 C* }% f* M6 j |