1 YUV2RGB的模块如下:
module yuv2rgb(
clk, //时钟输入
rstn, //复位输入,低电平复位 y_in, //变换前Y分量输出
cb_in, //变换前Cb分量输出
cr_in, //变换前Cr分量输出
ena_in, //待变换数据使能,当它为高时,输入数据有效 R_out, //变换后R分量输出
G_out, //变换后G分量输出
B_out, //变换后B分量输出
ena_out //变换后数据使能输出
);
测试模块功能的方法:
step1 用MATLAB读入一张RGB图片,将RGB转成YUV数据保存在txt文件中;
step2 用该模块把YUV数据转换成RGB数据并保存;
step3 用MATLAB读取模块转换的RGB数据做显示。
接下来详细说明step1~3的实现过程。
2 step1 用MATLAB读入一张RGB图片,将RGB转成YUV数据保存在txt文件中;
clc;close all;clear
RGBimg =imread('Penguins_720p.jpg'); %%用画图软件重新调整像素大小得到的720p图片
figure;imshow(RGBimg);
YUVimg = rgb2ycbcr(RGBimg); %%matlab中的转换函数
figure;imshow(YUVimg); [Hs Vs Dim] = size(YUVimg);
yuvimout = zeros(,Hs*Vs*Dim);
yuvimout(::Hs*Vs*Dim) = reshape(YUVimg(:,:,)',1,Hs*Vs); %%Y
yuvimout(::Hs*Vs*Dim) = reshape(YUVimg(:,:,)',1,Hs*Vs); %%U
yuvimout(::Hs*Vs*Dim) = reshape(YUVimg(:,:,)',1,Hs*Vs); %%V
fid= fopen('Penguins_720p.txt','w'); %%YUV数据写入到txt
fprintf(fid,'%02x\n',yuvimout); %%2位十六进制格式
fclose(fid); fid= fopen('Penguins_720p.yuv','rb'); %%用“7yuv”专业软件转换得到的yuv数据
yuvdat = fread(fid,'uint8');
yuvdat = yuvdat';
fclose(fid);
subAB = yuvdat-yuvimout; %%比较下matlab转换的yuv数据
figure;stem(find(subAB~=));
3 step2 用该模块把YUV数据转换成RGB数据并保存;
testbench的代码如下:
`timescale 1ns / 1ps module tb_yuv2rgb;
// Inputs
reg clk;
reg rstn; //复位输入,低电平复位
reg [:] y_in;
reg [:] cb_in;
reg [:] cr_in;
reg ena_in;
// Outputs
wire [:] R_out;
wire [:] G_out;
wire [:] B_out;
wire ena_out;
// Instantiate the Unit Under Test (UUT)
yuv2rgb uut (
.clk(clk),
.rstn(rstn),
.y_in(y_in),
.cb_in(cb_in),
.cr_in(cr_in),
.ena_in(ena_in),
.R_out(R_out),
.G_out(G_out),
.B_out(B_out),
.ena_out(ena_out)
); localparam PIXNUM_1080P =(**);
localparam PIXNUM_720P =(**); //read pixel from .txt file
reg[:] mem_imgpixel[:**];
reg [:] pixaddr;
integer fid,i;
initial begin //读取图片的YUV数据
$readmemh("Penguins_720p.txt",mem_imgpixel);
pixaddr = ;
#PIXNUM_1080P begin //等待图片的数据转换完成
fid = $fopen("Penguins_720pRGBout.txt","w");
for(i=; i<PIXNUM_720P; i=i+)
$fdisplay(fid,"%2x",mem_rgbout[i]);//输出转换的RGB数据
$fclose(fid);
$stop;
end
end
//clk的上升沿给输入的yuv数据
always @(posedge clk or negedge rstn) begin
if(!rstn) begin
y_in <= 'b0;
cb_in <= 'b0;
cr_in <= 'b0;
ena_in <= 'b0;
pixaddr<= ;
end
else begin
y_in <= mem_imgpixel[pixaddr];
cb_in <= mem_imgpixel[pixaddr+];
cr_in <= mem_imgpixel[pixaddr+];
ena_in <= 'b1;
pixaddr<= pixaddr + ;
end
end reg[:] outaddr;
reg[:] mem_rgbout[:**];//clk的下降沿读取转换的rgb数据
always @(negedge clk or negedge rstn) begin
if(!rstn) begin
outaddr <= ;
end
else begin //存入对应下标
mem_rgbout[outaddr] <= R_out;
mem_rgbout[outaddr+] <= G_out;
mem_rgbout[outaddr+] <= B_out;
outaddr <= outaddr + ; //下标增加3
end
end initial begin
// Initialize Inputs
clk = ;
rstn = ;
y_in = ;
cb_in = ;
cr_in = ;
ena_in = ;
#;
rstn = ;
// Wait 100 ns for global reset to finish
#;
rstn = ;
// Add stimulus here
end
always # clk =~clk;
endmodule
4 step3 用MATLAB读取模块转换的RGB数据做显示。
clc;close all;clear filename = 'Penguins_720pRGBout.txt';
fid = fopen(filename,'r');
rgbdat = fscanf(fid,'%x');
rgbdat = uint8(rgbdat'); %%转换为uint8
fclose(fid); imglen = ; imgwidth = ;
len = length(rgbdat);
r = rgbdat(::len);
r = reshape(r,imglen,imgwidth);
r = r'; g = rgbdat(::len);
g = reshape(g,imglen,imgwidth);
g = g'; b = rgbdat(::len);
b = reshape(b,imglen,imgwidth);
b = b'; rgbimg = cat(,r,g,b);
imshow(rgbimg);
step3中rgb数据正确时显示的图片