腐蚀-膨胀-FPGA

腐蚀-膨

//腐蚀-膨胀算法
module Erosion(
    clk                ,
    rst_n            ,
    matrixp11        ,
    matrixp12        ,
    matrixp13        ,
    matrixp21        ,
    matrixp22        ,
    matrixp23        ,
    matrixp31        ,
    matrixp32        ,
    matrixp33        ,
    matrix_vs        ,
    matrix_hs        ,
    matrix_en        ,
    Erosion_data    ,  
    post_vs            ,
    post_hs            ,
    post_en    
);

//************************ input and output *******************//
    
    input         clk            ;
    input          rst_n            ;
    input  [7:0]  matrixp11     ;
    input  [7:0]  matrixp12     ;
    input  [7:0]  matrixp13     ;
    input  [7:0]  matrixp21     ;
    input  [7:0]  matrixp22     ;
    input  [7:0]  matrixp23     ;
    input  [7:0]  matrixp31     ;
    input  [7:0]  matrixp32     ;
    input  [7:0]  matrixp33     ;
    input         matrix_vs        ;
    input         matrix_hs        ;
    input         matrix_en        ;
    output  [7:0] Erosion_data    ;  
    output        post_vs        ;
    output        post_hs        ;
    output        post_en        ;

//************************** main code ***********************//
/* 膨胀算法

关于算法的实现,可以用下式子来表示,即3x3像素的运算:
P = P11 & P12 & P13 & P21 & P22 & P23 & P31 & P32 & P33
在HDL中,为了通过面积去换速度,我们将上式改变如下:                             
P1 = P11 & P12 & P13
P2 = P21 & P22 & P23
P3 = P31 & P32 & P33
P =   P1 & P2  & P3

关于算法的实现,可以用下式子来表示,即3x3像素的运算:
P = P11 | P12 | P13 | P21 | P22 | P23 | P31 | P32 | P33
在HDL中,为了通过面积去换速度,我们将上式改变如下:                             
P1 = P11 | P12 | P13
P2 = P21 | P22 | P23
P3 = P31 | P32 | P33
P = P1 | P2 | P3
*/
//***********************************************************//    
    //一个时钟,第一步
    reg [7:0] row1_data;
    always@(posedge clk or negedge rst_n) begin
        if(!rst_n) 
            row1_data <= 'd0;
        else
            row1_data <= matrixp11 & matrixp12 & matrixp13;
    end
    
    reg [7:0] row2_data;
    always@(posedge clk or negedge rst_n) begin
        if(!rst_n) 
            row2_data <= 'd0;
        else
            row2_data <= matrixp21 & matrixp22 & matrixp23;
    end
    
    reg [7:0] row3_data;
    always@(posedge clk or negedge rst_n) begin
        if(!rst_n) 
            row3_data <= 'd0;
        else
            row3_data <= matrixp31 & matrixp32 & matrixp33;
    end

//第二步
    reg [7:0] Erosion_data_r;
    always@(posedge clk or negedge rst_n) begin
        if(!rst_n) 
            Erosion_data_r <= 'd0;
        else
            Erosion_data_r <= row1_data & row2_data & row3_data;
    end
    
//延迟2个时钟
    reg [1:0] post_vs_r ;
    reg [1:0] post_hs_r ;
    reg [1:0] post_en_r ;
    always@(posedge clk or negedge rst_n) begin
        if(!rst_n) begin
            post_vs_r <= 2'b00;
            post_hs_r <= 2'b00;
            post_en_r <= 2'b00;
        end
        else begin
            post_vs_r <= {post_vs_r[0],matrix_vs    };
            post_hs_r <= {post_hs_r[0],matrix_hs    };
            post_en_r <= {post_en_r[0],matrix_en    };
        end
    end
    
    
    assign post_vs = post_vs_r[1];   
    assign post_hs = post_hs_r[1];
    assign post_en = post_en_r[1];
    assign Erosion_data = post_en ? Erosion_data_r:8'd0;
    



endmodule

胀-FPGA

原文地址:https://www.cnblogs.com/wanglinwensi/p/12863956.html