Implementation of converting RGB images to Ycbcr based on FPGA, including tb test files and MATLAB-assisted verification

Table of contents

1. Preview of algorithm operation renderings

2.Algorithm running software version

3. Some core programs

4. Overview of algorithm theory

5. Algorithm complete program engineering


1. Preview of algorithm operation renderings

Import FPGA data into matlab for display

2.Algorithm running software version

Vivado2019.2

matlab2022a

3. Some core programs

`timescale 1ns / 1ps
//
// Company: 
// Engineer: 
// 
// Create Date: 2023/08/01  
// Design Name: 
// Module Name: RGB2gray
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//

module test_image;

reg i_clk;
reg i_rst;
reg [7:0] Rbuff [0:100000];
reg [7:0] Gbuff [0:100000];
reg [7:0] Bbuff [0:100000];
reg [7:0] i_Ir,i_Ig,i_Ib;
wire [7:0] o_Y,o_Cr,o_Cb;
integer fids1,dat1,fids2,dat2,fids3,dat3,jj=0;
 
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

initial 
begin
	fids3 = $fopen("D:\\FPGA_Proj\\FPGAtest\\code\\b.bmp","rb");
	dat3 = $fread(Bbuff,fids3);
	$fclose(fids3);
end


 
initial 
begin
i_clk=1;
i_rst=1;
#1200;
i_rst=0;
end 

always #5  i_clk=~i_clk;
 
always@(posedge i_clk) 
begin
	i_Ir<=Rbuff[jj];
	i_Ig<=Gbuff[jj];
	i_Ib<=Bbuff[jj];
	jj<=jj+1;
end
 

 
main_gray main_gray_u(
.i_clk    (i_clk),
.i_rst    (i_rst),
.i_image_R      (i_Ir),
.i_image_G      (i_Ig),
.i_image_B      (i_Ib),
.o_Y            (o_Y),// Y 
.o_Cr           (o_Cr),// Y 
.o_Cb           (o_Cb)// Y 
);


。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。


integer fout3;
initial begin
 fout3 = $fopen("Cb.txt","w");
end

always @ (posedge i_clk)
 begin
    if(jj<=66616)
	$fwrite(fout3,"%d\n",o_Cb);
	else
	$fwrite(fout3,"%d\n",0);
end
endmodule
0X_004m

4. Overview of algorithm theory

      The implementation of FPGA-based RGB image to Ycbcr conversion is mainly through the design of digital circuits and the use of hardware parallel processing capabilities to quickly complete the conversion of image data.

        RGB and YcbCr are both representations of color space. RGB is a color standard defined based on the colors recognized by the human eye. The three colors R (red), G (green), and B (blue) can be mixed to produce all colors. YCbCr is a color space mainly used for digital image and video processing. Y represents the brightness component, and Cb and Cr represent the chroma component.

The conversion formula from RGB to YCbCr is as follows:

Y = 0.299R + 0.587G + 0.114B
Cb = -0.169R - 0.331G + 0.5B + 128
Cr = 0.5R - 0.419G - 0.081B + 128

The above formula is based on the following definition:

  • Y is the brightness component, indicating how bright the image is.
  • Cb is the blue chroma component, representing the intensity of blue and magenta.
  • Cr is the red chromaticity component, indicating the intensity of red and cyan.

      To realize the conversion from RGB to YCbCr on FPGA, a digital circuit module can be written by hardware description language (such as VHDL or Verilog). This module receives RGB image data as input, then calculates the corresponding YCbCr value according to the above formula, and outputs it.

The specific implementation process may include the following steps:

  1. Define an appropriate data path and registers to store RGB image data and intermediate values ​​during calculations.
  2. Design appropriate combinational logic or sequential logic to implement the calculation of the above formula. This may include some basic mathematical operations such as addition and multiplication.
  3. Design control logic to control the entire conversion process, including data input and output.
  4. Implement this module on the FPGA and perform appropriate testing to ensure it works properly.

       This conversion is very common in video codecs because the YCbCr format is more beneficial to the human visual system and can better compress image data. For applications that require real-time processing of large amounts of image data, such as video stream processing, using the parallel processing capabilities of FPGA can greatly increase the processing speed.

5. Algorithm complete program engineering

OOOOO

OOO

O

Guess you like

Origin blog.csdn.net/aycd1234/article/details/132768732