中值滤波可以处理椒盐噪声,膨胀是最大值滤波,腐蚀是最小值滤波。
在黑色背景下,目标为白色,需要滤除白色噪点,那么就要先腐蚀,再膨胀,也成为开运算。
相反的,在白色背景下,目标为黑色,需要先膨胀,再腐蚀。即先用白色“腐蚀”黑色,再填充黑色,也称闭运算。
中值滤波
1 `timescale 1ns / 1ps 2 ////////////////////////////////////////////////////////////////////////////////// 3 // Company: 4 // Engineer: 5 // 6 // Create Date: 2021/01/11 20:21:56 7 // Design Name: 8 // Module Name: mid 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 module mid ( 22 input clk , 23 input rst_n , 24 input matrix_de , 25 input [7:0] matrix11 , 26 input [7:0] matrix12 , 27 input [7:0] matrix13 , 28 input [7:0] matrix21 , 29 input [7:0] matrix22 , 30 input [7:0] matrix23 , 31 input [7:0] matrix31 , 32 input [7:0] matrix32 , 33 input [7:0] matrix33 , 34 output mid_de , 35 output reg [7:0] mid_data 36 ); 37 38 wire [7:0] max_data1 ; 39 wire [7:0] mid_data1 ; 40 wire [7:0] min_data1 ; 41 42 wire [7:0] max_data2 ; 43 wire [7:0] mid_data2 ; 44 wire [7:0] min_data2 ; 45 46 wire [7:0] max_data3 ; 47 wire [7:0] mid_data3 ; 48 wire [7:0] min_data3 ; 49 50 wire [7:0] max_min_data; 51 wire [7:0] mid_mid_data; 52 wire [7:0] min_max_data; 53 54 sort_3 u1_sort_3 ( 55 .clk (clk ), 56 .rst_n (rst_n ), 57 .data1 (matrix11 ), 58 .data2 (matrix12 ), 59 .data3 (matrix13 ), 60 .min_data (min_data1 ), 61 .mid_data (mid_data1 ), 62 .max_data (max_data1 ) 63 ); 64 65 sort_3 u2_sort_3 ( 66 .clk (clk ), 67 .rst_n (rst_n ), 68 .data1 (matrix21 ), 69 .data2 (matrix22 ), 70 .data3 (matrix23 ), 71 .min_data (min_data2 ), 72 .mid_data (mid_data2 ), 73 .max_data (max_data2 ) 74 ); 75 76 sort_3 u3_sort_3 ( 77 .clk (clk ), 78 .rst_n (rst_n ), 79 .data1 (matrix31 ), 80 .data2 (matrix32 ), 81 .data3 (matrix33 ), 82 .min_data (min_data3 ), 83 .mid_data (mid_data3 ), 84 .max_data (max_data3 ) 85 ); 86 87 sort_3 u4_sort_3 ( 88 .clk (clk ), 89 .rst_n (rst_n ), 90 .data1 (max_data1 ), 91 .data2 (max_data2 ), 92 .data3 (max_data3 ), 93 .min_data (max_min_data ), 94 .mid_data ( ), 95 .max_data ( ) 96 ); 97 98 sort_3 u5_sort_3 ( 99 100 .clk (clk ), 101 .rst_n (rst_n ), 102 103 .data1 (mid_data1 ), 104 .data2 (mid_data2 ), 105 .data3 (mid_data3 ), 106 .min_data ( ), 107 .mid_data (mid_mid_data ), 108 .max_data ( ) 109 ); 110 111 sort_3 u6_sort_3 ( 112 .clk (clk ), 113 .rst_n (rst_n ), 114 .data1 (min_data1 ), 115 .data2 (min_data2 ), 116 .data3 (min_data3 ), 117 118 .min_data ( ), 119 .mid_data ( ), 120 .max_data (min_max_data ) 121 ); 122 123 wire [7:0] target_data ; 124 125 sort_3 u7_sort_3 ( 126 127 .clk (clk ), 128 .rst_n (rst_n ), 129 130 .data1 (max_min_data ), 131 .data2 (mid_mid_data ), 132 .data3 (min_max_data ), 133 .min_data ( ), 134 .mid_data (target_data ), 135 .max_data ( ) 136 ); 137 138 reg [3:0] matrix_de_r; 139 140 always@(posedge clk) 141 if(!rst_n) 142 matrix_de_r <= 4'd0; 143 else 144 matrix_de_r <= {matrix_de_r[2:0],matrix_de}; 145 146 always@(posedge clk) 147 if(!rst_n) 148 mid_data <= 8'd0; 149 else if(matrix_de_r[2]==1'b1) 150 mid_data <= target_data; 151 else 152 mid_data <= 8'd0; 153 154 155 assign mid_de = matrix_de_r[3]; 156 157 158 159 endmodule
排序代码
1 ////////////////////////////////////////////////////////////////////////////////// 2 // Company: 3 // Engineer: 4 // 5 // Create Date: 2021/01/01 19:39:38 6 // Design Name: 7 // Module Name: sort_3 8 // Project Name: 9 // Target Devices: 10 // Tool Versions: 11 // Description: 12 // 13 // Dependencies: 14 // 15 // Revision: 16 // Revision 0.01 - File Created 17 // Additional Comments: 18 // 19 ////////////////////////////////////////////////////////////////////////////////// 20 module sort_3( 21 input clk , 22 input rst_n , 23 24 input [7:0] data1 , 25 input [7:0] data2 , 26 input [7:0] data3 , 27 output reg [7:0] min_data , 28 output reg [7:0] mid_data , 29 output reg [7:0] max_data 30 ); 31 32 always@(posedge clk) 33 if(!rst_n) 34 min_data <= 0; 35 else if(data1 <= data2 && data1 <= data3) 36 min_data <= data1; 37 else if(data2 <= data1 && data2 <= data3) 38 min_data <= data2; 39 else 40 min_data <= data3; 41 42 always@(posedge clk) 43 if(!rst_n) 44 mid_data <= 0; 45 else if((data1 >= data2 && data1 <= data3) || (data1 >= data3 && data1 <= data2)) 46 mid_data <= data1; 47 else if((data2 >= data1 && data2 <= data3) || (data2 >= data3 && data2 <= data1)) 48 mid_data <= data2; 49 else 50 mid_data <= data3; 51 52 always@(posedge clk) 53 if(!rst_n) 54 max_data <= 0; 55 else if(data1 >= data2 && data1 >= data3) 56 max_data <= data1; 57 else if(data2 >= data1 && data2 >= data3) 58 max_data <= data2; 59 else 60 max_data <= data3; 61 62 63 64 65 66 67 endmodule
腐蚀膨胀和中值滤波原理相似,只不过是分别求最大值和最小值滤波。