加入星計(jì)劃,您可以享受以下權(quán)益:

  • 創(chuàng)作內(nèi)容快速變現(xiàn)
  • 行業(yè)影響力擴(kuò)散
  • 作品版權(quán)保護(hù)
  • 300W+ 專(zhuān)業(yè)用戶(hù)
  • 1.5W+ 優(yōu)質(zhì)創(chuàng)作者
  • 5000+ 長(zhǎng)期合作伙伴
立即加入
  • 正文
  • 推薦器件
  • 相關(guān)推薦
  • 電子產(chǎn)業(yè)圖譜
申請(qǐng)入駐 產(chǎn)業(yè)圖譜

FPGA設(shè)計(jì)中 Verilog HDL實(shí)現(xiàn)基本的圖像濾波處理仿真

04/14 08:55
2839
閱讀需 12 分鐘
加入交流群
掃碼加入
獲取工程師必備禮包
參與熱點(diǎn)資訊討論

大俠好,歡迎來(lái)到FPGA技術(shù)江湖,江湖偌大,相見(jiàn)即是緣分。大俠可以關(guān)注FPGA技術(shù)江湖,在“闖蕩江湖”、"行俠仗義"欄里獲取其他感興趣的資源,或者一起煮酒言歡?!爸缶蒲詺g”進(jìn)入IC技術(shù)圈,這里有近100個(gè)IC技術(shù)公眾號(hào)。

今天給大俠帶來(lái)FPGA設(shè)計(jì)中用Verilog HDL實(shí)現(xiàn)基本的圖像濾波處理仿真,話(huà)不多說(shuō),上貨。

1、用matlab代碼,準(zhǔn)備好把圖片轉(zhuǎn)化成Vivado Simulator識(shí)別的格式,即每行一個(gè)數(shù)據(jù):

代碼:

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,'rn');   end    end  end  figure,imshow(img);  fclose(s);

2、EdgeSobel的Verilog源代碼

代碼:

`timescale 1ns/1ps

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;

  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];     //邊緣檢測(cè)

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,"rn");              row = row + 1;            end;        end  end

endmodule

4、把輸出的txt文件轉(zhuǎn)化成圖片Matlab程序:

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

注意這里的A是double類(lèi)型的,直接進(jìn)行imshow會(huì)全白,要轉(zhuǎn)化到0-1:A=A./255,或者把double類(lèi)型轉(zhuǎn)化為整形。

推薦器件

更多器件
器件型號(hào) 數(shù)量 器件廠(chǎng)商 器件描述 數(shù)據(jù)手冊(cè) ECAD模型 風(fēng)險(xiǎn)等級(jí) 參考價(jià)格 更多信息
EPM570T100C5N 1 Intel Corporation Flash PLD, 8.7ns, 440-Cell, CMOS, PQFP100, 16 X 16 MM, 0.50 MM PITCH, LEAD FREE, TQFP-100

ECAD模型

下載ECAD模型
$23.38 查看
10M08SCU169I7G 1 Intel Corporation Field Programmable Gate Array, 8000-Cell, CMOS, PBGA169, 11 X 11 MM, 0.80 MM PITCH, ROHS COMPLIANT, UBGA-169

ECAD模型

下載ECAD模型
$28.99 查看
XC2C384-10TQG144I 1 AMD Xilinx Flash PLD, 10ns, 384-Cell, CMOS, PQFP144, 20 X 20 MM, 0.50 MM PITCH, LEAD FREE, TQFP-144

ECAD模型

下載ECAD模型
$41.45 查看

相關(guān)推薦

電子產(chǎn)業(yè)圖譜

任何技術(shù)的學(xué)習(xí)就好比一個(gè)江湖,對(duì)于每一位俠客都需要不斷的歷練,從初入江湖的小白到歸隱山林的隱世高人,需要不斷的自我感悟自己修煉,讓我們一起仗劍闖FPGA乃至更大的江湖。