首页 > 其他分享 >第7讲 图像均值滤波实现

第7讲 图像均值滤波实现

时间:2023-05-07 14:34:26浏览次数:37  
标签:wire input output de 滤波 图像 均值 data reg

中值滤波消除椒盐噪声,高斯滤波消除高斯噪声

  1 `timescale 1ns / 1ps
  2 //////////////////////////////////////////////////////////////////////////////////
  3 // Company: 
  4 // Engineer: 
  5 // 
  6 // Create Date: 2019/03/07 10:06:43
  7 // Design Name: 
  8 // Module Name: matrix
  9 // Project Name: 
 10 // Target Devices: 
 11 // Tool Versions: 
 12 // Description: 
 13 // 
 14 // Dependencies: 
 15 // 
 16 // Revision:
 17 // Revision 0.01 - File Created
 18 // Additional Comments:
 19 // 
 20 //////////////////////////////////////////////////////////////////////////////////
 21 
 22 
 23 module matrix #(    
 24     parameter COL = 640    ,
 25     parameter ROW = 480    
 26 )
 27 (
 28     input                 clk            ,
 29     input                 rst_n        ,
 30     
 31     input               data_de        ,
 32     input         [7:0]     data        ,
 33     
 34     output                   matrix_de    ,
 35 
 36     output reg [7:0]    matrix11    ,
 37     output reg [7:0]    matrix12    ,
 38     output reg [7:0]    matrix13    ,
 39     
 40     output reg [7:0]    matrix21    ,
 41     output reg [7:0]    matrix22    ,
 42     output reg [7:0]    matrix23    ,
 43     
 44     output reg [7:0]    matrix31    ,
 45     output reg [7:0]    matrix32    ,
 46     output reg [7:0]    matrix33    
 47 );
 48 
 49 wire     [7:0]      row3_data    ;                                                                                                           
 50 wire     [7:0]      row2_data    ;
 51 wire     [7:0]      row1_data    ;
 52 
 53 reg      [15:0]    col_cnt        ;
 54 reg      [15:0]    row_cnt        ;
 55 
 56 wire            row2_rd        ;
 57 
 58 reg             data_de_r    ;
 59 reg             data_de_r1    ;
 60 
 61 wire            u1_empty    ;    
 62 wire            u2_empty    ;
 63 
 64 wire            wr_en        ;
 65 
 66 always@(posedge clk)
 67     if(!rst_n)    
 68         col_cnt <= 'd0;
 69     else if(col_cnt==COL-1)
 70         col_cnt <= 'd0;
 71     else if(data_de==1'b1)
 72         col_cnt <= col_cnt + 1'b1;
 73     else
 74         col_cnt <= col_cnt;
 75 
 76 always@(posedge clk)
 77     if(!rst_n)    
 78         row_cnt <= 'd0;
 79     else if(col_cnt==COL-1&&row_cnt==ROW-1)
 80         row_cnt <= 'd0;
 81     else if(col_cnt==COL-1)
 82         row_cnt <= row_cnt + 1'b1;
 83     else
 84         row_cnt <= row_cnt;
 85 
 86 always@(posedge clk)
 87     if(!rst_n)    
 88         data_de_r <= 1'b0;
 89     else
 90         data_de_r <= data_de;
 91 
 92 always@(posedge clk)
 93     if(!rst_n)    
 94         data_de_r1 <= 1'b0;
 95     else
 96         data_de_r1 <= data_de_r;
 97         
 98 assign  matrix_de = data_de_r1    ;    
 99 assign     row3_data = data;
100 assign  wr_en = data_de&&row_cnt<ROW-1;
101 
102 fifo_matrix_buf u1_fifo_matrix_buf (
103   .rst            (~rst_n            ),
104   .wr_clk        (clk            ), 
105   .rd_clk        (clk            ), 
106   .din            (row3_data        ),    
107   .wr_en        (wr_en            ),  
108   .rd_en        (row2_rd        ),  
109   .dout            (row2_data        ),   
110   .full            (                ),   
111   .empty        (u1_empty        )       
112 );
113 
114 fifo_matrix_buf u2_fifo_matrix_buf (
115   .rst            (~rst_n            ),
116   .wr_clk        (clk            ), 
117   .rd_clk        (clk            ), 
118   .din            (row2_data        ),    
119   .wr_en        (wr_en            ),  
120   .rd_en        (row2_rd        ),  
121   .dout            (row1_data        ),   
122   .full            (                ),   
123   .empty        (u2_empty        )       
124 );
125 
126 assign row2_rd = data_de&&row_cnt>0;
127 
128 always@(posedge clk)
129     if(!rst_n)
130         begin
131             {matrix11, matrix12, matrix13} <= 24'd0;
132             {matrix21, matrix22, matrix23} <= 24'd0;
133             {matrix31, matrix32, matrix33} <= 24'd0;
134         end
135     else if(data_de==1'b1)
136         begin
137             {matrix11, matrix12, matrix13} <= {matrix12, matrix13, row1_data};
138             {matrix21, matrix22, matrix23} <= {matrix22, matrix23, row2_data};
139             {matrix31, matrix32, matrix33} <= {matrix32, matrix33, row3_data};
140         end
141     else
142         begin
143             {matrix11, matrix12, matrix13} <= 24'd0;
144             {matrix21, matrix22, matrix23} <= 24'd0;
145             {matrix31, matrix32, matrix33} <= 24'd0;
146         end        
147 
148 
149 
150 
151 
152         
153     
154 
155 endmodule

 

 

`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date: 2021/01/13 23:18:52
// Design Name: 
// Module Name: ave_filter
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//////////////////////////////////////////////////////////////////////////////////

