标签:仿真 load img title imshow Modelsim rgb Matlab txt
FPGA图像仿真平台
1 引言
在使用modelsim进行图像算法的功能仿真时,无法得到图像的实时预览,因此直观性有所欠缺。因此可配合matlab使用,通过modelsim读出txt格式的图像,利用matlab进行转换与显示,从而既可验证时序关系,又可直观看到算法的效果。
2 matlab代码
2.1 图片读取及通道转换
注意事项:图片与程序需放到同一文件夹内
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ProjectName : ycbcrcode.m
% Author : Huhao
% Description : image to txt
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all
close all
clc
img = imread('card.jpg');
ycbcr = rgb2ycbcr(img);
y=ycbcr(:,:,1);
cb=ycbcr(:,:,2);
cr=ycbcr(:,:,3);
2.2 图片转化为txt格式
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ProjectName : rgb2txt.m
% Author : Huhao
% Description : rgb to text
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all
close all
clc
img = imread('card.jpg');
r=img(:,:,1);
g=img(:,:,2);
b=img(:,:,3);
figure,
subplot(221),imshow(r),title('R');
subplot(222),imshow(g),title('G');
subplot(223),imshow(b),title('B');
subplot(224),imshow(img),title('RGB img');
r1=r';
g1=g';
b1=b';
fid1=fopen('img_r.txt','wt');
% fprintf(fid1,'%x\n',r1);%hexadecimal
fprintf(fid1,'%g\n',r1);%Binary
fclose(fid1);
fid2=fopen('img_g.txt','wt');
% fprintf(fid2,'%x\n',g1);
fprintf(fid2,'%g\n',g1);
fclose(fid2);
fid3=fopen('img_b.txt','wt');
% fprintf(fid3,'%x\n',b1);
fprintf(fid3,'%g\n',b1);
fclose(fid3);
2.3 modelsim输出的txt文档重新转化为图片并显示
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ProjectName : txt2rgb.m
% Author : Huhao
% Description : text to rgb
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all
close all
clc
r_load = load('img_r.txt');
g_load = load('img_g.txt');
b_load = load('img_b.txt');
length = 760;%image length
width = 397;%image width
r2 = reshape(r_load,[length, width]);
r2 = uint8(r2');
g2 = reshape(g_load,[length, width]);
g2 = uint8(g2');
b2 = reshape(b_load,[length, width]);
b2 = uint8(b2');
rgb_img(:,:,1)=r2;
rgb_img(:,:,2)=g2;
rgb_img(:,:,3)=b2;
figure,
subplot(221),imshow(r2),title('R');
subplot(222),imshow(g2),title('G');
subplot(223),imshow(b2),title('B');
subplot(224),imshow(rgb_img),title('RGB img');
3 结果显示
3.1 图片转化为txt结果
![rgb2txt 图片alt](https://raw.githubusercontent.com/aichidoude/DEMO1/master/rgb2txt.jpg)
图1 rgb to txt三通道图片
![rgb2txt 图片alt](https://raw.githubusercontent.com/aichidoude/DEMO1/master/rgb2txt2.jpg)
图2 rgb to txt得到的三通道txt数据
3.2 txt重新转化为图片结果
![rgb2txt 图片alt](https://raw.githubusercontent.com/aichidoude/DEMO1/master/txt2rgb.jpg)
图3 txt to rgb三通道图片
标签:仿真,load,img,title,imshow,Modelsim,rgb,Matlab,txt 来源: https://www.cnblogs.com/chidouxiong/p/15465194.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。