晓灰灰 发表于 2018-10-15 09:58:00

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

1、用matlab代码,准备好把图片转化成Vivado Simulator识别的格式,即每行一个数据:
img = imread('E:\matlab\Images\2016-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,'\r\n');   end
    end
end
figure,imshow(img);
fclose(s);

2、EdgeSobel的Verilog源代码:
`timescale 1ns / 1ps

module EdgeSobel
    (
    input clk,
    input inData,
    input x,
    input y,
    output 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 shiftReg;
    wire 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 =shiftReg;
      end
    end
    endgenerate

    wire average;
    assign average =
                  (Window+Window+Window+
                     //Window+Window+Window+
                     Window+Window+Window+
                     Window+Window+Window)/9 ;

    wire signed Gx;
    wire signed Gy;
    wire Gxabs;
    wire Gyabs;
    wire G;

    assign Gx =   shiftReg
               +2*shiftReg
               +shiftReg
               -shiftReg
               -2*shiftReg
               -shiftReg;
    assign Gy =   shiftReg
               +2*shiftReg
               +shiftReg
               -shiftReg
               -2*shiftReg
               -shiftReg;               
    assign Gxabs = (Gx>0)?Gx:(-Gx);
    assign Gyabs = (Gy>0)?Gy:(-Gy);
    assign G = Gxabs+Gyabs;

    //assign outData = average;    //平滑
    assign outData = G;   //边缘检测
endmodule

3、仿真文件:tb_EdgeSobel.v
`timescale 1ns / 1ps

module tb_edgesobel;

    reg clk;
    reg inData;
    reg cnt;
    reg row;
    wire outData;
    reg image ;
    integer file_id;
    reg 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+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

4、把输出的txt文件转化成图片Matlab程序:
A=importdata('E:\matlab\Vivado\mem2image.txt');
A=A./255;
imshow(A);

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

晓灰灰 发表于 2018-10-15 10:15:21

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

Sunlife 发表于 2018-10-15 11:45:18

                   :)

zhangyukun 发表于 2018-10-16 09:40:56

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

晓灰灰 发表于 2018-10-26 10:54:22

FPGA Verilog实现基本的图像滤波处理仿真
页: [1]
查看完整版本: FPGA Verilog实现基本的图像滤波处理仿真