module ave_filter #(
    parameter COL=640,
    parameter ROW=480
) 
(
    input                 clk            ,
    input                 rst_n        ,
    input               y_vs        ,
    input               y_de        ,
    input  [7:0]         y_data        ,
    output              ave_vs        ,
    output              ave_de        ,
    output [7:0]        ave_data   
);

wire            matrix_de     ;

wire   [7:0]    matrix11     ;
wire   [7:0]    matrix12     ;
wire   [7:0]    matrix13     ;

wire   [7:0]    matrix21     ;
wire   [7:0]    matrix22     ;
wire   [7:0]    matrix23     ;

wire   [7:0]    matrix31     ;
wire   [7:0]    matrix32     ;
wire   [7:0]    matrix33     ;

reg    [3:0]    matrix_de_r  ;

reg    [15:0]   one_line     ;
reg    [15:0]   second_line  ;
reg    [15:0]   third_line   ;

reg    [15:0]   add_line     ;

reg             pre_ave_de    ;
reg    [31:0]   pre_ave_data0 ;
wire   [7:0]    pre_ave_data  ;



matrix    #(
    .COL(COL),
    .ROW(ROW)
)
u1_matrix
(
    .clk        (clk        ),
    .rst_n        (rst_n        ),
    .data_de    (y_de        ),
    .data        (y_data        ),
    
    .matrix_de    (matrix_de    ),
    .matrix11    (matrix11    ),
    .matrix12   (matrix12    ),
    .matrix13   (matrix13    ),
    .matrix21   (matrix21    ),
    .matrix22   (matrix22    ),
    .matrix23   (matrix23    ),
    .matrix31   (matrix31    ),
    .matrix32   (matrix32    ),
    .matrix33   (matrix33    )
);

always@(posedge clk or negedge rst_n)
    if(!rst_n)
        matrix_de_r     <=  4'd0;
    else
        matrix_de_r     <=  {matrix_de_r[2:0],matrix_de};
        
