(1)理论学习
- 灰度像素:在 RGB 颜色模型下,图像中每个像素颜色的 R、G、B 三种基色的分量值相等的像素。
- 由灰度像素组成的灰度图像只能表现256中颜色(或亮度),通常把灰度图像中像素的亮度称为灰度值。
- 灰度化处理:是指把彩色图像转化为灰色图像的处理过程,常用的方法有三种:最大值法、平均值法、加权平均值法。
- 最大值法:使R、G、B的值三个值中最大的一个,即:R = G = B = max(R,G,B),用最大值法对彩色图像进行灰度化处理会使得图像的整体亮度增加。
- 平均值法:对R、G、B求平均值,即R = G = B = (R,G,B)/3,用平均值法对彩色图像灰度化处理会形成比较柔和的灰度图像。
- 加权平均值法:根据三种基色的重要性或其他指标赋予不同程度的权值,即:
由于人眼对绿色的敏感度最高,对红色的敏感度次之,对蓝色的敏感度最低,因此使 Wg > Wr > Wb 将得到比较合理的灰度图像。实验和理论推导证明,Wr = 0.299、Wg = 0.587、Wb = 0.114时效果最佳。R = G = B = (0.299R + 0.587G + 0.114B)/( 0.299 + 0.587 + 0.114) =0.299R + 0.587G +0.114B。
- YUV色域:“Y"表示亮度,”U“和”V“则是色度和浓度。RGB转YUV公式如下:
(2)实验目标
使用ov5640双目摄像头的其中一个,经过灰度处理以后传入DDR3,最终在HDMI显示屏上实时显示。
(3)灰度处理模块代码及顶层模块,其余模块请参考之前实验。
module rgb_yuv
(input wire [15:0] RGB565_data ,output wire [7:0] Y ,output wire [7:0] U ,output wire [7:0] V
);parameter YR = 306; //0.299 * 1024 = 306.176
parameter YG = 601; //0.587 * 1024 = 601.088
parameter YB = 117; //0.114 * 1024 = 116.736
parameter YW = 0;parameter UR = -173; //-0.169 * 1024 = -173.056
parameter UG = -339; //-0.331 * 1024 = -338.944
parameter UB = 512; //0.5 * 1024 = 512
parameter UW = 131072; //128 * 1024 = 131072parameter VR = 512; //0.5 * 1024 = 512
parameter VG = -429; //-0.419 * 1024 = -429.056
parameter VB = -83; //-0.081 * 1024 = -82.944
parameter VW = 131072; //128 * 1024 = 131072wire [7:0] R;
wire [7:0] G;
wire [7:0] B;wire [17:0] temp_Y;
wire [17:0] temp_U;
wire [17:0] temp_V;assign R = {RGB565_data[15:11],3'd0};
assign G = {RGB565_data[10:5],2'd0};
assign B = {RGB565_data[4:0],3'd0};assign temp_Y = YR * R + YG * G + YB * B + YW;
assign temp_U = UR * R + UG * G + UB * B + UW;
assign temp_V = VR * R + VG * G + VB * B + VW;assign Y = temp_Y[17:10];
assign U = temp_U[17:10];
assign V = temp_V[17:10];endmodule
module ov5640_yuv_ddr_hdmi
(input wire clk ,input wire reset_n ,
//ov5640 input wire ov5640_pclk ,input wire ov5640_href ,input wire ov5640_vsync ,input wire [7:0] ov5640_data ,output wire ov5640_scl ,inout wire ov5640_sda ,output wire ov5640_pwdn ,output wire ov5640_rst_n ,
//vga-hdmi接口output wire vga_hsync ,output wire vga_vsync ,output wire vga_clk ,output wire vga_rst_n ,output wire [23:0] vga_rgb ,output wire vga_DE ,
//hdmi配置端接口output wire ddc_scl ,inout wire ddc_sda ,
//DDR3物理接口output wire [14:0] ddr3_addr ,output wire [2:0] ddr3_ba ,output wire ddr3_cas_n ,output wire ddr3_ck_n ,output wire ddr3_ck_p ,output wire ddr3_cke ,output wire ddr3_ras_n ,output wire ddr3_reset_n ,output wire ddr3_we_n ,inout wire [31:0] ddr3_dq ,inout wire [3:0] ddr3_dqs_n ,inout wire [3:0] ddr3_dqs_p ,output wire ddr3_cs_n ,output wire [3:0] ddr3_dm ,output wire ddr3_odt
);parameter H_PIXEL = 24'd640 ;
parameter V_PIXEL = 24'd480 ;wire rst_n ;
wire cfg_done ;
wire ov5640_wren ;
wire [15:0] RGB565_data ;
wire [7:0] Y ;
wire [15:0] data_wr ;
wire data_rden ;
wire [15:0] data_rd ;
wire clk_25M ;
wire clk_320M ;
wire locked ;
wire init_calib_complete ;
wire [23:0] data_in ;
wire init_rst_n ;
wire sys_init_done ;assign rst_n = reset_n & locked ;
assign init_rst_n = rst_n & init_calib_complete;
assign ov5640_pwdn = 1'd0 ;
assign ov5640_rst_n = 1'd1 ;
assign vga_rst_n = rst_n ;
assign sys_init_done = cfg_done & init_calib_complete;pll pll_inst
(.clk_25M (clk_25M ), .clk_320M (clk_320M ), .reset (~reset_n ),.locked (locked ), .clk_in1 (clk )
); ov5640_top ov5640_top_inst
(.clk (clk_25M ),.reset_n (reset_n ),.sys_init_done (sys_init_done ),.ov5640_pclk (ov5640_pclk ),.ov5640_href (ov5640_href ),.ov5640_vsync (ov5640_vsync ),.ov5640_data (ov5640_data ),.cfg_scl (ov5640_scl ),.cfg_sda (ov5640_sda ),.cfg_done (cfg_done ), .ov5640_wren (ov5640_wren ),.data_out (RGB565_data )
);rgb_yuv rgb_yuv_inst
(.RGB565_data (RGB565_data),.Y (Y ),.U (),.V ()
);assign data_wr = {Y[7:3],Y[7:2],Y[7:3]};axi_ddr3_top axi_ddr3_top_inst
(.ddr3_clk (clk_320M ),.reset_n (rst_n ),.pingpang (1'd0 ),.ui_clk (),.ui_rst (),.wr_b_addr (32'd0 ),.wr_e_addr (H_PIXEL * V_PIXEL * 2 ),.wr_clk (ov5640_pclk ),.data_wren (ov5640_wren ),.data_wr (data_wr ),.wr_rst (1'd0 ),.rd_b_addr (32'd0 ),.rd_e_addr (H_PIXEL * V_PIXEL * 2 ),.rd_clk (clk_25M ),.data_rden (data_rden ),.data_rd (data_rd ),.rd_rst (1'd0 ),.read_enable (1'd1 ),.rd_data_valid (),.ddr3_addr (ddr3_addr ),.ddr3_ba (ddr3_ba ),.ddr3_cas_n (ddr3_cas_n ),.ddr3_ck_n (ddr3_ck_n ),.ddr3_ck_p (ddr3_ck_p ),.ddr3_cke (ddr3_cke ),.ddr3_ras_n (ddr3_ras_n ),.ddr3_reset_n (ddr3_reset_n ),.ddr3_we_n (ddr3_we_n ),.ddr3_dq (ddr3_dq ),.ddr3_dqs_n (ddr3_dqs_n ),.ddr3_dqs_p (ddr3_dqs_p ),.init_calib_complete (init_calib_complete ),.ddr3_cs_n (ddr3_cs_n ),.ddr3_dm (ddr3_dm ),.ddr3_odt (ddr3_odt )
);hdmi_iic hdmi_iic_inst
(.hdmi_clk (clk_25M ),.reset_n (rst_n ),.hdmi_scl (ddc_scl ),.hdmi_sda (ddc_sda )
);vga_ctrl vga_ctrl_inst
(.vga_clk (clk_25M ),.reset_n (init_rst_n ),.data_in (data_in ),.hang (),.lie (),.hsync (vga_hsync ),.vsync (vga_vsync ),.rgb_vga (vga_rgb ),.vga_DE (vga_DE )
);assign vga_clk = clk_25M;
assign data_rden = vga_DE;
assign data_in = {data_rd[15:11],3'b000,data_rd[10:5],2'b00,data_rd[4:0],3'b000}; endmodule
(4)实验现象:
- 实验板卡及摄像头:
- 没有经过灰度处理的HDMI显示屏显示情况:
- 经过灰度处理的HDMI显示屏显示情况: