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数据正确时显示的图片

最新文章

  1. 推荐一个自动抽取pdf高亮笔记的web应用
  2. sql install error
  3. switch的经典引用
  4. Android入门第八篇之GridView(九宫图)
  5. 【Selenium】1.介绍 Selenium
  6. 【Android Demo】获取指定网页的页面源代码
  7. 通过xrdp实现远程桌面连接Windows Azure linux虚拟机
  8. Android 高仿微信 获取最近刚刚拍照的缩略图 功能实现
  9. ThinkPHP中的视图二
  10. vc++笔记十一
  11. javaSE_05Java中方法(函数)与重载、递归-练习
  12. Execution failed for task&#39;:app;clean&#39;
  13. js饼状图(带百分比)功能实现,新人必懂
  14. jdk安装路径
  15. mysql 开发基础系列11 存储引擎memory和merge介绍
  16. 在linux上安装Scala详细步骤
  17. 在eclipse中导入hadoop jar包,和必要时导入源码包。
  18. (4.2)mysql备份还原——备份概述
  19. python web.py实现简单的get和post请求
  20. 关于使用CTE(公用表表达式)的递归查询

热门文章

  1. Mysql错误:Ignoring query to other database解决方法
  2. 强制浏览器使用兼容模式,Web.config,httpProtocol
  3. centos 6.6 使用tomcat6部署solr5.3.1
  4. 原生态ajax
  5. 脏检查and刷新机构
  6. OrchardNoCMS模块生成工具命令简化
  7. 物联网平台设计心得:你所不知道的CRC校验
  8. Js前端代码异常监控
  9. 微信开发 企业号(二)-- 回调模式之Tooken验证 .net/python
  10. npoi批量导入实现及相关技巧