always@(posedge clk or negedge rst_n)
    if(!rst_n)
        one_line <= 'd0;
    else if(matrix_de==1'b1)
        one_line <= matrix11+matrix12+matrix13;
    else    
        one_line <= 'd0;

always@(posedge clk or negedge rst_n)
    if(!rst_n)
        second_line <= 'd0;
    else if(matrix_de==1'b1)
        second_line <= matrix21+matrix22+matrix23;
    else    
        second_line <='d0;                

always@(posedge clk or negedge rst_n)
    if(!rst_n)
        third_line <= 'd0;
    else if(matrix_de==1'b1)
        third_line <= matrix31+matrix32+matrix33;
    else    
        third_line <='d0;
        
always@(posedge clk or negedge rst_n)
    if(!rst_n)
        add_line <= 'd0;
    else if(matrix_de_r[0]==1'b1)
        add_line <= one_line+second_line+third_line;
    else    
        add_line <= 'd0;

always@(posedge clk or negedge rst_n)
    if(!rst_n)
        pre_ave_de <= 1'b0;
    else if(matrix_de_r[1]==1'b1)
        pre_ave_de <= 1'b1;
    else
        pre_ave_de <= 1'b0;
        
always@(posedge clk or negedge rst_n)
    if(!rst_n)
        pre_ave_data0 <= 'd0;
    else  if(matrix_de_r[1]==1'b1)
        pre_ave_data0 <= add_line*7282;
    else
        pre_ave_data0 <= 'd0;    
    
assign pre_ave_data = pre_ave_data0[23:16];
    
move_cnter    #(
    .COL(COL),
    .ROW(ROW),
    .DW (8  )
)
u1_move_cnter(
    .clk            (clk               ),
    .rst_n          (rst_n             ),
    .i_de             (pre_ave_de      ),
    .i_data            (pre_ave_data    ),
    .o_de           (ave_de            ),
    .o_data            (ave_data       )
);        

assign ave_vs = y_vs;

endmodule

 

`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date: 2019/03/29 23:06:51
// Design Name: 
// Module Name: move_cnter
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//////////////////////////////////////////////////////////////////////////////////


module move_cnter #(
    parameter COL=640,
    parameter ROW=480,
    parameter DW =24

)
(
    input                         clk            ,
    input                         rst_n          ,    
    input                         i_de         ,
    input          [DW-1:0]        i_data        ,
    
    output reg                    o_de           ,
    output reg    [DW-1:0]        o_data    
);

reg  [15:0]     col_cnt            ;
reg  [15:0]     row_cnt            ;
    
reg  [15:0]     hcnt            ;
reg  [15:0]     vcnt            ;

reg             cnt_de          ;
reg  [7:0]      cnt             ;
reg             o_de_r           ;
    
reg             o_de_r1         ;
reg  [DW-1:0]    o_data_r           ;

reg  [15:0]     data_cnt        ;

wire              o_de_r2          ;  
wire [DW-1:0]      o_data_r2        ;


always@(posedge clk or negedge rst_n)
    if(!rst_n)
        col_cnt <= 'd0;
    else if(col_cnt==COL-1)
        col_cnt <= 'd0;
    else if(i_de==1'b1)
        col_cnt <= col_cnt + 1'b1;
    else
        col_cnt <= col_cnt;
        
always@(posedge clk or negedge rst_n)
    if(!rst_n)
        row_cnt <= 'd0;
    else if(col_cnt==COL-1&&row_cnt==ROW-1)
        row_cnt <= 'd0;
    else if(col_cnt==COL-1)
        row_cnt <= row_cnt + 1'b1;
    else
        row_cnt <= row_cnt;
//去除第一行数据
always@(posedge clk or negedge rst_n)
    if(!rst_n)
        o_de_r1  <= 1'b0;
    else if(row_cnt>=1&&row_cnt<=ROW-1&&i_de)
        o_de_r1  <= 1'b1;
    else
        o_de_r1  <= 1'b0;

always@(posedge clk or negedge rst_n)
    if(!rst_n)
        o_data_r  <= 'd0;
    else if(row_cnt>=1&&row_cnt<=ROW-1&&i_de)
        o_data_r  <= i_data;
    else
        o_data_r  <= 'd0;

//一帧数据计数完            
always@(posedge clk or negedge rst_n)
    if(!rst_n)
        cnt_de <= 1'b0;
    else if(cnt=='d99)  //实际波形为160个周期
        cnt_de <= 1'b0;
    else if(col_cnt==COL-1&&row_cnt==ROW-1)
        cnt_de <= 1'b1;
    else
        cnt_de <= cnt_de;            
                
always@(posedge clk or negedge rst_n)
    if(!rst_n)
        cnt <= 'd0;
    else if(cnt=='d99)
        cnt <= 'd0;
    else if(cnt_de==1'b1)
        cnt <= cnt + 1'b1;
    else
        cnt <= cnt;        
        
always@(posedge clk or negedge rst_n) //最后一行数据补零的标志位
    if(!rst_n)
        o_de_r <= 1'b0;        
    else if(data_cnt==COL-1)
        o_de_r <= 1'b0;
    else if(cnt=='d99)
        o_de_r <= 1'b1;
    else
        o_de_r <= o_de_r;    

always@(posedge clk or negedge rst_n)
    if(!rst_n)
        data_cnt <='d0;        
    else if(data_cnt==COL-1)
        data_cnt <= 'd0;
    else if(o_de_r==1'b1)
        data_cnt <= data_cnt + 1'b1;
    else
        data_cnt <= data_cnt;    

assign o_de_r2     =       o_de_r||o_de_r1;    
assign o_data_r2   =    o_de_r1?o_data_r:'d0; 

always@(posedge clk or negedge rst_n)
    if(!rst_n)
        hcnt <= 'd0;
    else if(hcnt==COL-1)
        hcnt <= 'd0;
    else if(o_de_r2==1'b1)
        hcnt <= hcnt + 1'b1;
    else
        hcnt <= hcnt;
        
always@(posedge clk or negedge rst_n)
    if(!rst_n)
        vcnt <= 'd0;
    else if(hcnt==COL-1&&vcnt==ROW-1)
        vcnt <= 'd0;
    else if(hcnt==COL-1)
        vcnt <= vcnt + 1'b1;
    else
        vcnt <= vcnt;

always@(posedge clk or negedge rst_n)
    if(!rst_n)
        o_de <= 1'b0;
    else
        o_de <= o_de_r2; 
//去掉了第一列的数据
always@(posedge clk or negedge rst_n)
    if(!rst_n)
        o_data <= 'd0;
    else if(hcnt>0&&hcnt<COL-1&&vcnt>0&&vcnt<ROW-1)
        o_data <= o_data_r2;
    else
        o_data <= 'd0;
        
        
        
            
endmodule

 

标签:wire,input,output,de,滤波,图像,均值,data,reg
From: https://www.cnblogs.com/tuzki9611/p/17379283.html

相关文章

  • OpenAI CLIP 关键点 - 连接图像和文字
    标签:#CLIP#Image2Text#Text2Image#OpenAI创建时间:2023-04-2100:17:52基本原理CLIP是一个图像分类模型。准备训练数据:准备大量的文本描述和图片的训练对,然后把进行对比训练。文本描述和图片的训练对的数据获取:从互联网上获得400Million的图像文本数据对。这个规模......
  • UNeXt:基于 MLP 的快速医学图像分割网络
    UNeXt是约翰霍普金斯大学在2022年发布的论文。它在早期阶段使用卷积,在潜在空间阶段使用MLP。通过一个标记化的MLP块来标记和投影卷积特征,并使用MLP对表示进行建模。对输入通道进行移位,可以专注于学习局部依赖性。UNeXt完整文章:https://avoid.overfit.cn/post/addeb0eacf6......
  • m基于POCS算法的空域序列图像超分辨率重建matlab仿真
    1.算法仿真效果matlab2022a仿真结果如下:     2.算法涉及理论知识概要       随着信息处理技术和视觉通信技术的高速发展,人们获取的知识量爆炸式增长,因此迫切的要求完善的信息处理技术为人们提供更加方便、快捷服务。数字图像及及其相关技术是信息处理技......
  • Shotwell 0.32.0图像浏览器发布
    Shotwell0.32.0开源图像查看器今天发布了一个重要的更新,承诺支持更多的图像格式,以及一堆你可能已经错过的新功能和改进。Shotwell0.32.0在这里引入了对新图像格式的支持,包括AVIF、WebP、JPEG-XL、CR3,以及HEIF/HVEC的更多变体。它还引入了在图像中手动标记人物的能力,而且......
  • Shotwell 0.32.0图像浏览器发布
    Shotwell0.32.0开源图像查看器今天发布了一个重要的更新,承诺支持更多的图像格式,以及一堆你可能已经错过的新功能和改进。Shotwell0.32.0在这里引入了对新图像格式的支持,包括AVIF、WebP、JPEG-XL、CR3,以及HEIF/HVEC的更多变体。它还引入了在图像中手动标记人物的能力,而且......
  • Shotwell 0.32.0图像浏览器发布
    Shotwell0.32.0开源图像查看器今天发布了一个重要的更新,承诺支持更多的图像格式,以及一堆你可能已经错过的新功能和改进。Shotwell0.32.0在这里引入了对新图像格式的支持,包括AVIF、WebP、JPEG-XL、CR3,以及HEIF/HVEC的更多变体。它还引入了在图像中手动标记人物的能力,而且......
  • Verilog实现FIR低通滤波器,vivado平台开发,包含testbench
    1.算法仿真效果vivado2019.2仿真结果如下:    2.算法涉及理论知识概要       FIR(FiniteImpulseResponse)滤波器:有限长单位冲激响应滤波器,又称为非递归型滤波器,是数字信号处理系统中最基本的元件,它可以在保证任意幅频特性的同时具有严格的线性相频特性,同时其......
  • 图像识别入门教程,带你从零到一!
    图像识别是一门利用计算机技术来识别和处理图像中的信息的学科。图像识别的应用非常广泛,例如人脸识别、车牌识别、医学影像分析等。本教程将带你从零到一,了解图像识别的基本原理和方法,以及如何使用Python和TensorFlow等工具来实现图像识别的任务。本教程分为以下几个部分:-第一部......
  • 图像处理-02
    1.为什么要进行频域变化?(用快速傅利叶变换的形式写下来。)一个图象经过傅立叶变换后,就从空域变到了频域,因此我们可以用信号处理中对于频域信号的处理方法对一幅图象进行处理。2.如果直接进行降采样会出现什么效果?所以高斯金字塔的过程应该是?高斯金字塔与拉普拉斯金字塔之间......
  • 基于深度神经网络的图像分类与训练系统(MATLAB GUI版,代码+图文详解)
    摘要:本博客详细介绍了基于深度神经网络的图像分类与训练系统的MATLAB实现代码,包括GUI界面和数据集,可选择模型进行图片分类,支持一键训练神经网络。首先介绍了基于GoogleNet、ResNet进行图像分类的背景、意义,系统研究现状及相关算法。然后展示了系统的界面演示效果,包括选择图片分......