在紫光PGL50H上简单实现一下图像灰度化处理。
01 软硬件平台
软件平台:PDS_2022.1
硬件平台:小眼睛科技盘古50K开发板
02 灰度化处理
灰度就是没有色彩, RGB色彩分量全部相等。将像素点RGB值转换为灰度的公式为
Gray = R×0.299 + G×0.587 + B×0.114;
但直接使用FPGA实现浮点运算是不可能的,所以将RGB灰度系数放大1024倍,得到结果后再右移10,即可得到0-255区间大小的灰度值。实现公式为:
Gray = (R×306 + G×601 + B×117)<<10;
03 程序设计
基于小眼睛科技提供的 HDMI 回环实验,对输入的RGB像素值套用灰度公式,然后将行场数据同步信号流水延迟对齐灰度数据输出。
module hdmi_gray_test(
input wire sys_clk,
output rstn_out,
output iic_scl,
inout iic_sda,
output iic_tx_scl,
inout iic_tx_sda,
input pixclk_in,
input vs_in,
input hs_in,
input de_in,
input [7:0] r_in,
input [7:0] g_in,
input [7:0] b_in,
output pixclk_out,
output reg vs_out,
output reg hs_out,
output reg de_out,
output reg [7:0] r_out,
output reg [7:0] g_out,
output reg [7:0] b_out,
output led_int
);
reg [15:0] rstn_1ms ;
wire pix_clk ;
wire cfg_clk ;
wire locked ;
pll u_pll(
.clkin1 (sys_clk ),
.pll_lock (locked ),
.clkout0 (cfg_clk )
);
ms72xx_ctl ms72xx_ctl(
.clk ( cfg_clk ),
.rst_n ( rstn_out ),
.init_over ( init_over ),
.iic_tx_scl ( iic_tx_scl ),
.iic_tx_sda ( iic_tx_sda ),
.iic_scl ( iic_scl ),
.iic_sda ( iic_sda )
);
assign led_int = init_over;
assign rstn_out = (rstn_1ms == 16'h2710);
assign pixclk_out = pixclk_in;
always @(posedge cfg_clk) begin
if(!locked) rstn_1ms <= 16'd0;
else if(rstn_1ms == 16'h2710) rstn_1ms <= rstn_1ms;
else rstn_1ms <= rstn_1ms + 1'b1;
end
reg [7:0] gray;
reg [7:0] value;
reg vs_temp0;
reg hs_temp0;
reg de_temp0;
reg vs_temp1;
reg hs_temp1;
reg de_temp1;
always @(posedge pixclk_out) begin
if(!init_over)
gray <= 8'd0;
else
gray <= (r_in*306 + g_in*601 + b_in*117)>>10;
end
always @(posedge pixclk_out) begin
if(!init_over)
value <= 8'h00;
else if(gray >= 8'd150)
value <= 8'hff;
else
value <= 8'h00;
end
always @(posedge pixclk_out) begin
vs_temp0 <= vs_in ;
hs_temp0 <= hs_in ;
de_temp0 <= de_in ;
vs_temp1 <= vs_temp0;
hs_temp1 <= hs_temp0;
de_temp1 <= de_temp0;
end
always @(posedge pixclk_out) begin
if(!init_over)begin
vs_out <= 1'b0 ;
hs_out <= 1'b0 ;
de_out <= 1'b0 ;
r_out <= 8'b0 ;
g_out <= 8'b0 ;
b_out <= 8'b0 ;
end
else begin
vs_out <= vs_temp0 ;
hs_out <= hs_temp0 ;
de_out <= de_temp0 ;
r_out <= gray ;
g_out <= gray ;
b_out <= gray ;
end
end
endmodule
04 灰度显示
灰度效果如下视频所示。